for loop to find certain numbers ?

ok so i'm using structs for the first time and don't really understand how they work. I have a text file with 30 records of dates, names, and other information. In the text file there are a list of rainfall in milimeters and i need to find all the days were the rainfall is >=4. Here is the code but its only the for loop at the end i need help on. A line of the text file would look something like this:
Date Name Sun Rain Temp
29/12/2009 Tom 3.5 5.2 4.8
I need to list the rainfall thats >=4 from the text file

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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct Date  //heres all the structs
{
	int day;
	int month;
	int year;
};

struct Personnel
{
	string name;
}; Personnel person;

struct Report
	{
		double sunshine;
		double rainfall;
		double temperature; 
	}; 

void DoFindAllWetDays(void);

int _tmain(int argc, _TCHAR* argv[])
{
	
	Date date[30];
	Personnel person[30];
	Report report[30];

	ifstream infile ("report.txt");	//this just displays text file
	
    while(!infile.eof())
		for (int i = 0; i < 30; i++)
	{
		infile >> date[i].day >> date[i].month >> date[i].year >> person[i].name >> report[i].sunshine >> report[i].rainfall >> report[i].temperature;	
	}


	
	cout << "Date\t\tName\t\tSunshine\tRainfall\tTemperature\n";
	for (int i = 0; i < 30; i++)
		{
			cout << date[i].day << "/" << date[i].month << "/" << date[i].year << "\t" << person[i].name << "\t\t"
				 << report[i].sunshine << "\t\t" << report[i].rainfall << "\t\t" << report[i].temperature << endl;	   
		}


	infile.close();	 
			
	return 0;
}

here is the for loop i need help on	
void DoFindAllWetDays(void)
{
	for for (int i = 0; i < 30; i++)
          {

          }
}


thanks
Last edited on
Please use code tags! That way, it's easier for us to read your code as well as refer directly to line numbers!

Each struct-object contains the data of a day. You're keeping an array of 30 of these objects, thus 30 days of data. You read in the data sequentially, thus struct i contains the data of day i, for all values of i between 0 and 29 (i.e. i € [0;29], thus inclusive).

This means that for each i, report[i].rainfall is the double containing the rainfall of day i. Thus, report[i].rainfall is simply a value.

Now that you can access the rainfall values for each day, you can also compare them. Since you're simply comparing two numbers (rainfall of day i versus "4"), you can use a simple if statement inside your loop.
thanks i did try to use an if statement but it kept telling me report is undeclared

this is what i tried:

1
2
3
4
5
6
7
8
for (int i = 0; i < 30; i++)
	{
		if(report[i].rainfall >= 4)
		{
			cout << "All wet days are " << rainfall << endl;
		}
	
	}
'rainfall' on line 5 is undeclared. You're outputting nothing. It's like asking "what is name?", rather than "what is your name?". "Name" has no value. My name and your name do.

In the same way, 'rainfall' is meaningless, unless it is attached to a report: "report[i].rainfall" does exist.

i thought it was declared up in the struct:

1
2
3
4
5
6
struct Report
{
double sunshine;
double rainfall;
double temperature; 
}; 


when i wanted to display rainfall in the text file report[i].rainfall worked as you can see in line 40 and 49 of the code above

so where would i put report[i].rainfall if that is the correct way of doing the if statement?
Last edited on
It's correct in the 'if' statement. It's not correct in the cout in line 5. Simply replace that by "... << report[i].rainfall << ..." and you'll be fine.
thanks for the help but its still not working

the report that i underlined says 'report' : undeclared identified

1
2
3
4
5
6
7
8
for (int i = 0; i < 30; i++)
	{
		if(report[i].rainfall >= 4)
		{
			cout << "All wet days are " << report[i].rainfall << endl;
		}
	
	}
Ah, that's because report is only defined inside main(), thus your function DoFindAllWetDays() doesn't know it. You can try defining it globally (Disch don't hurt me!) or passing it to your function as an argument.

Alternatively, you can make a member function of report called "checkIfWet()" (or whatever) that checks whether its rainfall variable is >= 4 and if so, outputs. Then, in your main, call:
1
2
3
for (int i = 0; i < 30; ++i) {
  report[i].checkIfWet();
}
so where would i put checkIfWet() in my program and would i say something like

if (checkIfWet() )
{
rainfall >=4
}
No, that does nothing.

You'd make checkIfWet() a member of Report:
1
2
3
4
5
6
7
struct Report {
  ...
  ... 
  bool checkIfWet() {
      // Code to check if rainfall >= 4
  }
}

All you need to do is an a check for the rainfall and have the function return the result.
ok 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
struct Report
	{
	double sunshine;
	double rainfall;
	double temperature; 
	bool checkIfWet() 
{ 
   if (rainfall >=4) 
        {
	cout <<  "wet days are " << rainfall << endl;
         }
		
};



void DoFindAllWetDays(void)
{
	
	bool checkIfWet();	

}


but nothing is coming up
Topic archived. No new replies allowed.