May 2, 2006
Hi!
I am trying to 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 is entered for all the months, the program should caluclate the and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures.
Inpuit Validation: ONly accept temperatures within the range between -100 and +150 degrees Fahrenheit.
Below is the code I have so far:
// This program shows a structure with two nested structure members.
#include <iostream>
using namespace std;
struct Weather
const int RAINFALL_LENGTH = 2;
const int HIGH TEMP_LENGTH = 3;
const int LOW TEMP_LENGTH = 3;
struct January
{
int rainfall;
int high temp;
int low temp;
};
struct February
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct March
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct April
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct May
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct June
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct July
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct August
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct September
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct September
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct November
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
struct December
{
int rainfall[RAINFALL_LENGTH];
int high temp[HIGH TEMP_LENGTH];
int low temp[LOW TEMP_LENGTH];
};
int main()
{
RainFall;
// Ask for the user for the amount of rainfall
cout << "Enter the amount of rainfall: ";
cin.getline(rainfall.Inches, RAINFALL_LENGTH);
// Get the high and low temperatures
cout << "Now enter the high temperature:\n";
cout << "Temperature (up to 3 digits): ";
cin >> High.Temperature.month;
cout << "Enter the low temperature: \n";
cin >> Low.Temperature.month;
cin.get(); // Remove the remaining newline character