Answers for "how to deny string input in c++"

C++
1

how to deny string input in c++

while( ! cin >> x){ //While cin failed to stream the data; Reads input then checks if cin has failed
//Alternative:
/*
cin >> x;
while(cin.fail()){
*/
   cin.clear(); //Reset the flags, so you can use cin again
   cin.ignore(100, '\n'); //Empty the buffer
   cout << "Please enter a number!\n";
/* If alternative is used:
   cin >> x; //Read input again
*/
}
Posted by: Guest on January-14-2021

Code answers related to "how to deny string input in c++"

Browse Popular Code Answers by Language