B
bintom
I have a program to read a string (using cin.getline()) followed by a
number, 3 times in a loop. The program compiles properly. However, the
runtime behaviour of the program is not what is expected. A sample
output is also given:
int main()
{ char name[3][21], dummy[21];
int i, mark[3];
for(i=0; i<3; i++)
{ cout << "Enter name: "; cin.getline(name, 21);
cout << "Enter mark: "; cin >> mark;
// cin.getline(dummy, 21);
}
for(i=0; i<3; i++)
cout << name << '\t' << mark << '\n';
}
A typical run would produce the following results:
Enter name: ABC
Enter mark: 56
Enter name: Enter mark: 78
Enter name: Enter mark: 27
ABC 56
78
27
After much heart-burn, I was able to get the expected output, provided
a dummy string is accepted (see the commented line in the program).
Can somebody tell me why this problem arises?
Thanks in advance,
Bintom
number, 3 times in a loop. The program compiles properly. However, the
runtime behaviour of the program is not what is expected. A sample
output is also given:
int main()
{ char name[3][21], dummy[21];
int i, mark[3];
for(i=0; i<3; i++)
{ cout << "Enter name: "; cin.getline(name, 21);
cout << "Enter mark: "; cin >> mark;
// cin.getline(dummy, 21);
}
for(i=0; i<3; i++)
cout << name << '\t' << mark << '\n';
}
A typical run would produce the following results:
Enter name: ABC
Enter mark: 56
Enter name: Enter mark: 78
Enter name: Enter mark: 27
ABC 56
78
27
After much heart-burn, I was able to get the expected output, provided
a dummy string is accepted (see the commented line in the program).
Can somebody tell me why this problem arises?
Thanks in advance,
Bintom