1

I'm trying to make this program work but in the part where it is supposed to read the content string, it just skips. Maybe because there is already something in the buffer which I don't know of.

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

What should I do?

Bo Persson
  • 86,087
  • 31
  • 138
  • 198
Dudi
  • 11
  • 2
  • 2
    The `Enter` key you press to end the input for `priority` is put into the input buffer as a newline, and will be read as an empty line by `getline`. – Some programmer dude Mar 19 '18 at 09:42

1 Answers1

0

add cin.clear() before getline

MO1988
  • 57
  • 6