我试图让这个程序工作但是在它应该读取内容字符串的部分,它只是跳过。也许是因为缓冲区中已经存在一些我不知道的东西。
struct Task {
int priority;
string content;
Task * nxtTask;
};
void addTask(Task *, int, string);
Task * newTask(int, string, Task *);
int main() {
Task * t1 = nullptr;
int choice;
do {
string content;
cin >> choice;
switch(choice) {
case 1:
break;
case 2:
cout << "Enter the priority of the task.\n>? ";
int priority;
cin >> priority;
cout << "What's the content of the task?\n>? ";
getline(cin, content);
addTask(t1, priority, content);
break;
case 3:
break;
};
} while (choice > 0);
return 0;
}
Task * newTask(int priority, string content, Task * nxtTask) {
Task * task = new Task;
task->priority = priority;
task->content = content;
task->nxtTask = nxtTask;
return task;
}
void addTask(Task * t1, int priority, string content) {
t1 = newTask(priority, content, nullptr);
return;
}
我该怎么办?
答案 0 :(得分:0)
添加 cin.clear() 在getline之前