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
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.
'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.
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.
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();
}