I have to write a method which asks a question to the user and gives couple options to choose from. User input must include numbers only. So if user enters 32a programs shows an error loops back to the question again. How can I check the user input for letters? Here is the part of my code for reading in the user input.
1 2 3 4 5
string input;
int choice;
cout<<userPrompt;
getline(cin,input);
choice = atoi(input.c_str());
bool is_number(const string& s){
for(int i = 0; i < s.length(); i++)//for each char in string,
if(! (s[i] >= '0' && s[i] <= '9' || s[i] == ' ') ) returnfalse;
//if s[i] is between '0' and '9' of if it's a whitespace (there may be some before and after
// the number) it's ok. otherwise it isn't a number.
returntrue;
}