I have answered this question (poorly titled "vector v.s infinite loop"), and the OP incorporated the proposed solution into their question by editing it. This made the answer a bit of useless, because the originally posted codes problems were addressed there.
While it looks to me the original problem is solved (actually check and manipulate the std::istream
state as needed), the original code better fit's for Q&A format. Also the title is pretty poor, to describe origin of the problem.
I'd appreciate proposals, how to improve the question for future research, with respect to the accepted answer.
The title should be something like "Why does this code end up in an infinite loop, reading from std::cin" IMHO (better proposals appreciated).
I could have used brute force1, rolling back to the original post, and editing afterwards (included giving the question a better title), but I'm not so sure, that's why I'm asking.
I have read "Is it wrong for OP to incorporate suggested changes into their posted code?", but I'm asking from the editor's point of view.
1) I have used brute force to edit the question meanwhile.