first post

hey guys, so i recently started taking c++ for school(6 weeks in class) and we were assigned a new project. i have 99% of it done, just keep getting stuck at the end, finally decided to sign up after lurking for a couple of weeks.

heres the problem: i have to design a program that will calculate world population growth for 75 years, output the current population for the year and the population change. at the end of those 75 lines of output, i have to have a single line outputting the year that the population doubled.

for the life of me, i cannot figure out how to get it to display the year that the population doubled. im going to attach my code if anyone can help me i would greatly appreciate it.


#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;
double popInit;
double rate;
int yearCounter = 1;
int doublePop;
static int thisisit;
double currentPop;
double rateCalc;
double popFinal;
double popChange;
double e = 2.718281828459045;
int main ()
{
cout<<"Enter current population\n";
cin>>popInit;
cout<<"Enter growth rate as a percentage\n";
cin>>rate;
cout<<endl;
cout<<"Year\tPopulation\tPopulation change\n"<<endl;

// while loop execution
currentPop = popInit;
doublePop = popInit *2;
while( yearCounter <= 75 )
{
rateCalc = rate / 100;
popFinal=pow(e,(rateCalc*yearCounter)) * popInit;
popChange = popFinal - popInit;
cout<<fixed<<yearCounter<<"\t"<<setprecision(0)<<fixed<<popFinal<<"\t"<<fixed<<popChange<<endl;
yearCounter++;
if (doublePop >= doublePop)
{
thisisit = yearCounter;
}



}
cout<<"Population doubled by year: "<<thisisit+1<<endl;
system("Pause");
return 0;
}

Your if-statement is incorrect.
if (doublePop >= doublePop)

You should be checking if the population change is greater/equal to the ( current population * 2 )

Also, when you change this, each population change will change the year it doubled. Maybe give 'thisisit' a default value and check that at the same time as a check for a double in population size.

if( ( your population check ) && ( thisisit != default value ) )

Hope this helps.
Last edited on
ahhh ok, so i changed my if statement to the following:

if (popChange >= popInit)
{
thisisit = yearCounter;
};

when the program runs for the year it was doubled i get an output of 76.

when u said set thisisit to a default value, to be honest not sure what u mean, like i know the process of assigning it a value, just dont understand the logic of what to assign and why. here is mu updated code.

#include <math.h>
#include <iomanip>

using namespace std;
double popInit;
double rate;
int yearCounter = 1;
int doublePop;
static int thisisit;
double currentPop;
double rateCalc;
double popFinal;
double popChange;
double e = 2.718281828459045;


int main ()

{
cout<<"Enter current population\n";
cin>>popInit;
cout<<"Enter growth rate as a percentage\n";
cin>>rate;
cout<<endl;
cout<<"Year\t Population\tPopulation change\n"<<endl;

// while loop execution
currentPop = popInit;
doublePop = popInit *2;
while( yearCounter <= 75 )
{
rateCalc = rate / 100;
popFinal=pow(e,(rateCalc*yearCounter)) * popInit;
popChange = popFinal - popInit;
cout<<fixed<<" "<<yearCounter<<"\t\t"<<setprecision(0)<<fixed<<popFinal<<"\t\t "<<fixed<<popChange<<endl;
yearCounter++;

if ((popFinal >= doublePop) && (thisisit != ))
{
thisisit = yearCounter;
}
}
cout<<"Population doubled by year: "<<thisisit<<endl;
system("Pause");
return 0;
};
Topic archived. No new replies allowed.