Write a program that uses one struct to store the following weather information
ID: 3769439 • 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
# Include <iostream.h>
# include <string.h>
struct weather
{
char month[12];
float total_rainfall;
float high_temp;
float low_temp
float average_temp:
};
Void main ()
{
weather year [12]
{"January", 0.0, 0.0, 0.0, 0.0},
{"February", 0.0, 0.0, 0.0, 0.0},
{"March", 0.0, 0.0, 0.0, 0.0},
{"April", 0.0, 0.0, 0.0, 0.0},
{"May", 0.0, 0.0, 0.0, 0.0},
{"June", 0.0, 0.0, 0.0, 0.0},
{"July", 0.0, 0.0, 0.0, 0.0},
{"August", 0.0, 0.0, 0.0, 0.0},
{"September", 0.0, 0.0, 0.0, 0.0},
{"October", 0.0, 0.0, 0.0, 0.0},
{"November", 0.0, 0.0, 0.0, 0.0},
{"December", 0.0, 0.0, 0.0, 0.0},
float total_year rain=0
float accumulated_avgtemp=0
float highest_temp=-100
float lowest_temp=140
char calender[12][10]
};
for (int x = 0; x < 12; x++)
{
cout << "Please enter the following
data for " << month[x]<< ": ";
cout << "High temperature: ";
cin >> month[x].high_temp;
cout << "Low temperature: ";
cin >> month[x].low_temp;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.