i have problem following code. use in xcode (os x).
[removed first try of code]
how input reset std::cin? try input values can't because std::cin seems not work anymore after wrong value.
upd2 in second try use code:
for ( unsigned char = 0; < 5; i++ ) { int value = 0; std::cout << "\n>> enter \"value\": "; std::cin >> value; if ( std::cin.fail() ) { std::cin.clear(); std::cin.ignore(); std::cout << "error: it's not integer value!\n"; } else { std::cout << "the value format ok!\n"; } std::cout << "value = " << value << std::endl; }
here input 5 values in loop. everytime check on error. when set wrong value ("asdf") std::cin goes crazy , doesn't work anymore. output:
>> enter "value": 4 value format ok! value = 4 >> enter "value": 234 value format ok! value = 234 >> enter "value": asdfghjkl error: it's not integer value! value = 0 >> enter "value": error: it's not integer value! value = 0 >> enter "value": error: it's not integer value! value = 0 234 234 78 345 657 345 dsf f
you use, when condition, std::cin.fail()
happens:
std::cin.clear(); std::cin.ignore();
and continue loop, continue;
statement. std::cin.clear()
clears error flags, , sets new ones, , std::cin.ignore()
ignores them (by extracting , discarding them).
sources:
Comments
Post a Comment