我有一个应用程序正在等待用户输入以发出命令,当它收到命令时,大量信息会打印到屏幕上。当第二次循环等待std::getline
时,应用程序崩溃。我认为这是因为缓冲区已满。如果我对字符串进行硬编码而不是从std::cin
中读取,则不会崩溃。我如何在第二次读取之前刷新缓冲区,我使用了以下内容,但是std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
#include <iostream>
#include <string>
int main ()
{
while(true){
std::string input{};
// if string is hardcoded app works so issue is with std::cin
// std::string input{"command_one"};
if (!std::getline(std::cin, input))
{
continue;
}
// app does work and prints alot to screen
}
}