I m using MinGW for running g++ compiler on windows. Whenever I run the following code, it the compiler gives strange results.
Code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int n;
string a;
cin>>n;
getline(cin,a);
cout<<a;
return 0;
}
No problem occurs when I compile the code. But as soon as I run the code and give input for n, it never asks for the input of a and ends. I m using MinGW 5.1.6, is there any problem with that or is there any problem with my code?
The problem is in your code. In a nutshell, the newline you type to commit the number for
nis still stored in the input buffer as it is not numerical input, so is not consumed byn. Thegetlinefunction then absorbs the newline and completes.