Population estimator, for loop not adding to variable and file is empty

For a class assignmet I have to make a population estimator. Here are the specifics:

In a population, the birth rate is the percentage increase of the population due to births, and the death rate is the percentage decrease of the population due to deaths. Write a program that asks for the following:

The starting size of a population (minimum 2)
The annual birth rate
The annual death rate
The number of years to display (minimum 1)

The program should then display the starting population and the projected population at the end of each year in screen and in a file. It should use a function that calculates and returns the projected new size of the population after a year. The formula is N=P(1+B)(1−D) where:

N is the new population size,
P is the previous population size,
B is the birth rate,
and D is the death rate.

Annual birth rate and death rate are the typical number of births and deaths in a year per 1,000 people, expressed as a decimal. So, for example, if there are normally about 32 births and 26 deaths per 1,000 people in a given population, the birth rate would be .032 and the death rate would be .026.

When I run my program, the year variable doesn't seem to get the + 1 added on each loop, and the file is created but nothing is in it, the line that displays the year # and estimated population by the end of the year also repeats to the number of years.

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
68
69
70
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	//N is the new population size.
	//B is the birth rate.
	//D is the death rate.
	//P is the previous population size;
	
	fstream File;

	double b, d;
	int p, pp, n, yrs;
	
	cout << "Population Estimator" << endl;
	
	cout << "Enter the starting size of the population: ";
	cin >> p;
	cout << endl;
	
	while (pp <= 2) 
	{
		cout << "Population must be 2 or more, enter again: ";
		cin >> pp;
		cout << endl;
    }
	
	cout << "Enter the Annual Birth Rate: ";
	cin >> b;
	b = b * p/1000;
	cout << endl;
	
	cout << "Enter the Annual Death Rate: ";
	cin >> d;
	d = d * p/1000;
	cout << endl;
	
	cout << "Enter the number of Years: ";
	cin >> yrs;
	cout << endl;
	
	while (yrs <= 1) 
	{
		cout << "Years must be 1 or more, enter again: ";
		cin >> yrs;
		cout << endl;
    }
	
	n = pp * 1+b * 1-d;
	
	fstream file("population.txt");
	for (int counter = 1; counter <= yrs; counter++)
	{
		int year;
		year + 1;
		n = pp * 1+b * 1-d;
		n * year;
		File << "Year # " << year << " the population was at: " << pp << " and will be at: " << n << " by the end of the year." << endl;
		cout << "Year # " << year << " the population was at: " << pp << " and will be at: " << n << " by the end of the year." << endl;
		pp = n;
	}
	
	File.close();
	
	return 0;
}
I fixed a couple of things here and there. Some syntax errors and some redundant information I found. Now, the year count is working, the values I don't know (the ones about population). But now, it compiles and it writes everything into the file. Any wrong calculation is up to you to fix. Any questions just ask.

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
68
69

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	//N is the new population size.
	//B is the birth rate.
	//D is the death rate.
	//P is the previous population size;
	
	double b, d;
	int p, n, yrs;
	
	cout << "Population Estimator" << endl;
	
	cout << "Enter the starting size of the population: ";
	cin >> p;
	cout << endl;
	
	while (p <= 2) 
	{
		cout << "Population must be 2 or more, enter again: ";
		cin >> p;
		cout << endl;
    }
	
	cout << "Enter the Annual Birth Rate: ";
	cin >> b;
	b = b * p/1000;
	cout << endl;
	
	cout << "Enter the Annual Death Rate: ";
	cin >> d;
	d = d * p/1000;
	cout << endl;
	
	cout << "Enter the number of Years: ";
	cin >> yrs;
	cout << endl;
	
	while (yrs <= 1) 
	{
		cout << "Years must be 1 or more, enter again: ";
		cin >> yrs;
		cout << endl;
    }
	
	n = p * 1+b * 1-d;
	
	ofstream file("population.txt");
	for (int counter = 1; counter <= yrs; counter++)
	{
		int year = 0;
		year++;
		n = p * 1+b * 1-d;
		n *= year;
		file << "Year # " << counter << " the population was at: " << p << " and will be at: " << n << " by the end of the year." << endl;
		cout << "Year # " << counter << " the population was at: " << p << " and will be at: " << n << " by the end of the year." << endl;
		p = n;
	}
	
	file.close();
	
	return 0;
}


And if it solved your problem, remember to mark your question as solved. Thanks!
Last edited on
Yes, this helped me, I found out that I had File instead of file inside the for loop, that is why my file was empty.
Topic archived. No new replies allowed.