Get input.
Verify input.
If input is a number, break
Else print "Enter a number, please" and reiterate with continue statement.
Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
do {
std::cout << "Enter a number: ";
getline(std::cin, str);
std::cout << "\n";
n = atof(str); // n is an integer, str is an std::string. If str can be converted to a number successfully, then you can use n
if (n) { // Check str is a number
break;
} else {
std::cout << "\t";
continue;
}
} while (true);
1. atof is not standard C++, it's deprecated, it's dangerous, it's just not a good choice.
2. even if you were to use one of those functions, it should be atoi since you specified an int.
3. again, even if you were to use atof or atoi, they cannot accept a string as a parameter. These are C functions, afterall. The correct parameter would be str.c_str()
4. your code would claim that the number 0 is not a number.
5. this code would say that a combination of numbers and letters is a valid number (if the first character was a number)
6. this may just be a style choice, I guess, but infinite loops like that are infuriating.
Sorry, I wasn't thinking.
I keep on putting atof() instead of atio() by accident too :l
Anyway, the code is bad but the principle is the same I guess.
But Zaita's post is definitely better.