Today i started working on a quite big project and found this problem I've never found. I always used to reset the cin stream with the usage in sequence of sync() and clear(). But after i changed my compiler (a month ago i switched from standalone minGW to minGW through msys2) to have filesystem included in the std library, it seems this combination doesn't work anymore. Now I'm asking 2 different questions:
- Does anyone knows why exactly? (I'm using gcc 11.2.0, just checked on my CMD)
- How can I solve this problem?
Here's the example code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int number;
string s;
cout << "insert a number" << endl;
cin >> number;
cin.sync();
cin.clear();
cout << "Now insert a string" << endl;
getline(cin, s);
cout << "This is your number: " << number << endl;
cout << "This is your string: " << s << endl;
}
And here's the terminal:
insert a number
8
Now insert a string
This is your number: 8
This is your string:
Obviously as you can see it doesn't wait for my input and i think it gets my enter as a char for the string. That's why it prints what seems to be nothing (it's simply '\n')