0

I have a big problem with cin.ignore() for days. This is my code excerpt, not all

string ID;
cin.ignore(100,'\n');
cout << "Enter User ID: " << endl;
getline(cin, ID);

The problem is, when I use cin.ignore() like this, I have to enter the string ID twice to continue but when i don't use it, I can not enter the string anymore. I completely dont know why, hope someone can explain it!

  • What do you think `cin.ignore(100,'\n')` does? Please post a [mcve]. – molbdnilo Jul 09 '20 at 07:55
  • As per your code snippet, cin.ignore() will ignore 100 characters until \n(Enter) is pressed. So you probably press enter before inputting ID. ``` int main() { string ID; cin.ignore(100, '\n'); cin >> ID; cout << "string:" << ID; return 0; } ``` – preethi selvaraju Jul 09 '20 at 08:13
  • 2
    I'm betting you can find the solution here https://stackoverflow.com/q/21567291/6865932 – anastaciu Jul 09 '20 at 08:23
  • So it seems highly relevant that you show the previous code. Taken in isolation there is no need for ignore in the code above. But you say your code does not work without it either. So we need to see more code. Try to construct a [mre]. – john Jul 09 '20 at 08:56

0 Answers0