It's hardly a strange problem since everybody sees it when learning C or C++. getline reads characters until a newline character is encountered, but cin>> leaves newlines on the stream. So a cin>> followed by a getline will typically cause problems.
>As always, I appreciate your help
Apparently not enough to use code tags.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>the cin.getline is mentioned in two C++ books I have as a way to enter in a string (where you can't with just "cin".
You can enter a string with cin's operator>>, just not one with whitespace by default. And that has no bearing on the problem at hand. You use cin.getline to read a string, then you use cin>> to read a number. That leaves a newline in the stream and the next call to cin.getline appears to be skipped because it immediately sees the termination character.
>Also, what do you mean by "code tags"?
Code tags . Use them when posting code.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>What would you use to enter text into an array (such as a name)?
cin.getline, of course. You're missing the point and focusing on your strings while the problem is here:
cin>>your_student.grade_1cr[i];
Add this, since you seem incapable of solving a problem after being told what it is:
cin>>your_student.grade_1cr[i];
cin.ignore(numeric_limits<streamsize>::max(), '\n');
And don't forget to include .
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Another way to purge all those wobbly '\n' ...
// clear the stream, purge any \n
while (cin.get() != '\n')
;
Goofy? Yes, these pitfalls make C++ one of the most difficult languages to learn. Kind of job security in the end.
vegaseat
DaniWeb's Hypocrite
5,988 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
>Another way to purge all those wobbly '\n' ...
That example is incompete, which is why cin.ignore is a superior alternative. It forces you to provide all of the requisite information whereas with the loop approach it's easy to miss a very important test for end-of-file. The correct loop would be:
while (std::cin.get(ch) && ch != '\n')
;
Or using the C-style alternative:
while ((ch = std::cin.get()) != EOF && ch != '\n')
;
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Another way to purge all those wobbly '\n' ...
That example is incompete, which is why cin.ignore is a superior alternative. It forces you to provide all of the requisite information whereas with the loop approach it's easy to miss a very important test for end-of-file. The correct loop would be:
while (std::cin.get(ch) && ch != '\n')
;
Or using the C-style alternative:
while ((ch = std::cin.get()) != EOF && ch != '\n')
;
Thanks Narue, for pointing this out! None of us wants to be incompete! Actually, the word has a ring to it, like "incomplete incompetence".
vegaseat
DaniWeb's Hypocrite
5,988 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417