0

The following code doesn't give the second prompt to "enter message". How do I fix it?

cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
getline(cin, msg);
ANKI_YUME
  • 13
  • 3
  • possible duplicate of [cin and getline skipping input](http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input) – Alejandro Jul 12 '15 at 03:29

2 Answers2

2

try this one

cout << "Enter shifts:" << endl;
cin >> shifts;
cout << "Enter message:" << endl;
cin.ignore();
getline(cin, msg);

use cin.ignore(); before using getline anywhere.

  • Thanks! it worked. But do I always have to manually flush the buffer? – ANKI_YUME Jul 12 '15 at 03:32
  • Is `cin.ignore()` similar to `cin >> ws`? I think I'd probably use the latter in this case. – celticminstrel Jul 12 '15 at 03:53
  • @ANKI_YUME, maybe you could also opt for creating a function that wraps both calls and have it return your input by value, which may get elided by NRVO if its a larger type, or take a reference and input it directly to that, similar to what `std::getline` does. – Alejandro Jul 12 '15 at 14:15
1

when you enter shifts there is a newline which read by geline funtion. So You need to ignore that newline.

write :

cout << "Enter shifts:" << endl;
cin >> shifts;
getchar();
cout << "Enter message:" << endl;
getline(cin, msg);
Robin Halder
  • 253
  • 2
  • 14