Averaging numbers in text file array

I am trying to get an output with averaging the numbers in a text file, but I keep getting a value of 0. I am pretty sure I am getting something wrong or missing while readFile is being run.

The text file includes 31 days of rain

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

class Rain
{
private:
	ifstream inRain;	
	float sumRain;
	float avgRain;

public:

	Rain();			
	void openFile();			
	void testFile();
	void readFile();
	void closeFile();
	void calcAvg();
	void display();
};

Rain::Rain()
{}

//open the file
void Rain::openFile()
{
	inRain.open("JuneRain.txt");

	testFile();
}

//test the file
void Rain::testFile()
{
	if (!inRain)
	{
		cerr << "Input file did not open properly" << endl;
		exit(1234);
	}
}

//close the input file
void Rain::closeFile()
{
	inRain.close();
}



//read the file 
void Rain::readFile()
{
	float sumRain;

	while (inRain >> sumRain	)
	{
		if (sumRain += avgRain)
			cout << sumRain;
	}
}

//calculate average rain
void Rain::calcAvg()
{
	avgRain = sumRain / 31;
}

//display the average
void Rain::display()
{
	cout << "The average rain for June is: " << avgRain << fixed << setprecision(2) << endl;
}


int main()
{
	//variables
	ifstream inRain;

	Rain rainObj;

	//open the file and test input
	rainObj.openFile();

	//process the input file
	rainObj.readFile();

	//close the input file
	rainObj.closeFile();

	//display the results
	rainObj.display();

	return 0;
}
Last edited on
closed account (E0p9LyTq)
Look closely at your readFile() method. You are reading each monthly datum into your sumRain variable, instead of adding each read value to the sum.

Unless the last rainfall is greater than 31 (int) then you will get a sum of zero.

You should initialize your data members in your constructor, otherwise you will get bogus starting values.
Last edited on
To expand on FurryGuy's answer, your readFile() method is adding to the local variable sumRain declared at line 58, not to the member variable sumRain.

calcAverage() will only work if the input file contains exactly 31 numbers. You need to keep track of the number of values too.
Hello SammyB,

Note not all months have 31 days. February has 28 or 29 days and some months have 30 days. You will need to acount for this in your program especially with the averaging.

I noticed in line 76 that "fixed" and "setprecision" should come before "avgRain" in order for it to affect the output of "avgRain".

Hope that helps,

Andy
Hello SammyB,

Looking over the program I noticed some problems.

In the subject line you say in text file array. Are you considering the text file an array? Or do you want to store the numbers form the input file into an array?

It may be some extra work, but I would consider reading the txt file into an array in the program. This would come in handy should you add to the program. If not the sum of all the numbers read can be sotred for later use in calculating the average.

In the class you define an "ifstream" for "inRain" and then again in main. The one in main is redundant, not used and not needed.

In the function "testFile()" you should find this code of more use:
1
2
3
4
5
6
if (!inRain)
{
	cerr << "Input file did not open properly" << endl;
	std::this_thread::sleep_for(std::chrono::seconds(5));  // Requires header files "chrono" and "thread"
	exit(1);
}

If you use VS as I do and run the program from the IDE you should find that the console window closes when the program ends. The second line of code will keep the console window open for 5 seconds before the "exit()" ends the program and the console window closes giving the user time to read the message.

"exit(1234);" is OK, but over kill. Any number greater then zero is considered an error exit. So all you really need is "1" or "2" or "3" etc. inside the ()s. If You should have more than one "exit()" in the program the number will help to find where the problem is.

You have referred to an input file for the month of June. Since June only has 30 days this could be a potential problem. It would also help if you post the input file, so anyone working on the program will be using the same information that you are. This way totals and the average should math.

Hope that helps,

Andy
Topic archived. No new replies allowed.