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

you missed the point. Create only ONE structure that contains the weather data. Then instantiate an array of 12 of these structures.

struct weather
{
   int rainfall;
   int high_temp;
   int low_temp;
};

weather array[12];

In the above, array[0] = January, array[1] = February ... array[11] = December.

In main() the program should have a for loop that counts from 0 to 12 (once for each month) and inside the loop ask for the values of rainfall, high_temp and low_temp, then put these values in the array.


After you get the above working, you need to make another three integers to hold the total rainfall, highest temp and lowest temp so that you can display these values after the loop has finished. These three values should also be calculated inside the loop. Do not prompt the user for these values -- they are computed based on the answer to the other three questions.

Comments
Good post

In main() the program should have a for loop that counts from 0 to 12 (once for each month) and inside the loop ask for the values of rainfall, high_temp and low_temp, then put these values in the array.

I'm sure it was just a typo, but for 12 months, you want to count from 0 to 11 :)

This article has been dead for over six months. Start a new discussion instead.