Question
I need help with: Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature
I need help with:
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and the lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures. Input Validation: Only accept the temperature within the range between -100 and +140 degrees Fahrenheit.
I have some written, but I keep getting this at the very end of the input while debugging in Visual Studio:
"Exception thrown: read access violation.
std::_String_alloc
Please help!
Here is what I have so far:
#include "stdafx.h" #include
using namespace std;
struct weather { double totalRainfall; double highTemp; double lowTemp; double avgTemp; };
void main() { weather months[12]; double total = 0, highest, lowest, avgsum; int highmonth, lowmonth; string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
for (int i = 0; i < 12; i++) { //total rainfall cout << "Enter the total rainfall for the month of " << month[i] << ": "; cin >> months[i].totalRainfall; //rainfall validation while (months[i].totalRainfall < 0) { cout << "Rainfall cannot be less than zero (0). "; cout << "\tEnter the total rainfall: "; cin >> months[i].totalRainfall; }
//temp high cout << "Enter the temperature high: "; cin >> months[i].highTemp; //temp high validation while (months[i].highTemp < -100 || months[i].highTemp > 140) { cout << "Error: Temperature must be in the range of -100F through 140F. "; cout << "\tHigh Temperature: "; cin >> months[i].highTemp; } //temp low cout << "Enter the temperature low: "; cin >> months[i].lowTemp; //temp low validation while (months[i].lowTemp < -100 || months[i].lowTemp > 140) { cout << "Error: Temperature must be in the range of -100 through 40. "; cout << "\tLow Temperature: "; cin >> months[i].lowTemp; } }
//monthly average temp and total rainfall for (int i = 0; i < 12; i++) { total = total + months[i].totalRainfall; months[i].avgTemp = (months[i].highTemp + months[i].lowTemp) / 2; }
highest = months[0].highTemp; lowest = months[0].lowTemp;
for (int i = 1; i < 12; i++) { if (months[i].highTemp > highest) { highest = months[i].highTemp; highmonth = i; } if (months[i].lowTemp < lowest) { lowest = months[i].lowTemp; lowmonth = i; } }
avgsum = 0;
//average of average for (int i = 0; i < 12; i++) { avgsum = avgsum + months[i].avgTemp; }
//displaying data cout << "Average monthly rainfall: " << total / 12 << endl; cout << "Total monthly rainfall: " << total << endl; cout << "Highest rainfall in " << month[highmonth] << " is: " << highest << endl; cout << "Lowest rainfall in " << month[lowmonth] << " is: " << lowest << endl; cout << "Average of average temperatures is: " << avgsum / 12 << endl;
system("pause");
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started