#include <iostream>
int main()
{
using namespace std;
char a[50];
int y;
cin >> y;
cin.getline(a, 40);
cout << "y= " << y << "\n"<< "a=" << a << endl;
}
When run, I input 45, enter. Then the output is
y= 45
a=
I haven't have a chance to input a
. But when I use this code:
#include <iostream>
int main()
{
using namespace std;
char a[50];
int y;
cin.getline(a, 40);
cin >> y;
cout << "y= " << y << "\n" << "a=" << a << endl;
}
I run it with 45, enter, fish, enter. Its output
y= 45
a=fish
So, can anybody explain why cin.getline(a,40)
in first code not work, but it work in second code? I got confused.