Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey guys, I need little help here. This is C++ by the way So, the program below is completed. But right below, there is one

Hey guys, I need little help here.

This is C++ by the way

So, the program below is completed. But right below, there is one numbered requirement that need to be added to program. Here it is:

1- Bullet proof your program by performing input validation - e.g. for int input, filter out any negative int, double, char or string e.g. allowing user one more try to enter a valid positive number (0 allowed) before quitting

Here is the program bellow

// Population

// This program calculates birth and death rates using

// a Population class.

#include

#include

using namespace std;

class Pop

{

private:

long population; // Current population

int numBirths, // Annual number of births

numDeaths; // Annual number of deaths

public:

// Constructors

Pop();

Pop(long, int, int);

// Mutators (i.e. "set" functions)

void setPopulation(long p);

void setBirths(int b);

void setDeaths(int d);

// Accessors

long getPopulation() // getPopulation is not required by specs

{ return population; };

double getBirthRate()

{ return double (numBirths) / population; }

double getDeathRate()

{ return double (numDeaths) / population; }

};

/********************************************************

* Pop::Pop *

* Population class default constructor *

********************************************************/

Pop::Pop()

{

population = 2;

numBirths = 0;

numDeaths = 0;

}

/********************************************************

* Pop::Pop *

* Population class constructor with 3 parameters *

********************************************************/

Pop::Pop(long p, int b, int d)

{

setPopulation(p);

setBirths(b);

setDeaths(d);

}

/********************************************************

* Pop::setPopulation *

* Validates and sets the population. *

********************************************************/

void Pop::setPopulation(long p)

{

if (p >= 2)

population = p;

else

population = 2; // Default value

}

/********************************************************

* Pop::setBirths *

* Validates and sets the annual number of births. *

********************************************************/

void Pop::setBirths(int b)

{

if (b >= 0)

numBirths = b;

else

numBirths = 0; // Default value

}

/********************************************************

* Pop::setDeaths *

* Validates and sets the annual number of deaths. *

********************************************************/

void Pop::setDeaths(int d)

{

if (d >= 0)

numDeaths = d;

else

numDeaths = 0; // Default value

}

//*********************** main ****************************

int main()

{

Pop myTown; // instantiate one Pop object

long numPeople;

int numBirths,

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 << " Population Statistics ";

cout << fixed << showpoint << setprecision(3);

cout << " \tPopulation: " << setw(7) << myTown.getPopulation();

cout << " \tBirth Rate: " << setw(7) << myTown.getBirthRate();

cout << " \tDeath Rate: " << setw(7) << myTown.getDeathRate() << endl;

return 0;

}

/* SAMPLE RUN RESULTS

Enter total population: 22500

Enter annual number of births: 860

Enter annual number of deaths: 410

Population Statistics

Population: 22500

Birth Rate: 0.038

Death Rate: 0.018

*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions