I have searched these forums and have seen multiple threads on the Weather Structure C++ program. I don't believe they have been resolved.

I am tasked with the following:

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 lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures.

Input validation: only accept temperatures within the range between -100 and 140 degrees Fahrenheit.

My question is if I can modify this code below to use a structure and change it to collect the necessary data. Or if I need to just start from scratch.

// This program takes rainfall statistics and displays the total for a year, average monthly rainfall, and highest and lowest amounts.
// 7.2 Programming Challenge
#include <iostream>
#include <iomanip>
using namespace std;

// Constant for number of months
const int numMonths = 12;

//Function Prototypes
double getTotal(double []);
double getAverage(double []);
double getHighest(double [], int &);
double getLowest(double [], int &);

int main ()
{
	// Array for holding the rainfall amounts
	double rainFall[numMonths];

	// Get rainfall for each month.
	for (int month = 0; month < numMonths; month++)
	{
		// Get months rainfall
		cout << "Enter the rainfall in inches for month #:";
		cout << (month + 1) << ": ";

		// Validate user input cannot accept negative numbers.
		while (rainFall[month] < 0 )
		{
			cout << "Rainfall must be greater than 0.\n";
			cout << "Please enter correct total.\n";
			cin >> rainFall[month];
		}
	}
	// Setting numeric output formatting
	cout << fixed << showpoint << setprecision(2) << endl;

	// Display total rainfall.
	cout << "The total rainfall for the year is ";
	cout << getTotal(rainFall)
		 << " inches." << endl;

	// Display the average rainfall from the year
	cout << "The average rainfall for the year is ";
	cout << getAverage(rainFall)
		 << " inches." << endl;

// Display the largest amount of rainfall
int amount = 0;

cout << "The highest amount of rainfall was ";
cout << getHighest(rainFall, amount)
	 << " inches in month ";
cout << (amount + 1) << "." << endl;

//Display the lowest amount of rainfall
cout << "The lowest amount of rainfall was ";
cout << getLowest(rainFall, amount)
	 << " inches in month ";
cout << (amount +1) << "." << endl;

system ("pause"); 
return 0;
}
// getTotal array reads values from array to get total
double getTotal(double rainFall[])
{
	double total = 0;
	for (int count = 0; count < numMonths; count++)
		total += rainFall[count];
	return total;
}

// Get total to calculate the average
double getAverage(double rainFall[])
{
	double average = 0.0;
	average = getTotal(rainFall) / numMonths;
	return average;
}

// Function to get the highest month rainfall
double getHighest(double rainFall[], int &amount)
{
	double highest;
	highest = rainFall[0];

	for (int month = 0; month < numMonths; month++)
	{
		if (rainFall[month] > highest )
		{
			highest = rainFall[month];
			amount = month;
		}
	}
	return highest;
}

// Function to get the smallest month total
double getLowest(double rainFall[], int &amount)
{
	double lowest;
	lowest = rainFall[0];

	for (int month = 0; month < numMonths; month++)
	{
		if (rainFall[month] < lowest )
		{
			lowest = rainFall[month];
			amount = month;
		}
	}
	return lowest;
}

This is what I started for the new program that uses structures.

// This program collects the weather statistics for a year. The data it collects is total rainfall, high temp, low temp, average temp.
//Programming Challenge 11.4
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Weather
{
	double rainfall;
	double high_temp;
	double low_temp;
	double avg_temp;
};

// Function Prototypes
void getTotalrain(double);
void getAvgrain(double);
void getAverageTemp(double);
void getHighTemp(double);
void getLowTemp(double);


int main()
{
	const int NUM_MONTHS = 12;   //Number of Months
	Weather months[NUM_MONTHS];  //Array of 12 months
	int index;                   //Loop counter
	
	double annualRainfall;

	
	// Get rainfall for each month.
	for (int index = 0; index < NUM_MONTHS; index++)
	{
		// Get months rainfall
		cout << "Enter the rainfall in inches for month #:";
		cout << (index + 1) << ": ";
		cin >> months[index].rainfall;
	}
	// Display total rainfall.
	cout << "The total rainfall for the year is ";
	cout << months[index].rainfall
		 << " inches." << endl;

	// Display the average rainfall from the year
	cout << "The average rainfall for the year is ";
	float average = months[index].rainfall / 12;
	cout << months[index].rainfall << " inches." << endl;

	cin.get();
	return 0;
}

Please offer any advice that you can, I am stuck on this program and it's past due. I am not looking for it to be done for me, mainly just advice as I am struggling with this program for some reason.

Yes, you can modify that code for your purposes.
Keep in mind, however, your function prototypes suggest there will be NO return values (void) for your functions.

Hi ussra,
Please see the program as below. I have not used any of the functions and so I've commented them out. Please note that I have added a new variable, AverageRainfall and I've also initialized AnnualRainfall to 0.0. Hope this helps.

// This program collects the weather statistics for a year. The data it collects is total rainfall, high temp, low temp, average temp.
//Programming Challenge 11.4
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Weather
{
	double rainfall;
	double high_temp;
	double low_temp;
	double avg_temp;
};

// Function Prototypes
/*double getTotalrain(double);
double getAvgrain(double);
double getAverageTemp(double);
double getHighTemp(double);
double getLowTemp(double);*/


int main()
{
	const int NUM_MONTHS = 12;   //Number of Months
	Weather months[NUM_MONTHS];  //Array of 12 months
	int index;                   //Loop counter

	double annualRainfall=0.0,averageRainfall;


	// Get rainfall for each month.
	for (int index = 0; index < NUM_MONTHS; index++)
	{
		// Get months rainfall
		cout << "Enter the rainfall in inches for month #:";
		cout << (index + 1) << ": ";
		cin >> months[index].rainfall;
                cout<<months[index].rainfall;
                annualRainfall+=months[index].rainfall;
                averageRainfall=annualRainfall/12;

	}
	// Display total rainfall.
	cout << "The total rainfall for the year is ";
	cout << annualRainfall
		 << " inches." << endl;

	// Display the average rainfall from the year
	cout << "The average rainfall for the year is ";
	//float average = months[index].rainfall / 12;
	cout << averageRainfall << " inches." << endl;

	cin.get();
	return 0;
}

Edited 5 Years Ago by c-tech: n/a

Hi ussra,
Please see the program as below. I have not used any of the functions and so I've commented them out. Please note that I have added a new variable, AverageRainfall and I've also initialized AnnualRainfall to 0.0. Hope this helps.

// This program collects the weather statistics for a year. The data it collects is total rainfall, high temp, low temp, average temp.
//Programming Challenge 11.4
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Weather
{
	double rainfall;
	double high_temp;
	double low_temp;
	double avg_temp;
};

// Function Prototypes
/*double getTotalrain(double);
double getAvgrain(double);
double getAverageTemp(double);
double getHighTemp(double);
double getLowTemp(double);*/


int main()
{
	const int NUM_MONTHS = 12;   //Number of Months
	Weather months[NUM_MONTHS];  //Array of 12 months
	int index;                   //Loop counter

	double annualRainfall=0.0,averageRainfall;


	// Get rainfall for each month.
	for (int index = 0; index < NUM_MONTHS; index++)
	{
		// Get months rainfall
		cout << "Enter the rainfall in inches for month #:";
		cout << (index + 1) << ": ";
		cin >> months[index].rainfall;
                cout<<months[index].rainfall;
                annualRainfall+=months[index].rainfall;
                averageRainfall=annualRainfall/12;

	}
	// Display total rainfall.
	cout << "The total rainfall for the year is ";
	cout << annualRainfall
		 << " inches." << endl;

	// Display the average rainfall from the year
	cout << "The average rainfall for the year is ";
	//float average = months[index].rainfall / 12;
	cout << averageRainfall << " inches." << endl;

	cin.get();
	return 0;
}

Thanks for the reply, I will try it out. However, I think I have resolved this program.

do you really think he's still loitering here, just waiting for someone to come in and ask him to do their homework for them?

I seriously doubt it.

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