我知道这个问题已经被讨论很多次了,但是无论如何我都不能让它工作。这里我贴上一些代码:
#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()等,但什么都没有改变!
std::istream::ignore
中的示例以了解如何实现。 - Some programmer dude