#include "../std_lib_facilities.h"
// Program to grow a vector by prompting user and calculating sum of numbers in the vector
// according to how many numbers the user wants to sum.
int main()
{
int sum=0;
int sumcount;
vector<int> numbers;
//Prompting user to grow vector
cout<< "Type in numbers. Enter a letter to stop\n";
int number;
while (cin>>number)
{
numbers.push_back(number);
}
//After getting out of the loop, the program doesn't wait for the user to cin>>sumcount and jumps through all the steps until he finishes...
cout<< "How many numbers would you like to sum?\n";
cin>>sumcount;
if (sumcount<numbers.size())
{
for (int i=0; i<sumcount; ++i)
{
sum+=numbers[i];
}
}
cout<<"Sum of " <<sumcount<<" first numbers = "<<sum;
}
> I see... How can I break out of the loop by pressing a letter without producing an error?
So you are trying to say that you want to handle the input error in the while loop in a peaceful way without having to break the loop? You want the while-loop to exit manually, not automatically?