Hi guys i wonder if u can help me im just tryin to create a simple menu using a switch function where the user can select a value from the list and go to that case. Ive got that working fine but i want it to validate that the number input by the user is within the values of the menu and that its not a char, so far ive got:
CODE
#include <iostream>
using namespace std;
const char BEEP = '\a';
void main ()
{
int selection;
system("cls");
cout << endl;
cout << "\t\t\t\t\Menu\n\n";
cout << "Welcome to menu...\n\n";
cout << "\t1 = Scenario 1\n";
cout << "\t2 = Scenario 2\n";
cout << "\t3 = Scenario 3\n";
cout << "\t0 = Scenario 0\n\n";
cout << "Please select an option from the menu: ";
cin >> selection;
while((!cin.good()) || (selection <0) || (selection >3))
{
cin.clear();
cout << BEEP <<"\nError! Please select a value from the menu (0-3): ";
cin.get();
cin >> selection;
}
switch (selection)
{
case 0:
system("cls");
cout << "\nScenario 0\n\n";
system("pause");
break;
case 1:
system("cls");
cout << "\nScenario 1\n\n";
system("pause");
break;
case 2:
system("cls");
cout << "\nScenario 2\n\n";
system("pause");
break;
case 3:
system("cls");
cout << "\nScenario 3\n\n";
system("pause");
break;
}
}
This works but if u enter more than one character then it loops around the error more than once, does anyone no how to prevent it doin this?
Cheers guys
This post has been edited by Reedo321: 17 Apr, 2007 - 10:52 AM