Take a file with numbers and output the average?

Hi,

I'm working on a practice project where I have to take the input from the files type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks.

I pretty much have a way to input an output the file but I think I'm missing something. This is what I've done so far.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

void add_plus_plus(ifstream& in_stream, ofstream& out_stream);

int main()
{
    ifstream fin;
    ofstream fout;
    
    char file_name[30];
    char file_output[40];
    
    cout << "Give a file name to open for input\n";
    cin >> file_name;       
    
    cout << "Give a file name to open for output\n";
    cin >> file_output;
    
    fin.open(file_name);
    if (fin.fail())
    {
       cout << "Input file opening failed.\n";
       system ("pause");
       exit(1);              
    }

    fout.open(file_output);
    if (fout.fail())
    {
       cout << "Output file opening failed.\n";
       system ("pause");
       exit(1);                
    }
    
    add_plus_plus(fin, fout);
    fin.close();
    fout.close();
    
    cout << "End of editing files.\n";
    system ("pause");
    return 0;
}

void add_plus_plus(ifstream& in_stream, ofstream& out_stream)
{
     int count = 1;
     char next;
     out_stream << count << ": ";
     cout << count << ": ";
     in_stream.get(next);
     while (! in_stream.eof( ))
     {          
           out_stream << next;
           cout << next;
           if (next == '\n')
              {
              count++;
              cout << count << ": ";
              out_stream << count << ": ";
              }           
           in_stream.get(next);
     }
}
Last edited on
You not parsing or adding number anywhere for one...
Why do you need an output file?
Just use cin << num to get for numbers, it will parse them for you (assuming num is of type double.)
Hint. Declare an array to store the numbers in file and also a for loop to loop through the file ;) http://www.cplusplus.com/doc/tutorial/control/ that will get you going and make your program a bit shorter but more efficient
They do not need an array. They only need to store the (running) sum and count to find an average.
Thanks for the replies, so which way do I do it? Some are saying do it one way some are saying another.

How do I open a specific file in a specific location?
Algorithms wrote:
I have to take the input from the files type double and outputs the average of the numbers in the file to the screen.


1
2
3
4
5
6
7
8
9
10
11
12
13
/* Example code */

ifstream dataFile("path/filename.extension");     // Open your data file for input use.

double sum = 0;                                   // This is your running total.
int count = 0;                                    // This is how many numbers have been read from the data file.
double number;                                    // This is a temporary place for input to be stored.
while( dataFile >> number ) {                     // Try to get the next number from input, and continue to loop if successful.
    count++;                                      // You successfully got another number from the file so increment the count.
    sum += number;                                // Also, add that number from the file to your running total.
}

double average = sum / count;
Last edited on
Topic archived. No new replies allowed.