char ch1,ch2;
cout << "Character to store in ch1:" << endl;
cin.get(ch1);
cout << "Character to store in ch2:" << endl; //Doesn't accept any input
cin.get(ch2);
cout << "\nCharacter stored in ch1:" << endl;
cout << ch1 << endl;
cout << "Character stored in ch2:" << endl;
cout << ch2 << endl;
Output:
Character to store in ch1:
a
Character to store in ch2:
Character stored in ch1:
a
Character stored in ch2:
Please note that if I replace cin.get(ch1) and cin.get(ch2) with cin >> ch1 and cin >> ch2, the program works fine.But can someone please explain the problem in using cin.get()?
I think the newline ('enter' after typing in ch1) remains in the buffer and is automatically read into ch2.
Adding cin.ignore() after the first get fixes it for me, but I'm guessing you'll need more effort for more advanced issues.
Actually I'm just curious about this thing. Thanks for the explanation. I believe that's exactly what's happening.
However would you please explain what cin.get() and cin.ignore() actually does?