In C++, I developed a simple application that requested the user to enter a number and then a string.
Surprisingly, when I ran the application, it never paused to ask for the string.
It simply ignored it.
After conducting some research on StackOverflow, I discovered that I needed to include the following line:
before the line with the string input
That addressed the problem and allowed the software to run.
My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it.
Here's the software I created:
using namespace std;
cout << "Please enter a number: " << "\n";
cin >> num;
cout << "Your number is: " << num << "\n";
cin.ignore(256, '\n'); // Why do I need this line?
cout << "Please enter your name: \n";
getline (cin, mystr);
cout << "So your name is " << mystr << "?\n";
cout << "Have a nice day. \n";