1

I've got following code:

system("CLS");
string title;
string content;
cout << "Get title." << endl;
getline(cin,title);
cout << "Get content." << endl;
getline(cin,content);

The problem is - application is not asking about tittle, I've got Get title, get content and then waiting for user input, it's not waiting for user input after get title.bDo I have to add any break or smth?
Or maybe, that isn't the best idea to read whole text line from user input?

erkses
  • 21
  • 1
  • 2
  • Why don't you just write `cin >> title` easily? – Tamer Shlash Jun 16 '11 at 21:21
  • 1
    @Mr. TAMER If the title has a space in it, doing the `cin >>` will only get the characters up to a space. – jonsca Jun 16 '11 at 21:23
  • 3
    Hello, erkses, welcome to Stack Overflow. Thank you for posting code so that we can see what you are talking about. In future, when posting code, please consider reducing your program to the minimal **complete** example that demonstrates your problem. In this case, I fear that you have excluded information critical to answering your question. For more information about how and why to post complete programs, see http://sscce.org. – Robᵩ Jun 16 '11 at 21:26

3 Answers3

3

If you have a cin >> something; call prior to your system() call.

For example, taking input into an integer. When cin >> myintvar; (or similar) then the integer is placed in myintvar and the '\n' gets sent along in the stream. The getline picks the \n up as indicative of the end of a line of input, so it is effectively "skipped".

Either change the cin >> to a getline() or call cin.ignore() to grab the '\n'(or better, call a cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n' ); to flush the input buffer-- but be sure you're not throwing away valuable input in the process).

jonsca
  • 10,218
  • 26
  • 54
  • 62
2

I'd bet that you have something like a menu for selecting options ( as a numeric type ) and after that you try to read the lines.

This happens because after std::cin read some value the remaining '\n' was not processed yet, the solution would be to include #include <limits> and then put std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); before your getline(cin,title);

lccarrasco
  • 2,031
  • 1
  • 17
  • 20
0

It is because when you use getline() it ignores the newline at the end of the line and feeds it into the input queue, so when your getline function is called for next time it encounters the newline character discarded by the previous getline() and so it considers that as end of your input string. So thats why it doesn't take any input from you. You could use something like this

getline(cin,title);
cin.get();

hope this works.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Naphstor
  • 2,356
  • 7
  • 35
  • 53