You need to check if extraction of the int
succeeded, and if it didn't, possibly clear()
the fail state of the stream. cin.eof()
and cin.bad()
can be used to check if the stream is in an unrecoverable state or you can combine them both with std::ios::badbit | std::ios::eofbit
and check if std::cin.rdstate()
has any of those bits set.
Checking if extraction succeeds is usually done by checking the stream's state in boolean context, like so:
if(!std::cin) { // or if(not std::cin) "not" is an alternative to "!"
// the stream is in a failed state
}
When you do formatted extraction from a stream with std::cin >> small
, operator>>
will return a reference to the stream, which makes extracting and then checking the state easy:
if(not(std::cin >> small)) { /* extraction failed */ }
Full example:
#include <iostream>
#include <limits> // std::numeric_limits
int main() {
int small;
while(std::cout << "Small Pizza Price: " && not(std::cin >> small)) {
// if(std::cin.eof() || std::cin.bad()) { or:
if(std::cin.rdstate() & (std::ios::badbit | std::ios::eofbit)) {
std::cout << "aborted\n";
return 1;
}
std::cout << "please enter a valid price\n";
std::cin.clear(); // clear the fail state
// remove any lingering characters in the istream:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
// small read successfully
}