For this assignment, I am supposed to get a starting population, an annual death rate, an annual birth rate, and the number of years to display. I am then supposed to use these values in the formula n = p(1 + b)(1-d), where n is the projected population, b is the birth rate and d is the death rate. I can get my program to calculate the projected population, but I am having trouble getting the program to loop for the number of years that the user specifies. I am also having trouble getting the program to recall the value for the population so that the projected population reflects changes over time. Any help would be greatly appreciated!!

Here is what I have currently:

``````// Week 7 - Programming Challenge 15

#include <iostream>
using namespace std;

int toprojectedpop(int startingpop);

// Define variables.

int startingpop;    // Starting population.
int birthrate;      // Birth rate.
int deathrate;      // Death rate.
int numyears;       // Number of years to be displayed.
int projectedpop;   // Projected population.

int main()

{

// Get inputs.

cout << "Enter the starting population: \n";                      // Gert starting population.
cin >> startingpop;

while (startingpop < 2.0)                                            // Do not accept values less than 2.
{
cout << "ERROR! Starting population must be two or more. Please re-enter: ";
cin >> startingpop;
}

cout << "Enter the annual number of births: \n";                  // Get annual birth rate.
cin >> birthrate;

while (birthrate < 0)                                                // Do not accept negative values.
{
cout << "ERROR! Birth rate cannot be negative. Please re-enter: ";
cin >> birthrate;
}

cout << "Enter the annual number of deaths: \n";                  // Get annual death rate.
cin >> deathrate;

while (deathrate < 0)                                                // Do not accept negative values.
{
cout << "ERROR! Death rate cannot be negative. Please re-enter: ";
cin >> deathrate;
}

cout << "Enter the number of years to display: \n";                   // Get number of years to display.
cin  >> numyears;

while (numyears < 1)                                             // Do not accept values less than 1.
{
cout << "ERROR! Number of years must be one or more. Please re-enter: ";
cin >> numyears;
}

// Calculate the projected population.

projectedpop = startingpop*(1 + birthrate)*(1 - deathrate);

// Display projected population.

cout << "The projected population is " << projectedpop << ". \n";

cin.ignore();
cin.get();

return 0;

}

int toprojectedpop(int startingpop)

{
for (numyears = 1; numyears <= 1000; numyears++)

{
return(int) (startingpop*(1 + birthrate)*(1 - deathrate));
}

}
``````

Edited by pritaeas: Moved to C++

2
Contributors
1
15
Views
4 Years
Discussion Span
Last Post by Moschops

I see your loop has a return in it. This means that the loop ends the very first time. Put the return after the loop. You're also using the variable that tells you how many years to run for (the user input) as the variable telling you how many years you've done so far. Use a different variable.

Something like this:

``````int toprojectedpop(int startingpop)
{
int currentPopulation = startingpop;
for (int currentYear = 0; currentYear < numYears; currentYear++)
{
currentPopulation = currentPopulation*(1 + birthrate)*(1 - deathrate));
}
return currentPopulation;
}
``````

Edited by Moschops

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.