I was going through some code in C++ and I came across an unexpected behavior.
using namespace std;
const int ArSize = 20;
cout << "Enter the name: ";
cin >> name;
cout << "Enter the dessert: ";
cout << endl << endl;
cout << name << dessert;
The above code gives the following output
Enter the name: Himansh
Enter the dessert:
As you can see, the above code doesn't even gives us a chance to input the name of dessert. As per my knowledge, when we type "Himansh" and press enter, the above cin puts a newline character in input buffer, and when the program comes across cin.getline(), it passes newline character to it, and cin.getline() replaces this newline character to null character and discards the newline character.
Now, try replacing
Now, this code works perfectly.
Now according to my knowledge , this process should go like this.
User types "Himansh" , press enter, the cin puts newline character in input buffer.
Now when cin comes across this newline character, AFAIK, the cin should terminate once it encounters newline. So why is it still accepting user input???
Thanks for reading this long description of my doubt.
Waiting for your reply.........