54

I have a problem using getline method to get a message that user types, I'm using something like:

string messageVar;
cout << "Type your message: ";
getline(cin, messageVar);

However, it's not stopping to get the output value, what's wrong with this?

5
  • 4
    There is nothing wrong with the three lines you show. You might want to edit your question to include some more context? Sep 13, 2013 at 12:43
  • 2
  • Is this all of your code? Do you do anything with messageVar afterwards?
    – Paddyd
    Sep 13, 2013 at 12:44
  • 1
    Most likely there's a '\n' left in the stream from previous input.
    – jrok
    Sep 13, 2013 at 12:47
  • Yes, I register the value in a DB, I was using cin >> messageVar, but that's just catch "word by word" of sentence
    – MGE
    Sep 13, 2013 at 12:48

7 Answers 7

104

If you're using getline() after cin >> something, you need to flush the newline character out of the buffer in between. You can do it by using cin.ignore().

It would be something like this:

string messageVar;
cout << "Type your message: ";
cin.ignore(); 
getline(cin, messageVar);

This happens because the >> operator leaves a newline \n character in the input buffer. This may become a problem when you do unformatted input, like getline(), which reads input until a newline character is found. This happening, it will stop reading immediately, because of that \n that was left hanging there in your previous operation.

2
  • What is an "unformatted input" and how is it related to getline()?
    – Minh Tran
    Sep 4, 2016 at 1:03
  • Also, is there an alternative way to achieve same behavior as the code above without using the cin,cin.ignore(), and getline() functions?
    – Minh Tran
    Sep 4, 2016 at 1:10
6

If you only have a single newline in the input, just doing

std::cin.ignore();

will work fine. It reads and discards the next character from the input.

But if you have anything else still in the input, besides the newline (for example, you read one word but the user entered two words), then you have to do

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

See e.g. this reference of the ignore function.

To be even more safe, do the second alternative above in a loop until gcount returns zero.

3

I had similar problems. The one downside is that with cin.ignore(), you have to press enter 1 more time, which messes with the program.

2
int main(){
.... example with file
     //input is a file
    if(input.is_open()){
        cin.ignore(1,'\n'); //it ignores everything after new line
        cin.getline(buffer,255); // save it in buffer
        input<<buffer; //save it in input(it's a file)
        input.close();
    }
}
1

I know I'm late but I hope this is useful. Logic is for taking one line at a time if the user wants to enter many lines

int main() 
{ 
int t;                    // no of lines user wants to enter
cin>>t;
string str;
cin.ignore();            // for clearing newline in cin
while(t--)
{
    getline(cin,str);    // accepting one line, getline is teminated when newline is found 
    cout<<str<<endl; 
}
return 0; 
} 

input :

3

Government collage Berhampore

Serampore textile collage

Berhampore Serampore

output :

Government collage Berhampore

Serampore textile collage

Berhampore Serampore

-1

i think you are not pausing the program before it ended so the output you are putting after getting the inpus is not seeing on the screen right?

do:

getchar();

before the end of the program

1
  • I assume this answer is for Windows, which usually terminates the console upon return. I believe it does not apply to the question?
    – GCon
    Dec 31, 2014 at 13:04
-1

The code is correct. The problem must lie somewhere else. Try the minimalistic example from the std::getline documentation.

main ()
{
    std::string name;

    std::cout << "Please, enter your full name: ";
    std::getline (std::cin,name);
    std::cout << "Hello, " << name << "!\n";

    return 0;
}
3
  • I tried your code, the input is inserted into the name object, but I cannot use it to print the hello name. Any ideas? Aug 17, 2014 at 11:34
  • Sure, you can. I just tried it out and it works just fine. What exactly does not work for you?
    – Konstantin
    Feb 9, 2017 at 10:44
  • No, this misses the point. The problem, as described by people above is that when the getline() follows another input using cin there is still a newline in the input buffer. The minimal example given has no input preceding the getline() and so of course it doesn't reproduce the issue that the original poster is asking about. May 16, 2019 at 13:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.