Jude André Legendre Jude André Legendre - 2 months ago 15
C# Question

Entered wrong value. goto Start;

Edit.
Based on the answers received I realized that I am asking more than I know about at this early stage in my learning and as such, I have decided to forget this question until I have learnt and understand more. I do not want to overstep and confuse myself.


Just a newbie looking for some guidance.

From a previous question I asked here, I was introduced to the goto Start; in the switch statement.

I started using it and it had me thinking that I wanted the program to go to the start in the event the user entered an invalid input. Example, in my calculator below, if the user enters something other than a number, how do I get the program to disregard the input and prompt the user again to enter a number? Maybe even display an "invalid input. please try again" to the user.

Right now, if anything but a number is entered, the console crashes and throws me an error.

Thanks in advance for the assistance

int num1, num2, output;
string op;

Console.Write("\n\n");
Console.WriteLine("Calculator\n");
Console.WriteLine("=============");
Console.Write("\n\n");

Start:

Console.Write("Please enter first number:");
num1 = Convert.ToInt32(Console.ReadLine());


Console.Write("Please enter second number: ");
num2 = Convert.ToInt32(Console.ReadLine());

Operator:

Console.WriteLine("Please select operator: ");
Console.WriteLine("\nAddition : +");
Console.WriteLine("Multiplication: *");
Console.WriteLine("Division: /");
Console.WriteLine("Subtraction: -");
Console.Write("Enter Operator: ");
op = Console.ReadLine();




switch (op)
{
case "+":
output = num1 + num2;
Console.WriteLine("{0} added to {1} = {2}", num1, num2, output);
break;

case "*":
output = num1 * num2;
Console.WriteLine("{0} multiplied by {1} = {2}", num1, num2, output);
break;

case "/":
if (num2 == 0)
{
Console.WriteLine("Cannot divide by zero. Please try again");
goto Start;

}
else
{
output = num1 / num2;
Console.WriteLine("{0} divided by {1} = {2}", num1, num2, output);
break;
}

case "-":
output = num1 - num2;
Console.WriteLine("{0} minus{1} = {2}", num1, num2, output);
break;

default:
Console.WriteLine("You entered an invalid operator. Please try again\n");
goto Operator;

}

Console.WriteLine("\nPress enter to continue....");
Console.ReadLine();

Answer

Please, whatever you do, do NOT use goto. Ever. Maybe except in switch to fall down to other case, but even that should be very rare. It makes your code unreadable, confusing and hard to maintain. Please see spaghetti code.

That said, you should do Int32.TryParse instead of Convert if you are not sure if the input is valid:

int num1;
do
{
    Console.Write("Please enter first number:");
} while (!Int32.TryParse(Console.ReadLine(), out num1));

And change your switch to something like this (to avoid goto):

bool inputOk = false;
while (!inputOk)
{
    Console.WriteLine("Please select operator: ");
    //...
    op = Console.ReadLine();

    inputOk = true;
    switch (op)
    {
        //...
        default:
            Console.WriteLine("You entered an invalid operator. Please try again\n");
            inputOk = false;
            break;
     }
}
Comments