I want to go somewhere in my code with the goto statement but it doesn't skip parts of my code but reads on. I just need to skip the parts i don't need... code:
int getal1, getal2;
int som,verschil,product,quotient;
Console.WriteLine("first number: ");
Console.ForegroundColor = ConsoleColor.Green;
getal1 = Convert.ToInt32(Console.ReadLine());
Console.ResetColor();
Console.WriteLine("choose operator: -,+,*,/");
if (Console.ReadLine() == "+")
{
goto second;
}
else if (Console.ReadLine() == "-")
{
goto second1;
}
second:
Console.WriteLine("Second number(+): ");
Console.ForegroundColor = ConsoleColor.Green;
getal2 = Convert.ToInt32(Console.ReadLine());
Console.ResetColor();
goto Operat;
Operat: som = getal1 + getal2;
verschil = getal1 - getal2;
product = getal1 * getal2;
quotient = getal1 / getal2;
goto optelling;
second1: Console.WriteLine("Second number(-): ");
Console.ForegroundColor = ConsoleColor.Green;
getal2 = Convert.ToInt32(Console.ReadLine());
Console.ResetColor();
goto Operat1;
Operat1:
som = getal1 + getal2;
verschil = getal1 - getal2;
product = getal1 * getal2;
quotient = getal1 / getal2;
goto verschil;
optelling:
Console.Write("De som van " );
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(getal1.ToString());
Console.ResetColor();
Console.Write(" en " );
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(getal2.ToString());
Console.ResetColor();
Console.Write(" is ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(som.ToString());
Console.ResetColor();
goto end;
verschil:
Console.WriteLine("Het verschil van " + getal1.ToString() + " en " + getal2.ToString() + " is " + verschil.ToString());
goto end;
product:
Console.WriteLine("Het product van " + getal1.ToString() + " en " + getal2.ToString() + " is " + product.ToString());
quotient:
Console.WriteLine("Het quotient van " + getal1.ToString() + " en " + getal2.ToString() + " is " + quotient.ToString());
end:
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Press Enter to continue...");
Console.ResetColor();
Console.readkey();
just copy paste this code and help me out of this!!
Your fundamental problem here is that you are naively translating a batch file into C#, rather than thinking about what the batch file does and writing a program in C# that does the same thing, using the style and conventions of C#.
The specific problem here is not your use of goto
, but rather that every time you call Console.ReadLine
it reads another line. When you say:
if (Console.ReadLine() == "+")
{
goto second;
}
else if (Console.ReadLine() == "-")
{
goto second1;
}
That means "if the current line is not +
then check to see if the next line is -
". You meant to say
var line = Console.ReadLine();
if (line == "+")
{
goto second;
}
else if (line == "-")
{
goto second1;
}
Whereas later in your program you correctly call Console.ReadLine
to read the next line. Remember, every time you call Console.ReadLine
in your program you are instructing the console to read the next line, not re-read the current line. You seem to want it to mean one thing sometimes and another thing other times.