need a point in the right direction

This is a discussion on need a point in the right direction within the C++ Programming forums, part of the General Programming Boards category; I'm trying to take the population of a town which increases by 10% each year and determine how long until ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    need a point in the right direction

    I'm trying to take the population of a town which increases by 10% each year and determine how long until population = 10,001.

    I had it working until I decided to put in the last cout statement that recalls the original input value of the population.

    Is there a way to do that?
    Thanks


    **Please note: I am not asking anyone to do my homework for me, I would just like a point in the right direction. Thanks.


    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    //Functions used ...(function prototypes)
    void instructions ();	
    int calculations ();
    int numYears;
    int population;
    int populationTotal;
    float rate;
    
    -----------------------------------------------
    
    int main ()
    {
                    instructions ();
    	calculations ();
    	
    
    	cout << endl;
    	return 0;
    }
    
    -----------------------------------------
    
    void instructions ()
    {
    	cout << "This program determines the number "
    	        << "of years it will take for a "
    	        << "town's population" 
    	        << "to reach more than 10,001 at a growth rate "
    	        << "of 10% per year." << endl << endl;
    }
    
    ------------------------------
    
    int calculations ()
    {
    	cout << "Please enter the current population: ";
    	cin >> population;
    
    	rate = 1.1;
    	
    	for (int numYears = 0; populationTotal <= 10001; numYears++)
    	{
    		populationTotal = populationTotal * rate;
    	}
    
    	populationTotal = population * 1;
    
    
    
    	cout << "With an initial population of "
    	        << population
    	        << ", it will take "
    	        << numYears
    	        << " years" << endl;
    	cout << "for the population to become "
    	        << "more than 10,001." << endl << endl;
    		return 0;
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well your program hangs on the for loop, you should initialize pupulationTotal to population, after you input it in that function. Also I would make rate a double instead of a float. This should make it work.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    First off, don't use global variables. Second, you didn't initialize populationTotal.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thanks guys, I'll try that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Point in the right direction
    By peanutym in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2008, 08:49 PM
  2. Point me in the right direction
    By vampje in forum C++ Programming
    Replies: 0
    Last Post: 06-07-2006, 03:52 AM
  3. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 08:26 AM
  4. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 11:59 AM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21