Thread: Something Eludes Me

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Something Eludes Me

    Alright, its a simple program to input 12 numbers in to an array corresponding to rain totals for each month and at the end it wants a total, an average, a high and a low.

    I'm not able to get the 'high' amount, when I run the program it just gives me whatever number I put in the array first.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	cout.setf(ios::fixed);
    	cout.setf(ios::showpoint);
    	cout.precision(2);
    	const int months = 12; // number of months
    	int rainamt[months];    // rain each month
    	int count;
    	int totalrain = 0;
    	int averagerain;
    	// Input the amount of rainfall for month
    	for (count = 0; count < months; count++)
    	{
    		cout << "Enter the total rainfall for month "
    			 << (count + 1) << ": ";
    		cin >> rainamt[count];
    	}
    	
    	{
    	// Display contents of the array
    	cout << "The rainfall amounts you entered are:";
    	for (count = 0; count < months; count++)
    		cout << " " << rainamt[count];
    	cout << endl;
    	}
    	{
            for (count = 0; count < months; count++)
    		totalrain += rainamt[count];
    		cout << "The total rainfall for the year is " << totalrain << " inches.";
    		cout << endl;
    	}
    	{
    		averagerain = totalrain / 12;
    		cout << "The average rainfall for the year is " << averagerain << " inches per month.";
    		cout << endl;
    	}
    	int highest;
    	int highcount;
    
    	highest = rainamt[0];
    	for (highcount = 1; highcount < months; highcount++)
    	{
    		if (rainamt[count] > highest)
    			highest = rainamt[count];
    	}
    		cout << "The highest rain amount in one month was " << highest << ".";
    		cout << endl;
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    I think I got it, nevermind

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    This is why I greatly prefer to put my variable declarations inside the for structure, unless I actually need the value to survive longer than the loop. E.g.:

    for (int count = 0; count < max; count++){
    }

    This causes count to stop existing (go out of scope) when the loop ends. At least if your compiler conforms to standards, it does. There's often no reason for the loop control variable to survive longer than the loop -- you never use count outside of a loop, so there's no reason for it to exist there.

    Had you done that (you'd need to redeclare count for every loop in which you use it) you'd have never had the problem -- you'd have gotten an undefined symbol error on the line that was wrong.

    I strongly recommend using scope to your advantage. My general rule of thumb is if I never need a variable beyond a certain segment of code, it shouldn't exist beyond that segment. Helps prevent late-night typos from hell, the kind that compile fine, run fine ninety percent of the time, and send everything to hell that last ten percent.
    Last edited by Cat; 08-21-2006 at 12:53 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed