#include <iostream>
#include <vector>
#include <algorithm>
usingnamespace std;
int main()
{
vector<int> vec;
int x;
int sum = 0;
cout << "Please enter the numbers: \n\n";
while (cin>>x){
vec.push_back(x);
}
for (int i = 0; i != vec.size(); ++i){
cout << vec[i] << ' ' ;}
int numtosum;
cout << "How many numbers would you like to sum? \n\n";
cin >> numtosum;
}
I enter an invalid character say '|' to exit the while loop, but this seems to print the next line and exit the program. No prompt is shown to input the next value.
1. It ignores whitespaces (space, tab, newline)
2. It reads Input to the next whitespace
3. It leaves the whitespace in the buffer
So, if you type "| <Enter>" the newline character is still in the buffer, is read by "cin >> numtosum;" and the program exits. So, either you type cin.ignore(1000,'\n')
before cin >> numtosum;