Well, I have this problem because I started my journey with C# and I wrote a console calculator, but I used goto
in it and I heard to not use it, but I have no clue what other instruction to use to make it work just how I want it to. I paste my code (it's not long) here, so maybe you can look at it and give me some tips. Thanks!
base:
Console.WriteLine("First number");
double first = double.Parse(Console.ReadLine());
Console.WriteLine("Second number");
double second = double.Parse(Console.ReadLine());
Console.WriteLine("What operation do you want to perform? \n1 - addition \n2 - subtraction \n3 - multiplication \n4 - division");
string choice = Console.ReadLine();
if (choice == "1")
{
Console.WriteLine(first + second);
Console.ReadLine();
}
if (choice == "2")
{
Console.WriteLine(first - second);
Console.ReadLine();
}
if (choice == "3")
{
Console.WriteLine(first * second);
Console.ReadLine();
}
else if (choice == "4" && second == 0)
{
Console.WriteLine(" Do not divide by zero");
}
if (choice == "4")
{
Console.WriteLine(first / second);
Console.ReadLine();
}
Console.WriteLine("Next one? \n1 - Yes \n2 - No");
string chc2 = Console.ReadLine();
if (chc2 == "1")
{
goto base;
}
if (chc2 == "2")
{
Console.WriteLine("Bye bye");
Console.ReadKey();
}
Use loops. Here's one common way:
while (true) {
// do some stuff
Console.WriteLine("Next one? \n1 - Yes \n2 - No");
string chc2 = Console.ReadLine();
if (chc2 == "1")
{
continue; // means go back to the top of the loop
}
if (chc2 == "2")
{
Console.WriteLine("Bye bye");
Console.ReadKey();
break; // means break out of the loop
}
}