Hello, this program takes input from a text file and displays the lowest temperature being the first column, highest temp second column and rain value third. MY program is able to display the minimum high(65)with an if statement ;however, my if statement for minimum low is not working correctly I am not sure why. It displays 30 instead of 25. Also, I am having trouble adding the values of rain, it displays zero, despite my summation statements being the same like the others. Thank you.
30 60 0.3
25 55 0
30 65 0
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
usingnamespace std;
int main()
{
ifstream weatherfile("wet.txt");//allows file to be opened
if (!weatherfile)//if statement to check if the file has opened correctly
{
cout<<"Please try again, unable to open text file"<<endl;//output if file does not open correctly
return -1;//return value if so
}
int minlow, minhigh;//declaring variables
int minimumlow, minimumhigh;//varibables that the above varibales will be set equal to, allows for calculations
double rain;//variable for rain
int counter=0;//counter declared if needed
int sumforlowtemp=0;//value holder for lowtemp holds the sum
int sumforhightemp=0;//value holder for hightemp holds the sum
int sumforrain=0;//value holder for rain holds the sum
int x=0;//allows the number data in each column to be added by how many entries
int b=0;//allows the number data in each column to be added by how many entries
int e=0;//allows the data in the rain column to be added, goes by entries
int averagelow;//variable for average
int averagehigh;//variable for average
while(weatherfile>>minlow>>minhigh>>rain)//tells program how data is organized in the file
{
sumforhightemp=sumforhightemp+minhigh;//sum of the high temperatures
sumforlowtemp=sumforlowtemp+minlow;//sum for the low temperatures
sumforrain=sumforrain+rain;//sum for the rain, this is displaying zero, why is that???
x++;//adding the entries
b++;//adding the entries
e++;//adding the entries
//cout<<minlow<<" "<<minhigh<<" "<<rain<<endl;
if(minimumlow<minlow)//if statement I am having trouble with it displays 30 not 25, not sure the program's logic behind this
{
minimumlow=minlow;//sets the minimumlow equal to the actual data
}
if(minimumhigh<minhigh)//this if statement works correctly and displays 65
{
minimumhigh=minhigh;//sets the minimumlow equal to the actual data
}
}
averagelow=sumforlowtemp/x;//calculations for average
averagehigh=sumforhightemp/b;//calculations for average
cout<<"Days reported: "<<x<<endl; //output of entries or days
cout<<"Minimum low temperature: "<<minimumlow<<endl;//output for minimum temperature
cout<<"Minimum high temperature:"<<minimumhigh<<endl;//output for minimum high
cout<<"Average low: "<<averagelow<<endl;//output for average
cout<<"Average high: "<<averagehigh<<endl;//output for average
cout<<"Total rain: "<<rain<<endl;//suppose to display total rain
}
As you have stated, you have one loop that works correctly and one that does not ... but as far as i am concerned, the statements do exactly the same thing only for values from different columns of your file.
So, well ... just try to turn the "greater" sign into a "lesser" or the other way around, and let me know if it worked.
Ah yes, I tried setting minimumlow and minimum high to a value of zero and it displays 30, or 0. If I set the value greater than it will chose that value.
Fluppe, the program would set the value to 0:(.