【发布时间】:2015-06-17 01:59:49
【问题描述】:
我知道,这个问题已经被处理了很多次了..但我还是不能让它工作..在这里我粘贴一些代码:
#include <sstream>
#include "header.h"
using namespace std;
using namespace header;
int main(){
string parent, child, line, node;
int choose = 0;
tree_ptr tree_inst = new tree;
cout << "*** Start ***" << endl;
while (getline(cin, line, '\n')){
istringstream line_stream(line);
line_stream >> parent;
if (parent.compare("0") == 0) break;
while (line_stream >> child) {
if (!tree_inst->insert(parent, child)) {
cout << "*** ERROR! ***" << endl;
tree_inst->visit(tree_inst->root);
cout << "*** End ***" << endl;
return -1;
}
}
}
while (true) {
cin >> choose; //<== doesn't wait for the input, just loop forever
// on the default statement of the switch...
switch (choose) {
...
}
}
}
我已经尝试插入一些 cin.sync() cin.clear() cin.ignore() ..etc.. 但没有任何改变!
【问题讨论】:
-
PS:“string”和“iostream”包含在“header.h”中..
-
你的输入是什么,如果它不是可以解析为整数的任何内容,则不会从输入缓冲区中删除文本,因此下一个循环将再次读取相同的输入。您需要 both 忽略该行的其余部分 并 清除错误。请参阅此
std::istream::ignore参考中的示例如何操作。 -
好的,我试试!无论如何,输入是一个文本文件(在 Linux 上,我使用控制台将文件通过“./myprogram
-
我刚试过:while (true) { cin.clear(); cin.ignore(std::numeric_limits<:streamsize>::max(), '\n'); cin >> 选择; switch (choose) { ... etch... 但没有任何改变 :(
-
我无法重现您的问题!你如何摆脱第一个循环?
标签: c++ cin getline io-redirection istream