Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.
And my code is:
1 2 3 4 5 6 7 8 9 10 11 12 13
int num;
ifstream outFile;
outFile.open("numbers");
outFile >> num;
outFile.close();
int a,sum=0;
for(int i=0; i<num; i++)
{
cin>> a;
sum = sum+a;
}
cout << sum << endl;
which works fine as long as the input is not more than one number. So, that's where I'm running into trouble. If, for instance, the input is 1 2 3 4 5, the output should be 15, but instead this code prints 5. Any nudges in the right direction/tips/hints would be very well appreciated.
As I understand it, you're supposed to read an integer value from standard input (the user) and read in that many values from the file (numbers), and sum them up.
In that case, you have outFile and cin mixed up....