I need help with: Write a program that uses a structure to store the following w
ID: 3809721 • Letter: I
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
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<std::_String_base_types<char,std::allocator<char> > >::_Mysize(...) returned 0x66DA5BE8."
Please help!
Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include <string>
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 << " Enter 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 << " High 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 << " Low 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");
}
Explanation / Answer
For me it executed, The following is the output.
Enter the total rainfall for the month of January: 12
Enter the temperature high: 45
Enter the temperature low: 12
Enter the total rainfall for the month of February: 13
Enter the temperature high: 46
Enter the temperature low: 11
Enter the total rainfall for the month of March: 15
Enter the temperature high: 44
Enter the temperature low: 44
Enter the total rainfall for the month of April: 12
Enter the temperature high: 11
Enter the temperature low: 11
Enter the total rainfall for the month of May: 44
Enter the temperature high: 22
Enter the temperature low: 33
Enter the total rainfall for the month of June: 56
Enter the temperature high: 11
Enter the temperature low: 1
Enter the total rainfall for the month of July: 6
Enter the temperature high: 54
Enter the temperature low: 23
Enter the total rainfall for the month of August: 90
Enter the temperature high: 89
Enter the temperature low: 50
Enter the total rainfall for the month of September: 50
Enter the temperature high: 30
Enter the temperature low: 12
Enter the total rainfall for the month of October: 90
Enter the temperature high: 99
Enter the temperature low: 87
Enter the total rainfall for the month of November: 22
Enter the temperature high: 22
Enter the temperature low: 12
Enter the total rainfall for the month of December: 22
Enter the temperature high: 22
Enter the temperature low: 11
Average monthly rainfall: 36
Total monthly rainfall: 432
Highest rainfall in October is: 99
Lowest rainfall in June is: 1
Average of average temperatures is: 33.4167
In visual studio it will throw this kind of errors are observed. So try to increase the value of month[12] to month[13] and try again.