Completely stuck and lost

Feb 11, 2014 at 6:56pm
Program is supposed to:

1) Be given the name of a file and open the file for input. If the file cannot be opened, program should immediately print an error message and then exit.

2) Given a reference to an open input file, reads the integers in that file and returns their average. The list of integers is terminated by -1. The -1 should not be averaged with the other numbers.

3) Given the name of a file and the average of the numbers in that file and prints that information to the screen in nicely formatted form.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include <iostream>
#include <fstream>
int main()
{
    using namespace std;
    ifstream inp;
    ofstream outp;
    char fileName[81];
    int input;
    //------------------
    cout << "Which file would you like to open?\n";
    cin >> fileName;
    //------------------
    inp.open(fileName);//open file under inp
    while(inp.fail())//if the file fails
    {
        inp.close();
        inp.clear();
        cout << "File does not exist.\n";
        system("pause");
        exit(1);}
    //------------------
    int sum(0), count(0), lastNum;
    while (inp >> fileName && lastNum != -1)
    {inp >> fileName;
    inp >> lastNum;
    sum = sum + lastNum;
    count++;
    cout << lastNum;}
    count = count - 1;
    //------------------
    double avg = (sum/count);
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    system("pause");
    inp.close();
    return 0;
}

It does not do any of that. Even the first number input, lastNum, is not correct. Seriously need help badly any advice is helpful. Thanks.
Feb 11, 2014 at 10:31pm
It looks like you're expected to use functions, certainly for step 2; and possibly a seperate program for 3.

You seem to have done step 1 ok. Doesn't it work?
Topic archived. No new replies allowed.