C ++ cin questions

This seems odd:

int main(int argc, char* argv[]) { cout << "function main() .." << '\n'; char ch = 0; double number_value=1.1; cin >> ch; cin.putback(ch); cin >> number_value; cout << "1 .. " << " " << cin.good() << " " << number_value << '\n'; cin >> number_value; cout << "2 .. " << " " << cin.good() << " " << number_value << '\n'; return 0; } 

If I enter the following:

 7a 1 

I get the following:

function main () ..

 7a 1 1 .. 1 7 2 .. 0 0 

I understand:

 1 .. 1 7 

but why the variable number_value is 0. cin.good() shows a failure, so it would not read anything, and the value in number_value from the previous destination would remain. I expect a value of 7.

+4
source share
1 answer

What I would expect too. With compilers, it’s convenient for me, the output is as follows:

 function main() .. 7a 1 .. 1 7 2 .. 0 7 

You may have found a bug in your standard compiler library.

+2
source

Source: https://habr.com/ru/post/1304225/


All Articles