Help with while loop please

Write a C++ program using the while loop to calculate when the population ceases breeding and thus dies out.You do not have to input any values.

birth rate = 5%
death rate = 17%
starting population = 1500
population threshold = 165

I'm having trouble looping the program so that it can continue on after year one. The output is supposed to look like this:

YEAR ALIVE AT START NEW BIRTHS KILLED ALIVE AT END
1 1500 75 267 1308
2 1308 65 233 1140
3 1140 57 203 994

....and so on until it reaches 165 for the population. I have already figured out the formulas and I have the year 1 all set. My problem is my program keeps looping an infinite number of years with the data for year one repeated, instead of taking the population at the end of the year and starting the new year with it. Any help will be greatly appreciated.

[code]
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;

int main()
{
//declare variables
int year;
int population;
int startPopulation;
int newBirths;
int killed;
int newPopulation;
const int birthPercentage = 5;
const int killPercentage = 17;
const int threshold = 165;
int aliveatend;
int begin;

year = 1;
startPopulation = 1500;
population = startPopulation;

cout << setfill(' ') << setw(1) << "|" << setw(5) << left << ("YEAR") << setw(1) << "|" << setw(15) << left << "ALIVE AT START" << setw(1) << "|" << setw(10) << left << "NEW BIRTHS" << setw(1) << "|" << setw(5) << left << "KILLED" << setw(1) << "|" << setw(5) << left << "ALIVE AT END" << setw(1) << "|" << endl;

while (population >= 165)
{
newBirths = ((birthPercentage * population) / 100);
newPopulation = population + newBirths;
killed = ((killPercentage * newPopulation) / 100);
aliveatend = newPopulation - killed;
cout << setfill(' ') << setw(3) << "|" << setw(3) << left << year << setw(6) << "|" << setw(10) << left << startPopulation << setw(5) << "|" << setw(6) << left << newBirths << setw(2) << "|" << setw(5) << left << killed << setw(5) << "|" << setw(8) << left << aliveatend << setw(1) << "|" << endl;
year++;
}

return 0;
}
Last edited on
population doesn't change inside your loop. Why?
that's the thing i'm confused on with how to correct it. what would I change inside my loop to fix it?
I did the program to see if i could do it because im starting too, and did this:

Inside the loop i had to put the number of birth and dead rate because if i put birth/dead_rate the loop went crazy non-stop dont know why.

I dont know what your setw in your cout does, didnt know about that function or whatever that is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main() {
	int population = 1500;
	int birth_rate = 0.05;
	int death_rate = 0.17;
	int birth, dead, alive;
	int year=1;

	cout << "Year " << "Alive at start " << "New Births ";
	cout << "Killed " << "Alive At The End" << endl;

	while( population >= 165 ) {

		birth = population * 0.05;
		dead = population * 0.17;
		alive = population + birth - dead;
		cout << year << " " << population << " " << birth;
		cout << " " << dead << " " << alive << endl;
		++year;
		population = alive;
	}
}
		
		


Is it better to write different int or it is better to write them more compact?
Last edited on
closed account (E0p9LyTq)
Using integer math will not work as you expect, it truncates (chops off) any fractional part. birth_rate and death_rate are effectively zero.

When dealing with fractions use float or double. Best to use double for fractional precision.
yap same mistake i did before, thanks. sbang wrote const int <name> = 17 (for example).

Adding const makes it capable of fractional precision? Or because its 17 and not 17.x or x.17 it works?

(If i'm spamming the sbang post, sorry about that, i stop)
thank you! you did help. I just tweaked the formula for dead to get the numbers I wanted. this is the end result I got. btw, setw sets the width between the numbers for when you're making a chart.

[/code]
#include <iostream>
using namespace std;

int main() {
int population = 1500;
int birth_rate = 0.05;
int death_rate = 0.17;
int birth, dead, alive;
int year=1;

cout << "Year " << "Alive at start " << "New Births ";
cout << "Killed " << "Alive At The End" << endl;

while( population >= 165 ) {

birth = population * 0.05;
dead = ((population + birth) * 0.17);
alive = population + birth - dead;
cout << year << " " << population << " " << birth;
cout << " " << dead << " " << alive << endl;
++year;
population = alive;
}
}
As someone said before, to use birth_rate and dead_rate and not having to put 0.05 and 0.17, you should double them and not int. But as did before it is ok too. And thanks about that thing of set, now i understand what that does, but i will search for a more efficient way to use it, more cleaner if there is one.
Last edited on
Topic archived. No new replies allowed.