当我输入 2 个带有空格的整数输入时,它会跳转到第二个 cin 函数

问题描述 投票:0回答:1

因此,当我尝试输入程序所需的输入时,我希望用户一一输入。当我同时输入它们时,它们之间有一个空格,例如:

5 10

直接进入第二个

cin
的结尾。

#include <iostream>

using namespace std;

int main() {
    int asd1, asd2;

    cout << "Enter the first one: ";
    cin >> asd1;
    cout << endl;
    if (asd1 == 5) {
        cout << "First one is 5" << endl;
    }

    cout << "Enter the second one: ";
    cin >> asd2;
    cout << endl;
    if (asd2 == 10) {
        cout << "Second one is 10" << endl;
    }
}

当我同时输入两个输入时,它会以一种丑陋的方式输出,这就是我问这个的原因。

输出:

Enter the first one: 5 10

First one is 5
Enter the second one:         <<<< Here it doesn't cin.
Second one is 10

我尝试使用

cin.get()
但并没有真正起作用。

c++ user-input cin
1个回答
0
投票

第一个读取语句:

cin >> asd1
从缓冲区中读取
5
,但将
10
留在该缓冲区中。第二个读取语句:
cin >> asd2
能够立即从缓冲区读取
10
,无需任何进一步的操作。

© www.soinside.com 2019 - 2024. All rights reserved.