Growth and Rate w/ Loops

Hi all if anybody can help I'll appreciate it, I have to do a program for class with growth and loops. How many bunnies will i have after (blank) years, and the 2nd part how many years will I have to wait until desired population. The first part of it works but I'm having problems with the second part, its suppose to be once i reached my desired population give me the year that happen but either I'm not getting the year or when i adjusted the program it will give me multiple years.(its at the bottom of the program near if you want to run the program again). Thanks




#include <iostream>
using namespace std;

int main()
{

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


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;


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


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



do
{

cout<<"(a)How many bunnies will I have after this many years?"<<endl;
cout<<"(b)How many years will I have to wait to get this many bunnies?"<<endl;
cin>>option;
while(option !='a' && option!='b')

{
cout<<endl<<"Please enter 'a' or 'b'"<<endl<<endl;
cin>>option;
}


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

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;
while(BirthRate<0)
{
cout<<endl<<"Please enter any non-negative number."<<endl<<endl;
cin >> BirthRate;
}

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


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

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

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


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

cout<<"Would you like to see it as a table?"<<endl;
cin>>data;
if(data=='y')
{cout<<"Years Population\n"
<<"----- ----------\n";

for(int year=0, population=initial;year<=years;++year,
population=population+population*growthRate+migrationRate)
{
if(population>thershold)
{
population=population-population*oPercentage;}
cout<<" "<<year, cout<<" "<<population<<endl;}}

else
{
for(int year=0, population=initial;year<=years;++year,
population=population+population*growthRate+migrationRate)
{
if(population>thershold)
{
population=population-population*oPercentage;}
cout<<"Year="<<year<<endl;
cout<<"Population="<<population<<endl<<endl;}}

}

else
{
cout<<"How long do I have to wait till populaiton reaches (input population)?"<<endl;
cin>>populationDesired;
while(populationDesired<initial||populationDesired>thershold)
{
cout<<"Goal Population must be greater than or equal to initial populaiton"
"and less than or equal to overpopulation threshold"<<endl;
cin>>populationDesired;}

for(int year=0, population=initial;year<=years;++year,
population=population+population*growthRate+migrationRate)
{

if(population>=thershold)
cout<<"Year(s):"<<year<<endl;}}



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

}
while (another == 'y');

cout<<endl<<"Have a nice day"<<endl;
return 0;
}
Please put your code inside the code tags ([ code ][ /code ] without the spaces).

I think the problem is with your loop conditions. The for loop will keep going until year > years. Your loop only outputs something when population is greater than or equal to the overpopulation threshold.

When there is no output, that means that year is greater than years, but the population has not reached the overpopulation threshold.

When there are multiple lines of output, it means the population has reached the threshold, but year < years.

Also, I think that this condition is wrong, because you seem to only assign a value to years in option a. The variable years should not be used anywhere in option b.
I used this I'm getting the answer but then alot more after that how would i just ask for the first one?

for(int year=0, population=initial;;++year,
population=population+population*growthRate+migrationRate)
{

if(population>thershold)
{population=population-population*oPercentage;}
if(population>=populationDesired)
cout<<" "<<year, cout<<" "<<population<<endl;}
You need an end condition for your loop. Really, I would have written your loop like this:

1
2
3
4
5
6
7
8
9
10
for(int year = 0, population = initial; population < populationDesired; year++)
{
	population += (population * growthRate) + migrationRate
	
	if (population > thershold)
		population -= population * oPercentage;
	
	if (population >= populationDesired)
		cout<<" "<< year << " "<< population << endl;
}


I am not quite sure of the logic though. For example, if the population was 999 and the threshold was 1000, with growthRate and oPercentage equal, and 0 migration, this would be an infinite loop.
Thanks fg109, your right i look over the instructions and certain ones cant be equal to 0, thanks for the extra set of eyes.

Also the condition you wrote work with the example inputs from the homework assignment, but it seem to be off by a year, the population was right but the year was less by one
I guess that would be because year is initialized as 0 instead of 1.
thanks fg109 for your help, I appreciate it
Topic archived. No new replies allowed.