简体   繁体   English

使用cin两次时的意外行为

[英]unexpected behavior when using cin twice

I have the following c++ code in a console program: 我在控制台程序中有以下c ++代码:

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. 如果我按Enter键,第二个变量将采用屏幕上显示的值。 Do I need to reset the cin buffer? 我需要重置cin缓冲区吗? I tried to make use of: 我试图利用:

cin.clear();

and: 和:

cin.ignore();

but the behavior stays. 但行为仍然存在。

This hast nothing todo with c++. 这与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. 您遇到的是cmd.exe一项功能, 如果在新行上按下右箭头作为第一次按键,则右箭头键的使用将获得您之前键入的行(字符字符)。

Nothing to do. 没事做。 Just know compiler behavior. 只知道编译器行为。 cin will take space(s) or next lines (\\n) separated value. cin将占用空格或下一行(\\ n)分隔值。 Now up and down arrow navigate down the history so you get previous input in console. 现在向上和向下箭头向下导航历史记录,以便您在控制台中获得先前的输入。 You can test from command prompt. 您可以从命令提示符进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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