Reputation: 55
For a class assignment we have to do a "population estimator", we use putty to store and compile our projects so the file created by this program is there.
Here is the specifics of the assignment.
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.
My program when run does create the file but nothing is in it when the program finishes. The for loop doesn't increment the years value and the population number stays the same, here is my code. Hope I can get some help.
#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;
int year = 0;
fstream file("population.txt");
for (int counter = 1; counter <= yrs; counter++)
{
year + 1;
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;
}
Upvotes: 1
Views: 62
Reputation: 881653
Statements like:
year + 1;
n * year;
are perfectly valid C or C++ but their effect is to calculate the values and then throw them away. As an aside, these are little different to the (also perfectly valid) 42;
.
Assuming you want to change those variables, you should be using something like (the comments to the right are shorthand):
year = year + 1; // ++year;
n = n * year; // n *= year
The reason you're file is empty is because you're not writing anything to it. Examine all the statements you use for file I/O:
fstream File;
fstream file("population.txt");
File << blah blah blah;
File.close();
You can see that the File
file (upper-case F
) uses the default constructor, meaning it's not attached to any actual file, and this is the one you're attempting to write to.
Your lower-case file
file is the one you attach to population.txt
but you never actually write anything to it.
You should choose one file handle and stick to it :-)
One other problems: your first while
loop should probably be using p
rather than pp
(as should the initial assignment to n
). It's meant to be checking the initial population, not the as-yet-unassigned previous population.
Additionally, that while
loop should be using <
rather than <=
since the latter means that two is considered invalid even though the specifications state "starting size of a population (minimum 2)".
Upvotes: 2