"abc" cannot be parsed as an int
, so the input will fail. The stream will remain in failed state until you have acknowledge the failure by clearing the state using the clear
member function. You never clear the failure, so the following extractions will fail before asking any input and the loop will never stop.
The language implementation is allowed to assume that any thread will eventually terminate, or make a call to a library I/O function (or do one of a few other things which this program doesn't do). Arguably, cin >> k;
may be considered to be a "call to a library I/O function", but if the implementation doesn't consider that in the case of stream in a failure state, then it may just terminate the program.
If you did enter 1 and the if
branch is entered, the behaviour of the program will be undefined because:
This:
fgets(student[i], 30, stdin);
will write outside the bounds of the student
vector.
:
fflush(stdin);
Passing an input stream to fflush
results in undefined behaviour.