I need to set up a loop that will pull a set of data from a text file for example, wet.txt In the text file will contain high and low temperature data, as well as rain accumulations for an unknown amount of days, For example:
low high rain
30 60 0.3
25 55 0
30 65 0
I feel like i'm making this too complicated, I feel like I can put this into just one loop but i'm not sure how. I believe I would be using a while loop because we don't how many days of data their would be.
The output should look like:
Days reported = 3
Min Temp = 25
Max Temp = 65
Avg Low = 28.3
Avg High = 60.0
Total rain = 0.3
Number of rainy days = 1
How do I make this happen with the while loop I have? Also, how do I call the variables "fin" into this function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <fstream>
#include <iostream>
usingnamespace std;
int main()
{
while( fin.good() )
{
fin >> low >> high >> rain;
}
system("PAUSE");
return EXIT_SUCCESS;
}
declare a bunch of variables outside your while loop and increment/change them within the loop while you're reading, then do your averages outside the loop.
int main()
{
ifstream fin("filename", ios::in);
// Declare some variables here to increment/change in your loop
int numDays = 0;
int sumLow = 0;
...
while( fin.good() )
{
fin >> low >> high >> rain;
...
sumLow += low;
++numDays;
}
// compute your averages and other stats here
int avgLow = sumLow / numDays;
...
}