How do I fix this population program?

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));
}

}
I'm not sure I understand how you calculate the projected population. Are birthrate and deathrate real numbers (for example, birthrate of 10 means 10 people will be born by next year) or is it based on a percentage of the population (deathrate of 10 means for n amount of people, 10 will die) ?

EDIT:

I've given it a second thought, and I suggest you go for birthrate and deathrate values in percentage to create something dynamic. Try this ?

1
2
3
4
5
6
7
8
// Calculate the projected population.

birthrate = birthrate / 100;
deathrate = deathrate / 100;

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

startingpop = projectedpop;


Find a way to properly loop the last two lines by the amount of years previously entered and it could work. Also, change birthrate and deathrate to floats.
Last edited on
Topic archived. No new replies allowed.