reading from text file to find average number

i have a text file with 30 records of dates and numbers(rainfall)
i want to find the average rainfall between two dates so i want to be able to enter two dates and the output will give me the average rainfall of those two dates

a record will look like this:
1 12 2009 2.5
2 12 2009 5.1
and so on til 30

heres what i have so far. all this code does so far is display the date
1
2
3
4
5
6
7
8
9
10
11
12
int day = 0, month = 0, year = 0;
cout <<"Enter a date : " << endl;
cin >> day,month,year;
for (int i = 0; i < 30; i++)
{
if (day, month, year == date[i].day, date[i].month, date[i].year) 
{
infile >> date[i].day >> date[i].month >> date[i].year >> report[i].rainfall; 

cout << "rainfall for the "<< date[i].day << "/" << date[i].month << "/" << date[i].year << " is " << report[i].rainfall  << endl;
					}
				 }


i havn't got the average yet because i want to make sure the correct date is displaying which its not. The output is displaying all dates instead of the date i entered

Last edited on
really hope someone can help me with this
thanks
take a look at how to build an "if" statement.

 
	if (day, month, year == date[i].day, date[i].month, date[i].year) 


should be expressed

 
	if (day == date[i].day && month == date[i].month && year == date[i].year) 

ok but now its not displaying anything
i want to display the date i just entered
Line 3...
ok but what do i do? if i remove line 3 it still doesnt display anything
The "comma operator" doesn't work that way. Not in an if, not in a cin. For a cin, repeat the >> ('in' operator) for each variable.
ok thanks it works so now the code looks 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
24
25
26
27
28
int day = 0; 
int month = 0; 
int year = 0;
cout <<"Enter a date : " << endl;
cin >> day >> month >> year;

for (int i = 0; i < 30; i++)
{
	if (day == date[i].day && month == date[i].month && year == date[i].year) 
	{
	infile >> date[i].day >> date[i].month >> date[i].year >> report[i].rainfall; 
	cout << "rainfall for the "<< date[i].day << "/" << date[i].month << "/" << date[i].year << " is " << report[i].rainfall  << endl;
	}
 }

int day2 = 0; 
int month2 = 0; 
int year2 = 0;
cout <<"Enter another date : " << endl;
cin >> day2 >> month2 >> year2;

for (int i = 0; i < 30; i++)
{
	if (day2 == date[i].day && month2 == date[i].month && year2 == date[i].year) 
	{
	infile >> date[i].day >> date[i].month >> date[i].year >> report[i].rainfall;
	cout << "rainfall for the "<< date[i].day << "/" << date[i].month << "/" << date[i].year << " is " << report[i].rainfall  << endl;
}


now i want to be able to take the two rainfall numbers that displayed and find the average of them
Last edited on
You're already accessing the data in your ifs. Now just save them in a variable. Make sure to declare the variable outside of the for loops, because you'll need them afterwards.

You can either use N variables to store all N rainfalls (N = 2 in your question, but in the general case where you want multiple days, you'll need N variables) and then sum and divide at the end, or use one variable to store the total. In the first case, you'll need a separate variable for each and use the assignment operator ('=') inside the ifs. The latter case needs one variable (initialize at 0!) and the addition operator ('+=').
ok so do you mean something like this?

1
2
3
4
5
  int N = 0; 
double average;
average = (report[N].rainfall + report[N].rainfall)/2;
cout << "Average rainfall is : " << average << endl;
 

i know this code is wrong though because the average output is not correct
could you tell me what i'm doing work?
can anyone help with this?
No...
1
2
3
4
double sum = 0; //a running total
for( int i = 0; i < N; i++ ) //loop though the "report" array, where "N" is it's length
    sum += report[i].rainfall; //add to the running total
double avg = sum / N; //calculate the average 

report[N] would be outside the array's boundaries. Array indexes must be an integer in [0, N) i.e. [0, N-1].
Last edited on
its still not working

1
2
3
4
5
6
7
int N =0;
double sum = 0; 
for( int i = 0; i < N; i++ )
sum += report[N].rainfall; 
double avg = sum / N; 
				
cout << "avgerage = " << avg << endl;


the output looks like this

Enter a date: 12 12 2009
rainfall on the 12 12 2009 is 1.8
Enter another date:13 12 2009
rainfall on the 13 12 2009 is 2.3
average = -1.#IND


it should be getting the average of 1.8 and 2.3 but now its not even display a number
Think in what you are doing. Use pen and paper to follow your program.
report[i], iterate through the array by increasing i each time... Not array[N] which tries to dereference memory that is out of bound!
Last edited on
Topic archived. No new replies allowed.