I am working on a simple class project for school to experiment with inheritance and other essentials in c# but I am having trouble with a certain part. I believe I need a try catch within an unconditional while loop to be certain the user enters the data in the correct form but I also need to be able to break out of the loop from within the error handling code. I have put a comment beneath the code that is giving me problems.
class Program : students
{
static void Main(string[] args)
{
students stu = new students();
Console.Write("Number of students are you recording results for: ");
int studNum = int.Parse(Console.ReadLine());
stu.setNoOfStudents(studNum);
Console.WriteLine();
for (int a = 0; a < studNum; a++)
{
Console.Write("{0}. Forename: ", a + 1);
stu.setForname(Console.ReadLine());
Console.Write("{0}. Surname: ", a + 1);
stu.setSurname(Console.ReadLine());
while (0 == 0)
{
try
{
Console.Write("{0}. Age: ", a + 1);
stu.setstudentAge(int.Parse(Console.ReadLine()));
Console.Write("{0}. Percentage: ", a + 1);
stu.setpercentageMark(int.Parse(Console.ReadLine()));
stu.fillArray();
break;
// This is the block that gives me problems; the
// while loop doesn't break.
}
catch (Exception)
{
Console.WriteLine("This must be a number.");
}
}
}
}
}
I'm not getting an error because its within a try/catch but the while(0 == 0) loop is never broken so the for loop cannot iterate the command. Can someone give me a solution to this?