如何循环阅读控制台输入? [英] How Do I read Console Input in a loop?
本文介绍了如何循环阅读控制台输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用G ++ 4.7.2编译此代码,并且很难连续读取来自控制台的输入.代码看起来像这样.
I am compiling this code with G++ 4.7.2, and I am having a hard time continually reading input from the console. The code looks like this.
//Ensure the CIN buffer is clear & empty from previous reads
std::cin.clear();
std::cin.ignore(INT_MAX, '\r');
std::string uInpt;
std::getline(std::cin, uInpt);
while (uInpt.compare("quit") != 0)
{
std::cin.clear();
std::cin.ignore(INT_MAX, '\r');
std::cout << "You entered: " << uInpt << std::endl;
std::getline(std::cin, uInpt);
}
我遇到的问题是:
1.似乎根本没有清除CIN缓冲区,原因是因为,我只能在循环开始之前输入1个字符,然后似乎已经有一个回车符.
2.在循环中,我似乎从未得到过读取提示.
有想法吗?
The problem I am having is:
1. It seems like the CIN buffer is not cleared at all, the reason being is because, I can only ever input 1 character before the loop starts, Then it seems like there is a carriage return already in place.
2. I never seem to get prompted for the read while in my loop.
Thoughts?
推荐答案
是的.问题出在将代码放在派生进程中并让父进程消失.
Yea....That doesn''t really work. The problem came from having this code in a fork''d process and letting the parent process go away.
这篇关于如何循环阅读控制台输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文