Write a program that uses one struct to store the following weather information
ID: 3767139 • Letter: W
Question
Write a program that uses one struct to store the following weather information for one month:
Total rainfall
High temperature
Low temperature
Average temperature (Of High/Low temperature)
Assume the values are floats. The program should declare an array that holds 12 structs. The array can then hold weather information for up to an entire year. When the program runs, it should read the information for the year from a file called weather.dat. Each line of the file (0 to 12 lines are possible) will contain one month’s data for Total rainfall, High temperature, and Low temperature. You can assume all three values will be on the line. A typical line might be as follows:
3.2 88.1 59.3
The average temperature for the month should be calculated by the program and stored in the array with the other monthly values as the data is input. The values in the input file will be separated by blanks.
Once the data is read for all the months, the program should prompt the user to select a month (1 = Jan, 2 = Feb, etc.), and then display the Total rainfall, High temperature, Low temperature, and Average temperature for that month. If the user selects 0, the program should calculate and display the total rainfall for the year along with the average rainfall for the year. If the user selects an invalid month, the program should display a helpful error message. The program should allow the user to repeat the month selection until they are ready to exit. Remember to allow a sentinel menu option to exit. Label all output clearly and format it neatly. Use functions to modularize your program. A good programmer will check to be sure the program handles an empty file and the last record in the input file correctly, and calculates yearly values correctly when the number of months input is not 12. make sure to use comments in code.
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <string>
#include<fstream>
#include <sstream>
using namespace std;
//defining structure
struct Temp
{
float Trainfall;
float Htemperature;
float Ltemperature;
float Atemperature;
};
// to read data in to array
void ReadData(Temp array[],string line,int i)
{
stringstream linestream(line);
linestream >> array[i].Trainfall;
linestream>>array[i].Htemperature;
linestream>>array[i].Ltemperature;
float av=(array[i].Htemperature+array[i].Ltemperature)/2;
array[i].Atemperature=av;
}
// to print month wise report
void DisplayMonthWise(Temp array[],int m,int input)
{
int i=0;
cout<<"Total Rainfall of Month: "<<array[input-1].Trainfall<<endl;
cout<<"The High Temperature of Month: "<<array[input-1].Htemperature<<endl;
cout<<"The Low Temperature of Month: "<<array[input-1].Ltemperature<<endl;
cout<<"The Average Temperature of Month: "<<array[input-1].Atemperature<<endl;
}
// to print report for array
void DisplayDataofyear(Temp array[],int m)
{
float tot=0.0;
int i;
for(i=0;i<m;i++)
{
tot=tot+array[i].Trainfall;
}
cout<<"Total Rainfall of year: "<<tot<<endl;
cout<<"The Average Temperature of Year: "<<tot/12<<endl;
}
int main(int argc, char *argv[])
{
struct Temp array[12];
int input,m;
ifstream infile;
infile.open("Temperature.txt");
string line;
int i=0;
cout<<"Enter no of months to input data"<<endl;
cin>>m;
while(getline(infile, line))
{
ReadData(array,line,i);
i++;
}
cout<<"Enter 1-12 for Specific month to see data"<<endl;
cout<<"Enter 0 for total Rainfall and Average Rainfall"<<endl;
cout<<"-1 to exit"<<endl;
cin>>input;
while(input!=-1)
{
if(input>=1 && input<=m)
{
DisplayMonthWise(array,m,input);
}
else if(input==0)
DisplayDataofyear(array,m);
else if(input==-1)
return 0;
else
cout<<"Invalid input."<<endl;
cout<<"Enter 1-12 for Specific month to see data"<<endl;
cout<<"Enter 0 for total Rainfall and Average Rainfall"<<endl;
cout<<"-1 to exit"<<endl;
cin>>input;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.