【问题标题】:While loop bug with cout and cin in c++? or is itc++ 中 cout 和 cin 的循环错误?或者是
【发布时间】:2020-05-06 19:17:29
【问题描述】:

所以基本上我是在制作一个聊天应用程序,然后我遇到了一个我无法修复的错误。为了不泄露我的源代码,这里有一个例子:

#include<iostream>
using namespace std;

int main()
{
    while (true) {
        string lol;
        cout << "you say >> ";
        cin >> lol;
    }
    return 0;
}

所以错误是当您在 cin 中键入空格时 你输入:“嗨,哈哈”它会打印“你说>>” 两次和更多的空间你 把更多的“你说>>”重复我真的不明白为什么会这样。有人能帮我吗?

【问题讨论】:

标签: c++ while-loop cout


【解决方案1】:

这不是错误,那是the way it works

std::stringoperator&gt;&gt;() 以空格分隔,因此您一次有效地得到一个单词。

如果您想阅读整行,请使用std::getline()

【讨论】:

  • std::getline() 的语法是什么?
  • getline 是一个函数,它与任何其他函数调用的语法相同。
猜你喜欢
  • 1970-01-01
  • 2016-07-08
  • 2015-11-13
  • 1970-01-01
  • 2011-12-07
  • 2011-01-16
  • 2016-06-30
  • 2013-02-01
  • 1970-01-01
相关资源
最近更新 更多