7

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?

ForeverLearning
  • 5,306
  • 3
  • 23
  • 33
  • 4
    post your code, to get an answer which solves your problem rather than speculative answers. – Alok Save Jul 25 '11 at 16:11
  • Is there a `'\n'` sitting in the input buffer from before, perhaps? – hammar Jul 25 '11 at 16:14
  • 1
    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). – Praetorian Jul 25 '11 at 16:21
  • 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. – Thomas Matthews Jul 25 '11 at 17:04
  • 2
    Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – user202729 Jan 31 '21 at 03:01

3 Answers3

6

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.

Shamim Hafiz
  • 19,616
  • 36
  • 104
  • 164
  • 6
    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.). – Sander De Dycker Jul 25 '11 at 16:17
  • 2
    An alternative way to get rid of the extra '\n' rather than a dummy getline call would be something like `cin.ignore(1, '\n');`. – Sven Jul 25 '11 at 16:19
  • 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. – Alok Save Jul 25 '11 at 16:21
  • 5
    @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. – Sven Jul 25 '11 at 16:24
  • 2
    Bingo! Mr. Hafiz, your ESP powers are terrifying. Thanks! – ForeverLearning Jul 25 '11 at 16:29
  • 3
    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. – Benjamin Lindley Jul 25 '11 at 16:30
  • @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. – ForeverLearning Jul 25 '11 at 16:31
  • @Sven: All the best with your psychic debugging. I will stick to answering a Q only knowing enough information to answer a Q. – Alok Save Jul 25 '11 at 16:31
  • This problem bit me very hard. Thankfully the dummy call to getline() came to the rescue. – peterchaula Oct 07 '20 at 04:40
2

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.

nn.
  • 41
  • 1
  • 7
0

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

Now it works:

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