Why does a std::getline call on std::cin not wait for user input? [duplicate]
Asked Answered
C

4

8

Is there any good reason why:

std::string input;
std::getline(std::cin, input);

the getline call won't wait for user input? Is the state of cin messed up somehow?

Cismontane answered 25/7, 2011 at 16:8 Comment(5)
post your code, to get an answer which solves your problem rather than speculative answers.Theola
Is there a '\n' sitting in the input buffer from before, perhaps?Banjermasin
Of course there's a good reason why it's happening - you've messed up somehow :-) Post more code that demonstrates the problem (as others have mentioned).Abaft
The cin is allowed to be buffered. Many implementations require a newline in order to flush the input buffer and return the data to the calling program.Convulsion
Does this answer your question? Why does std::getline() skip input after a formatted extraction?Clatter
E
7

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.

Ediva answered 25/7, 2011 at 16:14 Comment(9)
A better way to resolve this, is to always use std::getline for reading any input, and never use std::cin >> style input. For reading an int, you can read the line in a string buffer, and then parse that buffer to get the int out of it (using std::stringstream eg.).Orle
An alternative way to get rid of the extra '\n' rather than a dummy getline call would be something like cin.ignore(1, '\n');.Foretooth
Are you guys discussing a problem, which is probably NOT what the OP is facing? Unless the OP posts a source code, All speculations are futile.Theola
@Als: it's called psychic debugging, i.e. having seen a problem before and therefore knowing its probable cause. :) Sure, it might be something else, but there's no harm in trying to answer based on available info and one's own experience.Foretooth
Bingo! Mr. Hafiz, your ESP powers are terrifying. Thanks!Cismontane
I can pretty much guarantee this is what the OP is facing. I've seen this problem hundreds of times, and it has always been because of a call to operator>> preceding a call to getline, leaving a '\n' in the input buffer.Tipi
@Sven: Thanks very much. Reading command-line inputs isn't something I have done much. So I ran into some basic newbie issues. Thanks for sorting it out.Cismontane
@Sven: All the best with your psychic debugging. I will stick to answering a Q only knowing enough information to answer a Q.Theola
This problem bit me very hard. Thankfully the dummy call to getline() came to the rescue.Agosto
R
1

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.

Ronn answered 25/7, 2011 at 16:22 Comment(0)
W
1

This code does not work:

#include <iostream>
#include <string>

int main()
{
int nr;
std::cout << "Number: ";
std::cin >> nr;

std::string  input;
std::cout << "Write something: ";
getline(std::cin, input);
std::cout << "You input is: " << input << std::endl;

return 0;
}

This does work:

#include <iostream>
#include <string>

int main()
{
int nr;
std::cout << "Number: ";
std::cin >> nr;

std::string x;
std::getline(std::cin,x);

std::string  input;
std::cout << "Write something: ";
getline(std::cin, input);
std::cout << "You input is: " << input << std::endl;

return 0;
}
Woolpack answered 31/8, 2019 at 16:53 Comment(1)
According to authoritative sources (in particular, my opinion 😉), this is an acceptable way to solve this well-known issue.Sussex
J
0

this occurred cause before std::getline(std::cin, input); there's newline char (/n). The getline reads until it encounters /n. Therefore it will read an empty string and return null without waiting for the users input.

To counter this we use an dummy getline, or cin.ignore(1, /n);

Judicatory answered 25/9, 2023 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.