Loop through data file and calculate average


Hello, I'm trying to loop through a .dat file and calculate the average of a group of numbers. Here is what the data looks like. So essentially I am trying to skip certain numbers out of the data file. The numbers in [] indicate the following numbers to be averaged together. However, I'm having troubles just learning how to loop through the data file.

Any help is much appreciated

[5] 96 87 78 93 21 [4] 92 82 85 87 [6] 72 69 85 75 81 73

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
68
69
70
71
72
73
74
75

// W926(A).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string filename = "numbers.dat";
	ofstream outFile;
	ifstream inFile;
	int num1;
        int i;
	
	//Create file numbers.dat
	outFile.open(filename.c_str());

	//Check to make sure file opened correctly
	if (outFile.fail())
	{
		cout << "The file was not opened correctly" <<endl;
		exit(1);
	}

	//Data that will be entered into employee.dat
	outFile << 5 << ' ' << 96 << ' ' << 87 << ' ' << 78 << ' ' << 93 
		<< ' ' << 21 << ' ' << 4 << ' ' << 92 << ' ' << 82 << ' ' << 85 
		<< ' ' << 87 << ' ' << 6 << ' ' << 72 << ' ' << 69 << ' ' << 85
		<< ' ' << 75 << ' ' << 81 <<' ' << 73;
		
	//Close numbers.dat as a output file
	outFile.close();

	//Open numbers.dat as a input file
	inFile.open(filename.c_str());

	//Check to make sure file opened correctly
	if (outFile.fail())
	{
		cout << "The file was not opened correctly" <<endl;
		exit(1);
	}

		int count = 0; 

	while(inFile.good())
	{
		count++;
                 //Use for loop once counter reads first number (5)
		if (count == 1);
		{
			for(i = 1; i < 5; i++)
			{
                                //Ignore 5
				inFile.ignore();
				inFile >> num1;
				cout << num1 << endl;
			}
		}
		
	}


	system("pause");
	return 0;
}

in this loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while(inFile.good())
	{
		count++;
                 //Use for loop once counter reads first number (5)
		if (count == 1);
		{
			for(i = 1; i < 5; i++)
			{
                                //Ignore 5
				inFile.ignore();
				inFile >> num1;
				cout << num1 << endl;
			}
		}
		
	}


the comment reads //Use for loop once counter reads first number (5)
1) but you are not reading any number

2) counter will turn 1 inside while and then the for loop will just read 5 numbers blindly (if it does read any, [5] in the example is not a valid number). If I get correctly then you need to read how many numbers to read and average then read those many numbers

3) This way the loop will be an infinite loop if there are more than 5 numbers in the file, after count becomes 2 the for loop will never get executed and hence the inFile >> num will never will get executed and the inFile.good() will be always true
This is what I'm trying to do.

Read the first 5 numbers after the [5] - calculate average (it's not entered like that in data, I only put brackets so it'd be easier to read). Then read the next 4 numbers after the [4] and calculate average. Then read the final 6 numbers after [6] and calculate average.

I'm not so much concerned with skipping the numbers as I am just getting this damn loop to work, been trying so many variations for hours. It's frustrating.

1
2
3
4
5
6
7
8
9
10
11
		while(!inFile.eof())
		{
			for (int i = 0; i < 5; i++)
			{
				number = inFile.get();
				total = number + total;
			}
		}




No matter what the counter is, int i = 0; i < 1; ... it always reads the entire file!!
Try this

1
2
3
4
5
6
7
8
9
while(!inFile.eof())
{
    inFile >> count;
    for (int i = 0; i < count; i++)
    {
        number = inFile.get();
	total = number + total;
    }
}
I'm assuming you set count = 0, which I did and it's still adding up the sum of the entire file.

Thanks for trying.
So essentially I am trying to skip certain numbers out of the data file.

I'd consider that isn't the best way of viewing the problem.

From my point of view, the numbers which you 'skip' are the key to understanding the contents of the file, and should therefore be read and used to control the subsequent processing. In the code below, the count variable is used for those important numbers. The numbers to be averaged are read into number.

I wrote this based upon the input data, I've not studied the previous code, so I'm not sure how helpful this will be.

(I used a stringstream in this example, but a normal file should work in the same way).
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

    using namespace std;

int main()
{
    string data("5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73");

    istringstream inFile(data);

    int count;

    while (inFile>>count)
    {
        int number;
        double sum = 0.0;
        int more = count;
        while (more--)
        {
            if (inFile >> number)
                sum += number;
            else
            {
                cout << "Input data error" << endl;
                more = 0;
            }
        }
        double average = sum / count;
        cout << "Average of " << count << " values is " << average << endl;
    }

    return 0;
}
Topic archived. No new replies allowed.