Help creating a loop
I need to create a loop that can be used for input validation. With valid entries between 10 and 50. Ive tried a few different things, its seems so easy but Im just stuck.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Cameron WardPosted Aug 11, 2011, 1:09 AM
string inValue = "";
double number = 0;
Console.WriteLine("Enter a number between 10 and 50");
Console.WriteLine("\nTo stop, enter 1");
while (inValue!="1")
{ Console.WriteLine("Enter value(1 to exit)");
inValue = Console.ReadLine();
number = Convert.ToDouble(inValue);
if (number < 10 || number > 50)
{ Console.WriteLine("Invalid number, please enter another number");
}
else { Console.WriteLine("Correct Number");
}
VulpesPosted Aug 10, 2011, 4:12 AM
int number;
while(true)
{
Console.Write("Enter a number between 10 and 50 : ");
bool isValid = int.TryParse(Console.ReadLine(), out number);
if (!isValid)
{
Console.WriteLine("\nNot a valid numnber, try again");
}
else (if (number < 10 || number > 50)
{
Console.WriteLine("\nMust be between 10 and 50, try again");
}
else
{
Console.WriteLine();
break;
}
}