trouble with getline

I am new to programming and I am trying to do a problem out of a book. The problem is relatively simple but I encountered something along the way that I would like an explanation to. In the program, it takes in two different numbers and a string. I set it up to use cin for the two numbers and then use getline to take in the string. However, if I make it ask for the numbers before the string, the program doesn't wait for input to the string and the program just ends. If i ask for the string and then the numbers, it works just fine. Is there some rule that says you cannot use cin before getline? Thank you in advance. I'll include the part of the code that I'm having trouble with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string operation;
    double first;
    double second;

    //cout << "Enter an arithmetic operation (addition, subtraction, multiplication, division): " << "\n";
    //getline( cin, operation, '\n' );

    cout << "Enter your first number: ";
    cin >> first;

    cout << "Enter your second number: ";
    cin >> second;

    cout << "Enter an arithmetic operation (addition, subtraction, multiplication, division): " << "\n";
    getline( cin, operation, '\n' );
The problem with mixing cin and getline, is that cin leaves in the newline character in the buffer generally.
This might not mean anything to you right now, but basically there is a character in the buffer that getline sees, which happens to the value that getline terminates with.

To fix this, you need to make a call to either cin.sync() or cin.ignore()
Topic archived. No new replies allowed.