Calculate New Population in C++

What is C++?
Last edited on
In #1:

A) your names are wayyyy to similar:
new_Population (the parameter in int main)
new_population (the function)
new_population (the local parameter in new_population()

B) You return 0 in your new_population function, you should return the result

C) You should set initial_population = new_Population so that it changes for each year

This should help:
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
# include <iostream>

int new_population (int, double, double) ;
using namespace std;

int main ()
{
	int initial_population, years, new_Population;
	double birth_rate, death_rate;

	cout << " Please enter the starting size of the population : " << endl;
	cin >> initial_population;
	cout << " Annual birth rate : " << endl;
	cin >> birth_rate;
	cout << " Annual death rate : " << endl;
	cin >> death_rate;
	cout << " Numbers of years : " << endl;
	cin >> years;

	for (int i = 0; i < years ; i ++ )
	{
		new_Population = new_population ( initial_population, birth_rate, death_rate) ;
		initial_population = new_Population; // Set this for the next iteration
	}

}

int new_population ( int initial_population, double birth_rate, double death_rate)
{
	// Changed the name of the local variable here
	int output = initial_population + birth_rate - death_rate;

	cout << " New population is " << output << endl;
	return output; // No longer returning 0;
}
For #2:

You never set the computer's choice. You use an uninitialized variable meaning that the computer may as well have chosen a rubber duck.

add this to give the computer a random number between 0 and 2 (inclusive):
computer_choice = rand()%3;
You'll need to include <cstdlib> for this.

Finally, to get full marks, I suspect that you'll have to select a winner. That's easy to do since you already have tons of else-if statements comparing the computer to the player. Just stick it in there.
^^ thank you so much Stewbond.
Topic archived. No new replies allowed.