I have encountered a problem where my program crashes due to the input of the user not following the variable data type. What I am asking the user to do is register some data f
bool ok = false;
int release;
while (!ok)
{
Console.Write("Ange release: ");
ok = int.TryParse(Console.ReadLine(), out release);
ok = ok && (release >= 1000) && (release <= 2050);
}
Use TryParse instead.
Console.Write("Ange release: ");
int release;
while (!int.TryParse(Console.ReadLine(), out release))
{
Console.Write("Ange release: ");
}
Use Int32.TryParse()
and handle the case when it's not an integer. The return value indicates whether the operation succeeded.
For more information see
http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx