Struggling with outputting to a file?

I basically need to read numbers from a file, where the first # incdicates how many numbers should be averaged together. And the program runs until all the numbers have been accounted for. I have a program written but for some reason, this program does not create an output file. It just prints out the numbers in my input file in secureshell. Please help!!

The input file just looks like:
4 12 14 99 83 5 2 9 56 4 3 2 13 15 1 6 8 32 12 3 4 5 12 34 45

This is my program:
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
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

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

int main()
{
    ifstream in;
	in.open("lab6input.dat");
    ofstream out;
	out.open("lab6output.dat");
    
    char file_name[30];
    char file_output[40];
    
    add_plus_plus(in, out);
    in.close();
    out.close();
	
    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);
     }
}
Line 33, 44: You're reading the input one character at a time. If you intend to average the numbers (per the instructions), you should be reading ints.

The instructions say the first number (4) is the number of values to be averaged, and to continue until all numbers are accounted for.

4 12 14 99 83 5 2 9 56 4 3 2 13 15 1 6 8 32 12 3 4 5 12 34 45

To me, this implies there are 5 groups of numbers to be averaged.

Last edited on
You should also check to insure that the files open correctly. Something more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    // Use the constructors instead of calling open.
    ifstream in("lab6input.dat");
    if(!in)
    {
         cerr >> "Couldn't open the input file.\n";
         return 1;
    }
    ofstream out("lab6output.dat");
    if(!out)
    {
        cerr >> "Couldn't open the output file.\n";
        return 2;
     }

    add_plus_plus(in, out);

    // No need to close the file streams, let the destructors do their jobs.
}


AbstractionAnon - how exactly would I do that? That is what I need to do but I'm not certain as to how I would go about doing that.

jib - thank you! I totally forgot about that
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void add_plus_plus (ifstream& in_stream, ofstream& out_stream)
{   int count;
    int num;
	int sum;
	double avg;
	
	while (cin >> count)    // Loop until no more counts are found
	{   cout << "count: " << count << endl; 
        out_stream << "count: " << count << endl; 
        sum = 0;
        for (int i=0; i<count; i++)     //  Loop count times to pick up numbers
        {   in_stream >> num;
            sum += num;
            cout << num << endl;
            out_stream << num << " ";
        }
        out_stream << endl;
        //  We've read all the numbers in the group
        avg = (double)sum / count;
        cout << "Average for group = " << avg << endl;
        out_stream << "Average for group = " << avg << endl;               
    }        
 }

Topic archived. No new replies allowed.