Michel de Man
Michel de Man

Reputation: 223

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.

Upvotes: 0

Views: 136

Answers (2)

pcbabu
pcbabu

Reputation: 2329

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.

Upvotes: 0

René Nyffenegger
René Nyffenegger

Reputation: 40489

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.

Upvotes: 2

Related Questions