跳过获取线

Skiping the getline

本文关键字:获取      更新时间:2023-10-16

我正在尝试使该程序工作,但在它应该读取内容字符串的部分,它只是跳过。也许是因为缓冲区中已经有一些我不知道的东西。

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;
}

我该怎么办?

add cin.clear((在上线之前