Basic C++ code?

I'm having trouble getting this code to work:

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.

This is what I put in:

1
2
3
4
5
int a;
cin << a;
numbers.open("numbers");
numbers<<a;
cout >>numbers
For starters cin >> a instead of cin << a; also cout << numbers, not cout >> numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("numbers.txt"); // file named numbers

    int NumberOfNumbers, sum = 0;
    cin >> NumberOfNumbers;      // integer from standard input

    while(NumberOfNumbers--)     // that many values from numbers
    {
        int read;
        fin >> read;             // reading in
        sum += read;             // tracking total
    }
    cout << sum;                 // display thier total
}


I know I'm not supposed to give answers, but I'm just having a lazy evening.
Last edited on
Oh wow, I can't believe I didn't notice that, my mistake.
Topic archived. No new replies allowed.