I am learning C# using the C# programming yellow book by Rob Miles. In this he is discussing an example of a calculator for determining the total glass area needed in square meters. I recreated this code and wanted to try to improve on it with things that made sense for me. For example in his code he used a lot of blank lines which didn't look appealing to me. I wanted to try making it look a bit more clean. I mainly want to eliminate this first blank line, but make sure the blank line would appear on the next one.
This is the original code.
double width, height, woodLength, glassArea;
const double MAX_WIDTH = 5.0;
const double MIN_WIDTH = 0.5;
const double MAX_HEIGHT = 3.0;
const double MIN_HEIGHT = 0.75;
string widthString, heightString;
do {
Console.Write("\nGive the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
widthString = Console.ReadLine();
width = double.Parse(widthString);
if (width < MIN_WIDTH){
Console.WriteLine("Width is too small.");
}
if (width > MAX_WIDTH){
Console.WriteLine("Width is too large.");
}
} while (width < MIN_WIDTH || width > MAX_WIDTH);
I tried to use a goto, but because the loop is a different method(?) it didn't work. Through some googling I found out that goto is very bad practice, but I can't think of any other methods to use. (nor do i know any)
Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
goto first_loop;
do {
Console.Write("\nGive the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
first_loop:
widthString = Console.ReadLine();
width = double.Parse(widthString);
if (width < MIN_WIDTH){
Console.WriteLine("Width is too small.");
}
if (width > MAX_WIDTH){
Console.WriteLine("Width is too large.");
}
} while (width < MIN_WIDTH || width > MAX_WIDTH);
I mainly want to eliminate this first blank line, but make sure the blank line would appear on the next one.
Remove the \n
from the prompt, and instead add it to the end of both of the error messages:
do
{
Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
widthString = Console.ReadLine();
width = double.Parse(widthString);
if (width < MIN_WIDTH)
{
Console.WriteLine("Width is too small.\n");
}
else if (width > MAX_WIDTH)
{
Console.WriteLine("Width is too large.\n");
}
} while (width < MIN_WIDTH || width > MAX_WIDTH);
If I were writing this myself, though, I'd use a boolean to indicate the overall success and check that in the while loop conditional. I'd also use double.TryParse()
as shown below:
bool valid = false;
do
{
valid = true; // assume good until proven otherwise
Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
widthString = Console.ReadLine();
if (double.TryParse(widthString, out width))
{
if (width < MIN_WIDTH || width >MAX_WIDTH )
{
valid = false;
Console.WriteLine("Width is too " + ((width<MIN_WIDTH) ? "small" : "large") + ".");
}
}
else
{
Console.WriteLine("Invalid width entry. Please try again.");
valid = false;
}
if (!valid)
{
Console.WriteLine();
}
} while (!valid);