When an error occurs when reading from a stream, an error flag gets set and no more reading is possible until you clear the error flags.
That's why you get an infinite loop.
Instead use :
cin.clear(); // clears the error flags
// this line discards all the input waiting in the stream
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');