cout << "Enter Numbers" << '\n';
while(cin >> num)
cout << num << " was added to the Vector" << '\n';
The while loop is expected to terminate when the user provides an Invalid Input. But this while loop behaves unexpectedly when the user provides a 'Newline' input (by pressing Enter) and becomes an infinite loop. How can I prevent this from happening? Also I've heard that expecting invalid inputs isn't good code design. Is this true? If yes, then how can I solve my question without expecting Invalid Inputs?
Because we are entering an unknown number of inputs, the loop has to terminate at some point when the user is finished with his inputs. That is what I want. But entering a newline in this case causes the while loop to behave unexpectedly.
But just pressing enter will not cause the stream to enter an error state. To exit this loop you will need to enter some invalid value or perhaps by signaling eof() by using the control D or control Z key combination.