in C++, I must input 3 weather values (temperature low, temperature high, rain)
ID: 3568979 • Letter: I
Question
in C++, I must input 3 weather values (temperature low, temperature high, rain) and output the following: days reported, min temp, max temp, avg low, avg high, total rain and number of rainy days. This all must take place in a while loop in the function main. This program will also input from a text file. I am also not to use a prompt for input. so far I have:
#include <iostream>
using namespace std;
int main()
{
int tempHigh, tempLow, reportedDays=0, min=0, max=0, count=0, rainyDays=0;
double rain, avgLow=0, avgHigh=0, totRain=0, sumMin=0, sumMax=0;
while (reportedDays<=10)
{
cin >> tempHigh >> tempLow >> rain;
count++;
if (tempLow<min)
tempLow=min;
if (tempHigh>max)
tempHigh=max;
sumMin=sumMin+tempLow;
avgLow=sumMin/reportedDays;
sumMax=sumMax+tempHigh;
avgHigh=sumMax/reportedDays;
totRain=totRain+rain;
if (rain>0)
rainyDays=reportedDays;
}
cout << endl;
cout << "Days reported = " << reportedDays;
cout << "Min temp = " << min;
cout << "Max temp = " << max;
cout << "Avg low = " << avgLow;
cout << "Avg high = " << avgHigh;
cout << "Total rain = " << totRain;
cout << "Number of rainy days = " <<rainyDays;
return 0;
}
Explanation / Answer
// This opens a text file name input.txt within your program folder. It counts the input from the file as part of your 11 days. (since you start your loop at 0 and go until equal or less than 10 it makes it 11 days.) Only thing I changed in your subitted code was count = reportedDays and days of rain is added separately not equal to reported Days.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int tempHigh, tempLow, reportedDays=0, min=0, max=0, count=0, rainyDays=0;
double rain, avgLow=0, avgHigh=0, totRain=0, sumMin=0, sumMax=0;
// *********************************** Reading an input file ********************************************
ifstream inFile;
string filename;
filename = "input.txt";
inFile.open(filename.c_str());
if (inFile.fail())
{
cout << " The file was not successfully opened"
<< " Please check that the file path is correct."
<< endl;
cout << endl;
exit(1);
}
cout << " The file has been successfully opened for reading."
<< endl;
cout << endl;
// ************** Read your values
inFile >> tempHigh >> tempLow >> rain;
while(inFile.good())
{
cout << tempHigh << " " << tempLow << " " << rain << endl;
inFile >> tempHigh >> tempLow >> rain;
count++;
}
inFile.close();
// **************************************** End of input file *******************************************************************************
while (reportedDays<=10)
{
cin >> tempHigh >> tempLow >> rain;
count++;
reportedDays = count;
if (tempLow<min)
tempLow=min;
if (tempHigh>max)
tempHigh=max;
sumMin=sumMin+tempLow;
avgLow=sumMin/reportedDays;
sumMax=sumMax+tempHigh;
avgHigh=sumMax/reportedDays;
totRain=totRain+rain;
if (rain>0)
rainyDays += 1;
}
cout << endl;
cout << "Days reported = " << reportedDays << endl;
cout << "Min temp = " << min << endl;
cout << "Max temp = " << max << endl;
cout << "Avg low = " << avgLow << endl;
cout << "Avg high = " << avgHigh << endl;
cout << "Total rain = " << totRain << endl;
cout << "Number of rainy days = " <<rainyDays << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.