I tried to input data with gets()
function, but whenever program execution get to the the lien with the gets
, it ignores it.
When I use gets()
without previous data input, it runs properly. But when I use it after data input the problem happens.
Here's the code where it is used after previous data input (so in execution I can't input data to string):
int main() {
char str[255];
int a = 0;
cin >> a;
if(a == 1) {
gets(str);
cout << "\n" << str << endl;
}
}
How could I fix this?
NB: the same happens with cin.getline
After
when you input
a
and enter, there is also a\n
character left bycin
, therefore, when you usecin.getline()
orgets(str)
it will read that newline character.try the following:
You'd better use C++ way of reading input: