population help

closed account (i87iNwbp)
cannot figure out what is wrong with the program btw I have to run on the linux server if that makes a difference

In a population, the birth rate and death rate are calculated as follows:
Birth Rate = Number of Births ÷ Population Death Rate = Number of Deaths ÷ Population For example, in a population of 100,000 that has 8,000 births and 6,000 deaths per year, Birth Rate = 8,000 ÷ 100,000 = 0.08 Death Rate = 6,000 ÷ 100,000 = 0.06 Design a Population class that stores a current population, annual number of births, and annual number of deaths for some geographic area. The class should allow these three values to be set in two ways: by passing arguments to a three-parameter constructor when a new Population object is created or by calling the setPopulation, setBirths, andsetDeaths class member functions. The class should also have getBirthRate and getDeathRate
functions that compute and return the birth and death rates. Your program shouldproperly use pointer data types where appropriate. Write aprogram that uses the Population class and illustrates its capabilities. Input Validation: If a population figure less than 2 is passed to the class, use a default value of 2. Population must be greater than zero. If a birth or death figure less than 0 is passed to the class, prompt user to type in a positive

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  #include <iostream>
#include <iomanip>
using namespace std;

	 
class Pop

{

private:

int population;
int births;
int deaths;

	 

public:
void setPopulation (int);
void setBirths(int);
void setDeaths(int);

int getPopulation();
double getBirthRate();
double getDeathRate();

	 

Pop() : population(0), births(0), deaths(0) {}
};

void Pop::setPopulation(int p)
{
	population = p;
}

void Pop::setBirths(int B)/>

{
	births = b;
}

void Pop::setDeaths(int d)
{
	 deaths = d;
}

int Pop::getPopulation()
{
	return population;
}

double Pop::getBirthRate()
{
	return births / static_cast<double>(population);
}

double Pop::getDeathRate()
{
	return deaths / static_cast<double>(population);
}

//*********************** main ****************************
int main()
{
	Pop  myTown;             // instantiate one Pop object
	int  numPeople;
	int  numBirths,
	int  numDeaths;
	 
	// Input, validate, and set values for myTown
	cout << "Enter total population: ";
	cin  >> numPeople;
	while (numPeople < 1)
	{   cout << "Value must be greater than 0.  Please re-enter: ";
	cin  >> numPeople;
	}
	myTown.setPopulation(numPeople); 

	 cout << "Enter annual number of births: ";
	 cin  >> numBirths;
	 while (numBirths < 0)
	{   cout << "Value cannot be negative.  Please re-enter: ";
		cin  >> numBirths;
	}
	myTown.setBirths(numBirths); 
	   
	cout << "Enter annual number of deaths: ";
	cin  >> numDeaths;
	while (numDeaths < 0)
	{   cout << "Value cannot be negative.  Please re-enter: ";
		cin  >> numDeaths;
	}
	myTown.setDeaths(numDeaths);

	// Display statistics for myTown
	cout << "\nPopulation Statistics  ";        
	cout << fixed << showpoint  << setprecision(3);
	cout << "\n\tPopulation:  " << setw(7) << myTown.getPopulation();
	cout << "\n\tBirth Rate:  " << setw(7) << myTown.getBirthRate();
	cout << "\n\tDeath Rate:  " << setw(7) << myTown.getDeathRate() << endl;
	 
	 return 0;
}
Last edited on
cannot figure out what is wrong with the program


Just read the errors your compiler is telling you.
I tried to compile it here and it told me some useful stuff like this:

- garbage characters like "/>
void Pop::setBirths(int B)/>

- the function
1
2
3
4
5
void Pop::setBirths(int B)/>

{
	births = b;
}

you are trying to use "b", but you haven't declared it. (in other words it should be something like
1
2
3
4
5
void Pop::setBirths(int b)/>

{
	births = b;
}


this:
int numBirths,
should end in a semi-colon and not a comma.

Seriously, if your compiler isn't telling you stuff like this, get a more up to date one.
Looks like it may have been a copy of the code from: http://www.dreamincode.net/forums/topic/201035-birthrate-and-deathrate-need-some-suggestions/page__view__findpost__p__1174358

The things like:
void Pop::setBirths(int B)/>
are known bugs in the forum software being used.

Last edited on
Ah I see..
Topic archived. No new replies allowed.