Growth Rate and Loops.....HELP

Hi I'm having a problem with my program and have no one to ask. The question is "How many bunnies will I have after this many years?"
We are given a formula that is Bnew=Bold+Bold*growthRate+migrationRate
Bnew=# of bunnies at end of year
Bold=# of bunnies at beginning of year

where growthrate=birthRate-deathRate
and miigartionRate=immigrationRate-EmigrationRate

I cant get it to to loop if i ask it for 5 years, it will just give me a year i'm stuck and need help. Thanks



#include <iostream>
using namespace std;

int main()
{

double thershold,oPercentage,BirthRate,DeathRate;
double growthRate,migrationRate;
int population,years, initial,ImmigrationRate,EmigrationRate;
char another;


cout<<"To predict bunny population growth."<<endl<<endl;

cout<<"Please pick which program you would like to use,";
cout<<" but first enter Threshold population";
cout<<" and Overpopulation Percentage, Thanks."<<endl<<endl;


do
{
cout<<"What is the Overpopulation Threshold?"<<endl<<endl;
cin>>thershold;
if(thershold<=0)
cout<<endl<<"Please enter a positive number"<<endl<<endl;
}
while(thershold<=0);


do
{
cout<<"What is the Overpopulation Percentage?"<<endl<<endl;
cin>>oPercentage;
if(oPercentage>1)
cout<<endl<<"Number must be less than or equal to 1."<<endl<<endl;
}
while(oPercentage>1);



do
{
do
{
cout<<"What is the Initial Population?";
cout<<"(How many bunnies we start with)"<<endl<<endl;
cin>>initial;
if(initial<2)
cout<<endl<<"Please enter a number great or equal to 2."<<endl<<endl;
}
while(initial<2);


do
{
cout<<"What is the Birth Rate per Bunny?";
cout<<"(percentage of baby bunnies per bunny per year,";
cout<<"ex:0.10=10%)"<<endl<<endl;
cin>>BirthRate;
if(BirthRate<0)
cout<<endl<<"Please enter any non-negative number."<<endl<<endl;
}
while(BirthRate<0);

do
{
cout<<"What is the Death Rate per Bunny?";
cout<<"(percentage of dead bunnies per bunny per year)"<<endl<<endl;
cin>>DeathRate;
if(DeathRate>=1)
cout<<endl<<"Please enter any non-negeative number"<<endl<<endl;
}
while(DeathRate>=1);


do
{
cout<<"What is the Immigration Rate bunnies per year?";
cout<<"(each year, on average that sneak in)"<<endl<<endl;
cin>>ImmigrationRate;
if(ImmigrationRate<0)
cout<<endl<<"Please enter any non-negative number."<<endl<<endl;
}
while(ImmigrationRate<0);


do
{
cout<<"What is the Emigration Rate per Bunny/year?";
cout<<"(each year, on avaerage that sneak out)"<<endl<<endl;
cin>>EmigrationRate;
if(EmigrationRate<0)
cout<<endl<<"Please enter any non-negative number."<<endl<<endl;
}
while(EmigrationRate<0);






do
{
cout<<"How many bunnies after...years?"<<endl<<endl;
cin>>years;
if(years<0)
cout<<endl<<"Please enter a poitive number."<<endl<<endl;
}
while(years<0);

growthRate=BirthRate-DeathRate;//(new bunnies per bunny per year)
migrationRate=ImmigrationRate-EmigrationRate;//(bunnies per year)




for(int year=0;year<=years;++year)
{
cout<<"Population="<<population<<endl;
population=population+population*growthRate+migrationRate;
}


do
{
cout<<endl<<"Do another calculation (y/n)?";
cin>>another;
if(another !='y'&& another!='n')
cout<<endl<<"Please enter 'y' or 'n'"<<endl<<endl;
}
while (another!='y'&& another!='n');
}
while (another == 'y');


cout<<endl<<"Have a nice day"<<endl;
return 0;
}
Last edited on
1
2
bold=initial;
population=bold+bold*growthRate+migrationRate;

You aren't using any loops here, so it will just run once.
yes i forgot to input that in, i assume i would use a for loop for that portion would it be

for(population=0;population<=years;++population)
{
cout<<"Population="<<population<<endl;
population=population+population*growthRate+migrationRate;
}
cout<<"Population="<<population<<endl;


i tried this a a few others but it keeps going and doesnt stop giving me populations. How would i write it
Don't use "population" as your loop index.

for(int year=0;year<=years;++year)
{
cout<<"Population="<<population<<endl;
population=population+population*growthRate+migrationRate;
}
thanks toum, I'm having a problem with population it doesnt seem to grow from year to year. I been messing with it for awhile. I know it has something to do with population. They original formula is:


Bnew=Bold+Bold*growthRate+migrationRate

Bnew=# of bunnies at end of year
Bold=# of bunnies at beginning of year

but how would i incorporate that into program. Thanks
Topic archived. No new replies allowed.