Population program, loop issue (not calculating correctly)?

Hey guys. Everything is working great here except when the program outputs the calculations, the "newpopulation" outputs the same exact value as the original input for "population". For example, if you enter 85000, year 1 will be 85000, year 2 will be 85000, etc. Something isn't calculating or outputting correctly either in my function or my calculating. Thanks in advance.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <iomanip>
using namespace std;

double calculatepop(int, float, float);

int main()
{
	int years;
	int population;
	float birthrate;
	float deathrate;
	int newpopulation;

    cout << "What is the current population?\n";
	cin >> population;
    // Catches non-realistic input
	while(population < 2)
	{
		cout << "Please specify a number larger than 1.\n";
		cin >> population;
	}

	cout << "What is the annual birth rate? (percentage)\n";
	cin >> birthrate;
	// Catches invalid input
	while(birthrate < 0.0)
	{
		cout << "Please specify a non-negative percentage.\n";
		cin >> birthrate;
	}

	cout << "What is the annual death rate? (percentage)\n";
	cin >> deathrate;
	// Catches invalid input
	while(deathrate < 0.0)
	{
		cout << "Please specify a non-negative percentage.\n";
		cin >> birthrate;
	}

	cout << "For how many years will this increase take place?\n";
	cin >> years;
    // Catches invalid input
	while(years < 1)
	{
		cout << "Please specify a number larger than zero.\n";
		cin >> years;
	}

	cout << "" << endl;

	cout << "YEAR                   POPULATION\n";
	cout << "---------------------------------\n";
	for (double i = 1; i <= years; i++)
    {
    newpopulation = calculatepop(population, birthrate, deathrate);
    cout << i << "                    " << newpopulation << endl;
}

	return 0;
}

double calculatepop(int population, float birthrate, float deathrate){
    int newpopulation = population + (birthrate * .01) - (deathrate * .01);
    return newpopulation;
}
One thing that probably isn't causing you problem, but is still an issue is that calculate pop is a double, but your returning an int. Another thing, what are you inputting? If your birthrate and deathrate are the same, that could cause issues.
Last edited on
http://i61.tinypic.com/28uqszt.png

I don't think either of those are the issue. Even tried changing calculatepop to an int and still no luck. The above link is a screenshot.
Line 65 is wrong. The birth and death rate appear to be percentages. Therefore, the new population is equal to the difference between birthrate and death rate times the population.

And, you might as well return an int in that function.

The other problem is line 57. You calculate a new population, but you never assign the new population to the old population. This means that every loop using the original 8500 pop.
@mstru

I changed up your program to use recursion instead of the for loop, and changed the way it calculates. Not sure if the numbers are correct as a result, but they look like they are.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <iomanip>
using namespace std;

void calculatepop(int, float, float, int,int);

int main()
{
	int years;
	int population;
	float birthrate;
	float deathrate;
	int newpopulation = 0;

	cout << "What is the current population?\n";
	cin >> population;
	// Catches non-realistic input
	while (population < 2)
	{
		cout << "Please specify a number larger than 1.\n";
		cin >> population;
	}

	cout << "What is the annual birth rate? (percentage)\n";
	cin >> birthrate;
	// Catches invalid input
	while (birthrate < 0.0)
	{
		cout << "Please specify a non-negative percentage.\n";
		cin >> birthrate;
	}

	cout << "What is the annual death rate? (percentage)\n";
	cin >> deathrate;
	// Catches invalid input
	while (deathrate < 0.0)
	{
		cout << "Please specify a non-negative percentage.\n";
		cin >> birthrate;
	}

	cout << "For how many years will this increase take place?\n";
	cin >> years;
	// Catches invalid input
	while (years < 1)
	{
		cout << "Please specify a number larger than zero.\n";
		cin >> years;
	}

	cout << "" << endl;

	cout << "YEAR                   POPULATION\n";
	cout << "---------------------------------\n";
		calculatepop(population, birthrate, deathrate, years,1);
	return 0;
}
// Using recursion instead of fo loop
void calculatepop(int population, float birthrate, float deathrate, int years,int start)
{
	if (start > years)
		return;
	population += (population * (birthrate * .01)) - (population * deathrate * .01);
	cout << start << "                    " << population << endl;
	start++;
	calculatepop(population, birthrate, deathrate, years, start);
}
@whitenite1

I used your code and it seemed to work just fine. I think the big deal was how you calculated the population after your if statement. Thank you.
Yes, that was indeed the problem. Your line 63 worked much better with my for loop. It wasn't a syntax issue, it was just me forgetting how to do simple arithmetic. Thanks again.
Topic archived. No new replies allowed.