is judged based on the status of ss and failbit of the badbit object.
i has a value of 5. Why is this?
Because after reading the last number 4, ss is still in a normal state, and an error will occur only if you try to read it again (i.e., the 5th loop). You can try entering a blank line and the value of i is 1.
Common practices are:
int i = 0;
getline(cin, s);
ss.clear(); ss.str(s);
while(ss >> H[i++]) { }
is judged based on the status of
ss
andfailbit
of thebadbit
object.Because after reading the last number 4,
ss
is still in a normal state, and an error will occur only if you try to read it again (i.e., the 5th loop). You can try entering a blank line and the value ofi
is 1.Common practices are:
Reference
std::basic_ios::fail
std::basic_stringstream