-
invalid user entry
I am using VC++ and am trying to figure out how to do a loop such that when the user enters a char of str the program will display error and ask the user to retry. I have it working for numbers but dont know how to solve the other invalid entry of letters. Any ideas?
Code:
void menu (int &selection)
{
cout << " Available Currencies Menu" << endl << endl; //Display Menu
cout << "1. British Pounds" << endl;
cout << "2. Canadian Dollars" << endl;
cout << "3. Indian Rupees" << endl;
cout << "4. Japanese Yens" << endl;
cout << "5. Mexican Pesos" << endl << endl;
cout << "Please enter your selection number from the list above: # "; //user enters menu selection
cin >> selection;
//validate selection
while (selection < 1 || selection > 5) //user entry validated
{
cout << "Invalid Entry! Please Enter Number Between 1-5" << endl; //if invalid, error displayed
cout << "Please try again: # "; //user enters menu selection
cin >> selection;
}//end while
}//end menu selection function
-
maybe try
do
if(selection < 1 && selection > 5)
{
work
}
else
{
dont work
}
just a geuss see if it works
-
Make the selection variable a char, then just use similar valiadation code:
Code:
while ( selection >= '1' && '5' >= selection ) {
// Process
}
The way this works is that cin will always enter a char value, so you test that char value with the range of '1' - '5' instead of the integer values 1 - 5. If anything but '1', '2', '3', '4', or '5' is entered, the input is invalid.
-Prelude
-
Didn't work I think because the selection points to an array value that is const ????
I thought that if i specified what the valid selections that anything other than those would give error irregardless of what the entry is. I guess it all part of the learning process.