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