void get_english_input() {
string input = " ";
stringstream my_string(input);
int ft;
double in;
while(true) {
cout << "Enter an integer value of feet." << endl;
getline(cin, input);
my_string << input;
if(my_string >> ft)
break;
cout << "Invalid input! Please try again." << endl;
}
cout << "you entered " << ft << " as the int value for feet." << endl;
/*while(true) {
cout << "Enter a double value of inches." << endl;
getline(cin, input);
my_string << input;
break;
cout << "Invalid input! Please try again." << endl;
}
cout << "we are done entering english input" << endl;
cout << "feet = " << ft << endl;
cout << "inches = " << in << endl;*/
}
This code is supposed to test if the input is an integer by trying to put the contents of my_string into ft. If I enter a letter instead of an integer I get the error message "Invalid input! Please try again," which is what is supposed to happen. The problem is, after I get that message once, I will get it for every input after that, even if the next input is valid.
Someone suggested that I should use std::cin.clear(); to clear the error-flags. I tried putting it before the getline() and it did not change the problem. Was I using this incorrectly?