Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle wrong data type input

In C++, how do you handle wrong inputs? Like, if the program asks for an integer, when you type a character it should be able to do something and then loop to repeat the input but the loop goes infinite when you input a character when an integer is need and vice versa.

like image 407
Zik Avatar asked Apr 27 '12 11:04

Zik


People also ask

How do you handle wrong input in C++?

The thing to do is to clear that flag and discard the bad input from the input buffer. See the C++ FAQ for this, and other examples, including adding a minimum and/or maximum into the condition.

How do I make sure user input is int in C++?

Input the data. Apply isdigit() function that checks whether a given input is numeric character or not. This function takes single argument as an integer and also returns the value of type int.

What is CIN fail in C++?

cin. fail() - This function returns true when an input failure occurs. In this case it would be an input that is not an integer. If the cin fails then the input buffer is kept in an error state.


2 Answers

The reason the program goes into an infinite loop is because std::cin's bad input flag is set due to the input failing. The thing to do is to clear that flag and discard the bad input from the input buffer.

//executes loop if the input fails (e.g., no characters were read) while (std::cout << "Enter a number" && !(std::cin >> num)) {     std::cin.clear(); //clear bad input flag     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input     std::cout << "Invalid input; please re-enter.\n"; } 

See the C++ FAQ for this, and other examples, including adding a minimum and/or maximum into the condition.

Another way would be to get the input as a string and convert it to an integer with std::stoi or some other method that allows checking the conversion.

like image 194
chris Avatar answered Oct 07 '22 11:10

chris


The top voted answer covers the solution really well.

In addition to that answer, this may help visualize what's going on a little better:

int main()      int input = 1;//set to 1 for illustrative purposes     bool cinState = false;     string test = "\0";     while(input != -1){//enter -1 to exit         cout << "Please input (a) character(s): ";//input a character here as a test         cin >> input; //attempting to input a character to an int variable will cause cin to fail         cout << "input: " << input << endl;//input has changed from 1 to 0         cinState = cin;//cin is in bad state, returns false         cout << "cinState: " << cinState << endl;         cin.clear();//bad state flag cleared         cinState = cin;//cin now returns true and will input to a variable         cout << "cinState: " << cinState << endl;         cout << "Please enter character(s): ";         cin >> test;//remaining text in buffer is dumped here. cin will not pause if there is any text left in the buffer.         cout << "test: " << test << endl;     }     return 0;     } 

Dumping the text in the buffer to a variable isn't particularly useful, however it helps visualize why cin.ignore() is necessary.

I noted the change to the input variable as well because if you're using an input variable in your condition for a while loop, or a switch statement it may go into deadlock, or it may fulfill a condition you weren't expecting, which can be more confusing to debug.

like image 43
AWolfAtTheDoor Avatar answered Oct 07 '22 09:10

AWolfAtTheDoor