Thread: beginner loop help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25

    beginner loop help

    this is the code i have for my program. i'm having trouble keeping the output in integer format since i'm multiplying by floating point numbers. i'm also having a problem with the last output, mine keeps coming up as 281. if someone could help me out that'd be great.

    our output should look like this:

    How many jackalopes do you have? 200
    How many generations do you want to wait? 1
    If you start with 200 jackalopes and wait 1 generations,
    you'll end up with a total of 204 of them.

    How many jackalopes do you have? 132
    How many generations do you want to wait? 2
    If you start with 132 jackalopes and wait 2 generations,
    you'll end up with a total of 137 of them.

    How many jackalopes do you have? 40
    How many generations do you want to wait? 100
    If you start with 40 jackalopes and wait 100 generations,
    you'll end up with a total of 291 of them.


    Code:
    // This program calculates population growth using a loop. 
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    
      int jacks,   // initial number of jackalopes
          gens;    // number of generations 
    
      const float birthRate = .03,   // birth rate
    	deathRate = .01;   // death rate
    
      // Ask user initial jackalope population.
      cout << "How many jackalopes do you have? \n";
      cin >> jacks;
    
      // Ask user how many generations will be calculated.
      cout << "How many generations do you want to wait? \n";
      cin >> gens;
    
      int newPop,    // population of jackalopes including births 
          newPop2;   // population of jackalopes including deaths
      newPop = jacks; 
    
      cout << showpoint << fixed << setprecision(2);   
    
      for (int i = 1; i <= gens; i++)
      { 
        newPop = (newPop * birthRate) + newPop;  // calculate new population of jackalopes
        newPop2 = newPop - (newPop * deathRate);  // calculate how many jackalopes die
        newPop = newPop2;  // total population
      }
    
      cout << "If you start with "<< jacks <<" jackalopes and wait "<< gens <<" generation(s), \n";  
      cout << "you'll end up with a total of "<< newPop2 <<" of them. \n";
    
      return 0;
    
    }
    Last edited by helloalyssa; 10-25-2010 at 12:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM