Problem with the logic in my function. All other aspects of the program are working. I think it may be the FOR loop but it is accumulating the running total just fine but it is failing to read the high/low temps passed in which seems to be a logic error, just unsure how to fix it.
The main function only calls this function and one to set the data, I can verify each array element has the structure data independently
The function call from within this function is to a simple switch stament which also works.
lowTemp is set to one more than the allowed maximum temperature
highTemp is set to one lower that the allowed minimum temperature
/********************************************************************
* Definition of function calcData
* Recieve an array of structures than calculate high temp for the
* year, low temp for the year, total rainfall and average rainfall
//START FUNCTION
//FOR ( Each structure in the array )
// Add rainfall to total
// IF (High temp at count is greater than existing high temp )
// Store new high temp and count number
// IF (Low temp at count is less than existing low temp )
// Store new low temp and count number
//END FOR
//Calculate and store average
//Display findings
//END FUNCTION
********************************************************************/
void calcData ( WeatherData array1[], int size )
{
//Constnats
for ( int i = 0 ; i < size ; i++ )
{
//Running total for rain
totalRain += array1[i].totalRain ;
//Determine years high temp
if ( array1[i].highTemp > highTemp )
{
highTemp = array1[i].highTemp ;
highCount = i ;
}
//Determine years low temp
if ( array1[i].lowTemp < lowTemp )
{
lowTemp = array1[i].lowTemp ;
lowCount = i ;
}
}//END FOR
//Calculate average
avrgRain = totalRain/size ;
//Display the findings
cout << " The total rainfall for the year in inches was : " << totalRain << endl ;
cout << " The Average rainfall for the year in inches : " << avrgRain << endl ;
cout << " " ;
showMonth(highCount) ;
cout << " had the highest temerature of : " << highTemp << endl ;
cout << " " ;
showMonth(lowCount) ;
cout << " had the lowest temperature of : " << lowTemp << endl ;
cout << endl << endl ;
My appologies, thought about posting the rest after I was already in the car.
/********************************************************************
* Date : November 18, 2011
* Problem : 11.4 Weather Stats
* Description : Create a weater data structure to hold weather data
* of total rainfall, high temp, low temp, average temp for 12 months
* Display total rain for the year, average for the year and low and
* high temp and which month they occured in
//START PROGRAM
//Call function for weather data entry
//Call function for calculations and display
//END PROGRAM
********************************************************************/
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//Function Prototypes
void setData ( WeatherData [] , int ) ;
void showMonth ( int ) ;
void calcData ( WeatherData [] , int ) ;
int _tmain(int argc, _TCHAR* argv[])
{
//Constants
const int MONTHS = 12 ;
//Variables
WeatherData info[MONTHS];
//START PROGRAM
//To recieve info
setData ( info, MONTHS ) ;
//To calculate and display info
calcData ( info, MONTHS ) ;
//END PROGRAM
return 0;
}
/********************************************************************
* Definition of function calcData
* Recieve an array of structures than calculate high temp for the
* year, low temp for the year, total rainfall and average rainfall
//START FUNCTION
//FOR ( Each structure in the array )
// Add rainfall to total
// IF (High temp at count is greater than existing high temp )
// Store new high temp and count number
// IF (Low temp at count is less than existing low temp )
// Store new low temp and count number
//END FOR
//Calculate and store average
//Display findings
//END FUNCTION
********************************************************************/
void calcData ( WeatherData array1[], int size )
{
//Constnats
for ( int i = 0 ; i < size ; i++ )
{
//Running total for rain
totalRain += array1[i].totalRain ;
//Determine years high temp
if ( array1[i].highTemp > highTemp )
{
cout << array1[i].highTemp ;
highTemp = array1[i].highTemp ;
highCount = i ;
}
//Determine years low temp
if ( array1[i].lowTemp < lowTemp )
{
lowTemp = array1[i].lowTemp ;
lowCount = i ;
}
}//END FOR
//Calculate average
avrgRain = totalRain/size ;
//Display the findings
cout << " The total rainfall for the year in inches was : " << totalRain << endl ;
cout << " The Average rainfall for the year in inches : " << avrgRain << endl ;
cout << " " ;
showMonth(highCount) ;
cout << " had the highest temerature of : " << highTemp << endl ;
cout << " " ;
showMonth(lowCount) ;
cout << " had the lowest temperature of : " << lowTemp << endl ;
cout << endl << endl ;
//END FUNCTION
}
/********************************************************************
* Definition of function setData
* Recieve an array of structures and populate the fields
//START FUNCTION
//FOR ( Each element in the array
// WHILE ( Rainfall if less than zero )
// Store Rainfall
// WHILE ( Temp is above max )
// Store High Temp
// While ( Temp is below min )
// Store Low Temp
// Calculate and store Average temp
//END FOR
//END FUNCTION
*********************************************************************/
void setData ( WeatherData array1[], int size )
{
//Constants
const int MAX_TEMP = 140 ;
const int MIN_TEMP = -100 ;
//Variables
//START FUNCTION
for (int i = 0 ; i < size ; i++ )
{
cout << " Please enter total rain for month of " ;
showMonth(i) ;
cout << ": " ;
cin >> array1[i].totalRain ;
cout << endl ;
//Verify zero or higher rain fall amount
while ( array1[i].totalRain < 0 )
{
cout << " Sorry, no such thing as negative rain fall. " << endl ;
cout << " Please enter total rain for month of " ;
showMonth(i) ;
cout << ": " ;
cin >> array1[i].totalRain ;
cout << endl ;
}
cout << " Please enter high temperature for month of " ;
showMonth(i) ;
cout << ": " ;
cin >> array1[i].highTemp ;
cout << endl ;
//Verify High temp range
while ( array1[i].highTemp > MAX_TEMP )
{
cout << " Sorry high temp can not be greater than " << MAX_TEMP << endl ;
cout << " Please enter high temperature for month of " ;
showMonth(i) ;
cout << ": " ;
cin >> array1[i].highTemp ;
cout << endl ;
}
cout << " Please enter low temperature for month of " ;
showMonth(i) ;
cout << ": " ;
cin >> array1[i].highTemp ;
cout << endl ;
//Verify low temp range
while ( array1[i].lowTemp > MIN_TEMP )
{
cout << " Sorry low temp must be greater than " << MIN_TEMP << endl ;
cout << " Please enter high temperature for month of " ;
showMonth(i) ;
cout << ": " ;
cin >> array1[i].highTemp ;
cout << endl ;
}
//Calculate and store average temp
array1[i].avrgTemp = (array1[i].highTemp + array1[i].lowTemp)/2 ;
}//End For
//END FUNCTION
}
/********************************************************************
* Defintion of function showMonth
* Recieve a number in a range display appropriate case from a switch
//IF (Number is in range )
// SWITCH (num)
// Display case that matches num
// END SWITCH
//END IF
//END FUNCTION
********************************************************************/
void showMonth ( int num )
{
//Constants
const int MIN = 1 ;//Verification for 12 months in a year
const int MAX = 12 ;