You're not checking stream status. The std::cin
stream extraction operator (operator>>
) can fail. When it does, the stream is marked as "bad" (failbit
, badbit
, or eofbit
are set). Once "bad", all subsequent stream extractions on that stream will fail unless you clear the status.
Learn to be a paranoid programmer. Always check status of those formatted input operations. You could, for example throw an exception, or print an error message and exit. The one thing you shouldn't do is to simply assume that it worked.