why do I need the getline(cin, students[0]); on line 15 to make the program run normally? |
That's a kind of a workaround. What you really need to do is to remove the newline character '\n' which remains in the input buffer after you did
After that, you should do as a minimum,
though it is common to do something like
The first just reads and discards a single character from the input buffer, the latter will ignore up to 1000 characters, or until a newline is read.
This is a common problem,
cin >>
and
getline(cin
don't always play nicely together unless you clean up after the
cin >>
One more comment. When you use
new []
, it is good practice to have a matching
delete []
. This avoids memory leaks.
It may not matter much when the program is about to end, but properly you should have
1 2
|
delete [] students;
delete [] grade;
|
when you have finished using the dynamically allocated arrays.