In this laboratory, you will create a class, PopInfo and use objects of that cla
ID: 3766590 • Letter: I
Question
In this laboratory, you will create a class, PopInfo and use objects of that class. The private member data will be a city name - a string, the total population of the city - a long, the number of births and the number of deaths - both ints. The data on city name, population, the number of births and the number of deaths will be read from standard input. The program will calculate the birth rate and the death rate for the city. In a population, the birth rate and the death rate are calculated as follows: birth rate = (number of births)/population_size death rate = (number of deaths)/population_size For example, in a single year, a city with a population of 100,000 has 8000 births and 6000 deaths. The rates are then birth rate = 8000/100,000 = 0.08 death rate = 6000/100,000 = 0.06 Note that the birth rate and the death rate must be floats or doubles. The class needs at least one constructor; mutator functions, setPopulation(), setBirths(), setDeaths() and setCity(); and accessor functions, getPoPulation(), getBirths, getDeaths() and getCity(). Two additional member functions are getBirthrate() and getDeathrate() which return the birth and death rates ( float or double values) calculated by the formulas given above. Data validation should be done in the class functions to ensure that population values are not less than 1 and that birth or death values are not less than 0. The main() function should declare a PopInfo object in a way consistent with your constructor. The data for the object population, number of births, number of deaths, and city name should be read in from standard input. The values will be assigned to the object using the class mutator functions. The the program should write out the city information, the birth rate and the death rate for the object using the accessor functions, the getBirthrate() and the getDeathrate() functions. The information should be written to standard output and clearly identified as to what each number or string is. All I/O should be done in the program that uses the class and NOT in the class. Make sure that the output makes sense to you.
I have spent hours on this question, and in desperation I have made a Chegg account. Here's my code so far:
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class PopInfo
{
private:
long popTotal;
int birthNum;
int deathNum;
string city;
double birthRate, deathRate;
public:
PopInfo() //Constructor function
{
birthNum = 0;
city = "";
deathNum = 0;
popTotal = 1;
birthRate = 0.0;
deathRate = 0.0;
}
void setPopulation(int popTotalInput)
{
popTotalInput = popTotal;
popTotal += birthNum;
popTotal -= deathNum;
}
int getBirths (int birthNum)
{
if (birthNum < 0) { //Data validation for birthNum
cout << endl << "You cannot have negative births or deaths. Please restart the program." << endl;
return 0;
}
return birthNum;
}
void setBirths (int birthNumInput)
{
birthNum = birthNumInput;
}
int getDeaths (int deathNum)
{
if (deathNum < 0) { //Data validation for deathNum
cout << endl << "You cannot have negative births or deaths. Please restart the program." << endl;
return 0;
}
return deathNum;
}
int setDeaths (int deathNumInput)
{
deathNum = deathNumInput;
}
string getCity(string city)
{
return city;
}
long getPopulation(long popTotal)
{
if (popTotal < 1) { //Data validation for popTotal
cout << endl << "You cannot have a population of less than 1. Please restart the program." << endl;
return 0;
}
return popTotal;
}
void setCity (string cityInput)
{
city = cityInput;
}
double getBirthRate(long popTotal, int birthNumInput)
{
birthRate = 1.0*(birthNumInput/popTotal);
return birthRate;
}
double getDeathRate (long popTotal, int deathNumInput)
{
deathRate = 1.0*(deathNumInput/popTotal);
return deathRate;
}
} numberOfPeople;
int main()
{
string cityInput;
int birthNumInput, deathNumInput;
long popTotalInput;
cout << "What is the name of your city? ";
cin >> cityInput;
cout << " What is the number of births in the city? ";
cin >> birthNumInput;
cout << " What is the number of deaths in the city? ";
cin >> deathNumInput;
cout << " What is the city's population? ";
cin >> popTotalInput;
cout << " The city name is: " << numberOfPeople.getCity(cityInput) << "." << endl
<< "The number of births is: " << numberOfPeople.getBirths(birthNumInput) << "." << endl
<< "The number of deaths is: " << numberOfPeople.getDeaths(deathNumInput) << "." << endl
<< "The city's population is: " << numberOfPeople.getPopulation(popTotalInput) << "." << endl
<< "The city's birth rate is: " << showpoint << numberOfPeople.getBirthRate(popTotalInput, birthNumInput) << "." << endl
<< "The city's death rate is: " << showpoint << numberOfPeople.getDeathRate(popTotalInput, deathNumInput) << "." << endl;
return 0;
}
When it displays the birth and death rates, it only gives me a 0. I have tried countless times to fix it and gotten nowhere. Please answer this question as soon as possible.
Explanation / Answer
Mistake:
1.0*(5/2) is 2.00 but 1.0*5/2 is 2.500 because 1.0*(5/2) is evaluated to 1.0*(2.5) then 2.5 is converted into integer 2 and then 2.0 is multiplied by 1.0 to produce 2.00. Remember that all calculations in paranthesis are performed first then the paranthesis is removed.
here is the modified code
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class PopInfo
{
private:
long popTotal;
int birthNum;
int deathNum;
string city;
double birthRate, deathRate;
public:
PopInfo() //Constructor function
{
birthNum = 0;
city = "";
deathNum = 0;
popTotal = 1;
birthRate = 0.0;
deathRate = 0.0;
}
void setPopulation(int popTotalInput)
{
popTotalInput = popTotal;
popTotal += birthNum;
popTotal -= deathNum;
}
int getBirths (int birthNum)
{
if (birthNum < 0) { //Data validation for birthNum
cout << endl << "You cannot have negative births or deaths. Please restart the program." << endl;
return 0;
}
return birthNum;
}
void setBirths (int birthNumInput)
{
birthNum = birthNumInput;
}
int getDeaths (int deathNum)
{
if (deathNum < 0) { //Data validation for deathNum
cout << endl << "You cannot have negative births or deaths. Please restart the program." << endl;
return 0;
}
return deathNum;
}
int setDeaths (int deathNumInput)
{
deathNum = deathNumInput;
}
string getCity(string city)
{
return city;
}
long getPopulation(long popTotal)
{
if (popTotal < 1) { //Data validation for popTotal
cout << endl << "You cannot have a population of less than 1. Please restart the program." << endl;
return 0;
}
return popTotal;
}
void setCity (string cityInput)
{
city = cityInput;
}
double getBirthRate(long popTotal, int birthNumInput)
{
birthRate = 1.0*birthNumInput/popTotal;
return birthRate;
}
double getDeathRate (long popTotal, int deathNumInput)
{
deathRate = 1.0*deathNumInput/popTotal;
return deathRate;
}
} numberOfPeople;
int main()
{
string cityInput;
int birthNumInput, deathNumInput;
long popTotalInput;
cout << "What is the name of your city? ";
cin >> cityInput;
cout << " What is the number of births in the city? ";
cin >> birthNumInput;
cout << " What is the number of deaths in the city? ";
cin >> deathNumInput;
cout << " What is the city's population? ";
cin >> popTotalInput;
cout << " The city name is: " << numberOfPeople.getCity(cityInput) << "." << endl
<< "The number of births is: " << numberOfPeople.getBirths(birthNumInput) << "." << endl
<< "The number of deaths is: " << numberOfPeople.getDeaths(deathNumInput) << "." << endl
<< "The city's population is: " << numberOfPeople.getPopulation(popTotalInput) << "." << endl
<< "The city's birth rate is: " << showpoint << numberOfPeople.getBirthRate(popTotalInput, birthNumInput) << "." << endl
<< "The city's death rate is: " << showpoint << numberOfPeople.getDeathRate(popTotalInput, deathNumInput) << "." << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.