how to store string input c++ code example
Example 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
*/
}
Example 2: input a string in c++
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;