By MGE


2013-09-13 12:40:28 8 Comments

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?

6 comments

@logankilpatrick 2017-02-27 00:40:04

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.

@Natan Streppel 2013-09-13 12:48:00

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.

@Minh Tran 2016-09-04 01:03:06

What is an "unformatted input" and how is it related to getline()?

@Minh Tran 2016-09-04 01:10:49

Also, is there an alternative way to achieve same behavior as the code above without using the cin,cin.ignore(), and getline() functions?

@Antonija 2014-08-16 11:45:33

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

@Some programmer dude 2013-09-13 12:53:30

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.

@Konstantin 2013-09-13 12:47:52

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

@user3165438 2014-08-17 11:34:43

I tried your code, the input is inserted into the name object, but I cannot use it to print the hello name. Any ideas?

@Konstantin 2017-02-09 10:44:14

Sure, you can. I just tried it out and it works just fine. What exactly does not work for you?

@gleedadswell 2019-05-16 13:57:34

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.

@No Idea For Name 2013-09-13 12:47:51

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

@GCon 2014-12-31 13:04:26

I assume this answer is for Windows, which usually terminates the console upon return. I believe it does not apply to the question?

Related Questions

Sponsored Content

41 Answered Questions

[SOLVED] Get the length of a String

  • 2014-06-04 12:40:01
  • Scott Walter
  • 450715 View
  • 762 Score
  • 41 Answer
  • Tags:   swift string

37 Answered Questions

18 Answered Questions

[SOLVED] Convert bytes to a string

22 Answered Questions

[SOLVED] What is the "-->" operator in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2223192 View
  • 4247 Score
  • 1 Answer
  • Tags:   c++ c++-faq

23 Answered Questions

[SOLVED] Pythonic way to create a long multi-line string

18 Answered Questions

[SOLVED] Why should C++ programmers minimize use of 'new'?

23 Answered Questions

[SOLVED] How to check if the string is empty?

10 Answered Questions

7 Answered Questions

Sponsored Content