### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` `````` // W926(A).cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include 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" <> num1; cout << num1 << endl; } } } system("pause"); return 0; } ``````
in this loop

 ``12345678910111213141516`` ``````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.

 ``1234567891011`` `````` 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

 ``123456789`` ``````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).
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include #include #include #include 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.