How can I stop my never-ending loop

closed account (167DSL3A)
At the moment I have a menu displaying four choices (1. exit, 2. statistics, 3. find mark, 4. display mark). If the user types anything else apart from 0-3, then an error message will be displayed. I somehow cannot get the error message to display properly when a user types in a letter (a,b,c), instead, it just continuously displays the error message (never-ending loop). Here is what my code looks like:

#include <iostream>
using namespace std;


int main()
{
int menu;
string studentID[] = {"P1001", "P1002"};
float studentMark[] = {78.50, 66};
int i;
int j;
int totalStudents=0;
float totalMarks=0;
float averageMark;

cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "------------------------------\n"
<< "Your choice -> ";
cin >> menu;

while (menu != 0)
{
switch (menu)
{
case 1:
for(int i=0;i<2;i++)
totalStudents+=studentID;
for(int j=0;j<2;j++)
totalMarks+=studentMark[j];
cout << "Mean or average: " << totalMarks <<endl;
break;
case 2:
cout << "Please enter Student ID: ";
break;
case 3:
cout << "Find mark" << endl;
break;
default:
cout << "Invalid selection. Please make a selection between 0-3.\n"
<< endl;
}
system("Pause");
cout << "MAIN MENU\n"
<< "0. Exit 1. Statistics\n"
<< "2. Enter mark 3. Find mark\n"
<< "----------------------------\n"
<< "Your choice -> ";
cin >> menu;
}

return 0;
}


How can I fix this problem?
You should validate the input: http://www.cplusplus.com/forum/articles/6046/
( and use [code][/code] tags please )
Topic archived. No new replies allowed.