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;
}
|