简体   繁体   中英

unexpected behavior when using cin twice

I have the following c++ code in a console program:

float var1, var2;

cin >> var1;
cin >> var2;

When I am about to enter the second variable and use the right arrow key, the input of the first variable starts to appear on the screen. If I hit enter, the second variable will take the value that appeared on the screen. Do I need to reset the cin buffer? I tried to make use of:

cin.clear();

and:

cin.ignore();

but the behavior stays.

This hast nothing todo with c++. What you're experiencing is a feature of cmd.exe where the use of the right arrow key gets your previously typed line (character for character) if the right arrow is pressed as first keystroke on a new line.

Nothing to do. Just know compiler behavior. cin will take space(s) or next lines (\\n) separated value. Now up and down arrow navigate down the history so you get previous input in console. You can test from command prompt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM