Write a program that uses one struct to store the following weather information
ID: 3769440 • 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.
also add comments and make sure to use data from file called weather.dat
Explanation / Answer
using namespace std;
int main()
{
const int NUM_MONTHS = 12; //number of values
int values[NUM_MONTHS]; //each value
int month; //loop counter
int lowest, highest, rainfall, count;
double average;
double total = 0;
string months[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for(int month = 1; month <= NUM_MONTHS; month++)
{
cout << "Enter the total rainfall for " << months[month-1] << ": ";
cin >> rainfall;
if ( rainfall < 0 )
{
cout << "The number you have entered is invalid." << endl;
cout << "Please reenter: ";
cin >> rainfall;
}
total += rainfall;
}
average = total/12;
highest = values[0];
lowest = values[0];
for(int month = 0; month < NUM_MONTHS; month++)
{
if(values[month] > highest)
highest = values[month];
if(values[month] < lowest)
lowest = values[month];
}
cout << " The total rainfall for the year is: " << total << " inches" << " The average monthly rainfall is: " << average << " inches";
cout << " The month with the highest rainfall is: " << highest << " The month with the lowest rainfall is: " << lowest << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.