相关疑难解决方法(0)

105
推荐指数
7
解决办法
28万
查看次数

当我使用getline时,为什么cout打印两次?

我正在尝试使用getline读取一串文本.出于某种原因,它会打印两次"请输入您的选择":

Please enter your selection
Please enter your selection
Run Code Online (Sandbox Code Playgroud)

如果我键入无效文本,它会再次循环,并且此后每次循环只打印一次.

while (valid == false) {    
    cout << "Please enter your selection" << endl;
    getline (cin,selection);

    // I have a function here which checks if the string is valid and sets it to true 
    // if it is valid.  This function works fine, so I have not included it here.  The while
    // look breaks correctly if the user enters valid input.
}
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么会这样?

谢谢

c++

6
推荐指数
1
解决办法
2612
查看次数

标签 统计

c++ ×2

cin ×1

io-buffering ×1