std::getline on std::cin

Most likely you are trying to read a string after reading some other data, say an int.

consider the input:

11
is a prime

if you use the following code:

std::cin>>number;
std::getline(std::cin,input)

the getline will only read the newline after 11 and hence you will get the impression that it's not waiting for user input.

The way to resolve this is to use a dummy getline to consume the new line after the number.


I have tested the following code and it worked ok.

#include <iostream>
using namespace std;
int main()
{
    string  input;
    getline(cin, input);
    cout << "You input is: " << input << endl;
    return 0;
}

I guess in your program that you might already have something in you input buffer.

Tags:

C++

Cin

Getline