• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Simple Weather Program

Status
Not open for further replies.
Level 16
Joined
Mar 27, 2011
Messages
1,349
Good evening. Learning C++ here and I'm stuck trying to make a program. The program is
suppose to ask for weather data for each month for a year, then display that information
back at the user using structure arrays and enum variables.

When entering a number, the program reports to the user that these values are invalid.
Entering the same number a second time works for some reason. Anyone know whats
going on?

Code:
// Chapter 11 PCs Q4 - Weather Stats.cpp : Defines the entry point for the console application.
//  Chapter 11 Programming Challenges, Question 4 - Weather Statistics. Page 646.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

// Structure to store info about each month
struct MonthStats
{
	int rainfall;
	int temphigh;
	int templow;
	int tempaverage;
};

// Enum variable to store the months in the year.
enum Month {January, Febuary, March, April, May, June,
	July, August, September, October, November, December };

// Function prototypes
void DisplayMonthName(int);
void ValidateTemperature(int *);

int _tmain(int argc, _TCHAR* argv[])
{
	MonthStats months[12];
	int index; // To keep track of each loop's iteration
        int annualavtemp; // To store the annual average temperature
	int annualavtempacc = 0; // Keeps track of monthly temps. Needs
	                         // to be set (initialized) to 0 before it 
	                         // can accumalate

	cout << "This program accepts a years worth of weather data\n";
	cout << "and displays the information.\n";

	for (index = 0; index < December; index ++)
	{
		cout << endl;
	    DisplayMonthName(index);
		cout << endl << endl;
		cout << "Enter the monthly rainfall: ";
		// Rainfall recorded for the month
		cin >> months[index].rainfall;
		cin.ignore();
		cout << "Enter the month's lowest temperature: ";
		// Lowtemp recorded for the month
		cin >> months[index].templow;
		cin.ignore();
		// The temperature is checked to be within acceptable parameters
		ValidateTemperature(&months[index].templow);
		cout << "Enter the month's highest temperature: ";
		cin >> months[index].temphigh;
		cin.ignore();
		ValidateTemperature(&months[index].temphigh);
		// Calculates the average temperature for the month
		months[index].tempaverage = (months[index].templow +
			months[index].temphigh) / 2;
		// Keeps count of the monthly temps to use later for annual average
		annualavtempacc = annualavtempacc + months[index].tempaverage;
	}

	cout << endl << endl;
	cout << "----------------------------\n\n";
	cout << "Here is the data you entered!";
	cout << fixed << showpoint << setprecision(2);

	for (index = 0; index < December; index ++)
	{
		DisplayMonthName(index);
		cout << endl << "Total Rainfall: ";
		cout << months[index].rainfall;
		cout << endl << "Highest Temperature: ";
		cout << months[index].temphigh;
		cout << endl << "Lowest Temperature: ";
		cout << months[index].templow;
		cout << endl << "Average Temperature: ";
		cout << months[index].tempaverage;
		cout << endl;
	}

	annualavtemp = annualavtempacc / 12;
	cout << endl << "Average annual temperature: ";
	cout << annualavtemp;

	return 0;
}

// Function checks the temperature to be within an acceptable range.
// Asks for a new temperature until a valid temperature is given.
void ValidateTemperature (int *temp)
{
	while (*temp < -100 || *temp > 140);
	{
		cout << "Enter a temperature between -100 and 140: ";
		cin >> *temp;
	} 
}

// Function used to display the name of each month
void DisplayMonthName(int Month)
{
	switch(Month)
	{
	    case 0 : cout << "January";
			break;
		case 1 : cout << "Febuary";
			break;
		case 2 : cout << "March";
			break;
		case 3 : cout << "April";
			break;
		case 4 : cout << "May";
			break;
		case 5 : cout << "June";
			break;
		case 6 : cout << "July";
			break;
		case 7 : cout << "August";
			break;
		case 8 : cout << "September";
			break;
		case 9 : cout << "October";
			break;
		case 10 : cout << "November";
			break;
		case 11 : cout << "December";
			break;
	}
}
 
Last edited:
Status
Not open for further replies.
Top