how to deny string input in c 2b 2b

Solutions on MaxInterview for how to deny string input in c 2b 2b by the best coders in the world

showing results for - "how to deny string input in c 2b 2b"
Scott
03 Sep 2016
1while( ! cin >> x){ //While cin failed to stream the data; Reads input then checks if cin has failed
2//Alternative:
3/*
4cin >> x;
5while(cin.fail()){
6*/
7   cin.clear(); //Reset the flags, so you can use cin again
8   cin.ignore(100, '\n'); //Empty the buffer
9   cout << "Please enter a number!\n";
10/* If alternative is used:
11   cin >> x; //Read input again
12*/
13}