Why it can't read in a number?
Hi, I can't figure out why it can't read in the number N... Can you please help?
Code:
// Exercise 8 - Chapter 5 ("Programming: Principles and Practice using C++")
#include "std_lib_facilities.h"
class out_of_range_error{};
int main()
try {
vector <int> numbers;
int num, N, sum;
cout << "Enter some numbers (press '|' at prompt to stop):";
while (cin>>num)
numbers.push_back(num);
cout << "Enter how many of the first numbers you want to sum: \n";
cin >> N; // PROBLEM HERE!!! - It cannot read in N!
cout << "The sum of the first " << N << " numbers: ";
sum = 0;
for (int i=0; i<N; ++i) {
sum += numbers[i];
cout << numbers[i] << ", ";
if (i==(N-1)) cout << " and";
}
cout << " is " << sum << endl;
}
catch (out_of_range_error) {
cout << "Range error!\n";
return 1;
}
catch (...) {
cerr << "Exception: Something went wrong\n";
return 2;
}