Hello,
i have wrote a program using the while loop but it crashes whenever i run it
Basically, the goal of a program is to calculate the population of a town with constant 10% growth annually and then find how many years left.
here is my code (any help will be appreciated)

//Program1
// Chapter 5.4

#include<iostream>

using namespace std;

int const INITPOP = 900;
int const FINALPOP = 20000;

// prototype

int main()
{
	int peopleCount;
	float annualPerc = .1;
	float yearsLeft,
		  annualGrowth;

	

	peopleCount = INITPOP;
	while (peopleCount < FINALPOP)
	{
		 annualGrowth = annualPerc * INITPOP;
		 peopleCount = annualGrowth + INITPOP;
		 yearsLeft = FINALPOP / peopleCount;
		 peopleCount += peopleCount;
		 cout << peopleCount << yearsLeft << endl;
	}
	
	system("pause");
	return 0;
	
}

try this:

while (peopleCount < FINALPOP)
	{
		 annualGrowth = annualPerc * INITPOP;
		 peopleCount += (int)annualGrowth; // + INITPOP;
		 yearsLeft = FINALPOP / (float)peopleCount;
		 // peopleCount += peopleCount;
		 cout << peopleCount << yearsLeft << endl;
	}

thank you
i will try it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.