The questions is a lot more specific than the title actually. I'm just going through some C# and I come across an error that I managed to fix but I don't know why.
Why does my do-while loop give me an error when the string power is defined in the loop?
It's all fine and dandy though when I define my string power before the do-whiel loop
String power;
do
{
Console.WriteLine("Enter values X & Y for X+Y");
Console.WriteLine("Your value of X is ");
int numOne = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Your value of Y is ");
int numTwo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your Sum is {0}", numOne+numTwo);
Console.Write("Turn calculator on or off? ");
power = Console.ReadLine();
} while (power == "on" || power =="On" || power =="ON");
}
}
}
This ends up running fine.
But the following example results in an error at the while statement about the name "power" not existing in the current context.
do
{
Console.WriteLine("Enter values X & Y for X+Y");
Console.WriteLine("Your value of X is ");
int numOne = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Your value of Y is ");
int numTwo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your Sum is {0}", numOne+numTwo);
Console.Write("Turn calculator on or off? ");
String power;
power = Console.ReadLine();
} while (power == "on" || power =="On" || power =="ON");
}
}