Limit input to numbers only

I recently created a program that will create a math problem based on user input. By entering either 1-4, the program can create a problem, or the user can exit it by typing 5. The only problem I encounter is that when I enter a character, the program goes into an endless loop. What function could I use to check if the input is not a number so that I can display an error message?


//CIS180 Assignment #4 #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; int main() { //Declare variables. int num1, num2, menuNum; int addInput, subInput, multInput, divInput; int addAnswer, subAnswer, multAnswer, divAnswer; int addSolution, subSolution, multSolution, divSolution; srand(time(0)); //Display menu. cout << "Menu" << endl; cout << "1. Addition problem" << endl; cout << "2. Subtraction problem" << endl; cout << "3. Multiplication problem" << endl; cout << "4. Division problem" << endl; cout << "5. Quit this program" << endl << endl; cout << "Enter your choice (1-5): " << endl; cin >> menuNum; //Loop that will provide math problems when user inputs number. while(menuNum != 5) { //Check if the input is valid. while((menuNum < 1) || (menuNum >5)) { cout << "The valid choices are 1, 2, 3 , 4, and 5. Please choose: " << endl; cin >> menuNum; } //Generate two random numbers for addition and display output. if(menuNum == 1) { num1 = rand()%500 + 1; num2 = rand()%500 + 1; addSolution = num1 + num2; cout << setw(5) << right << num1 << endl; cout << setw(2) << left << "+" << setw(3) << right << num2 << endl; cout << setw(5) << fixed << "-----" << endl; cin >> addAnswer; //Check if the addition answer input is correct. if(addAnswer != addSolution) cout << "Sorry, the correct answer is " << addSolution << "." << endl; else if(addAnswer == addSolution) cout << "Congratulations! That right." << endl << endl; } . . . 
+4
source share
3 answers

First, you must determine if your typing attempt was successful: always check after that the read attempt was successful. Then, when you find that you cannot read the value, you need to reset to transfer the stream to a good state with clear() , and you will need to get rid of any bad characters, for example, using ignore() , given that the characters are usually , were entered, that is, the user had to strike back before the characters were used, he, as a rule, can be used to get the entire line. For instance:

 for (choice = -1; !(1 <= choice && choice <= 5); ) { if (!(std::cin >> choice)) { std::cout << "invalid character was added (ignoring the line)\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } 

Using std::numeric_limits<std::streamsize>::max() is a way to get a magic number that ignore() makes as many characters as possible until a character with the value of its second argument is found.

+7
source
  • Read one character
  • If this character is a digit, use it.
  • If this character was not a number, go to 1.

Actually, forget about step 2. Just check if it was one of the numbers you really want ( '1' , '2' , '3' , '4' , '5' ):

 char choice; while(cin.get(choice)){ if(choice == '5') break; switch(choice){ default: printWrongCharacterMessage(); break; case '1': do1Stuff(); break; case '2': do2Stuff(); break; case '3': do3Stuff(); break; case '4': do4Stuff(); break; } } 
+4
source

You can use isdigit from ctype.h

+1
source

All Articles