Formatted input of an numeric values succeeds is there is a prefix which contains digits. The streams won't easily allow you to change that behavior (you can change that behavior by using a specialized std::num_get<char>
facet but I won't classify that as simple). The easiest approach is probably to check whether the next character (if there is one) happens to be a space (and don't use fail()
; you should just use the conversion to a boolean value):
if (std::cin && (std::isspace(std::cin.peek()) || std::cin.eof())) {
...
}
Also, don't compare boolean values in conditions to true
or false
: the result doesn't become more boolean by doing so.