Hi to all I am new to programming with C++, and I have been assigned a project where I have to build a program that calculates the average birthrate & death rate percentage of a population. I am currently stuck on this step where I need to convert an integer value into a double value to get the correct output. For example, a function for finding the birthrate would be (births/population) and since dividing integers it causes the floating point number to be truncated giving me a result of 0 instead of getting (10/100 which would equal 0.1). I understand that I have declared integers, but that is the requirement. I have thought about typecasting but had no luck converting the value. I apologize for the long explanation I hope it makes sense. Thank you in advance!
This is the following criteria:
private variables: have to be integers (population, births, deaths)
public functions:
"Set" member function for each of the three-member variables. If a population figure is less than 2 and is passed to the class, use a default value of 2. If a birth or a death figure less than 0 is passed, use a default value of 0.
getBirthrate member function. calculates the birth rate based on the following formula: (Birthrate = Number of births/population)
getDeathrate member function. calculates the death rate based on the following formula: (Deathrate = Number of deaths/ population)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Population class declaration
class Population
{
private:
int population, births, deaths;
public:
// default constructor: with no parameters
Population()
{
population = 2;
births = 0;
deaths = 0;
}
// constructor
Population(int p, int b, int d)
{
population = p;
births = b;
deaths = d;
}
// birthrate member function
int getBirthrate();
// deathrate member function
int getDeathrate();
void setPopulation(int);
void setBirth(int);
void setDeath(int);
int getPopulation();
int getBirth(int);
int getDeaths(int);
};
// member functions
void Population::setPopulation(int p)
{
if (population < 2)
{
population = 2;
cout << "Error: Invalid population input!" << endl;
}
else
{
population = p;
}
}
void Population::setBirth(int b)
{
if (births < 0)
{
births = 0;
cout << "Error: Invalid births input!" << endl;
}
else
{
births = b;
}
}
void Population::setDeath(int d)
{
if (deaths < 0)
{
deaths = 0;
cout << "Error: Invalid deaths input!" << endl;
}
else
{
deaths = d;
}
}
int Population::getPopulation()
{
return population;
}
int Population::getBirth(int)
{
return births;
}
int Population::getDeaths(int)
{
return deaths;
}
// calculating functions
int Population::getBirthrate()
{
return births / population;
}
int Population::getDeathrate()
{
return deaths / population;
}
// main function
int main()
{
// instantiation of a population object
Population x;
int xBirths, xDeaths, xPopulation;
// getting the values
cout << "What is the population: " << endl;
cin >> xPopulation;
cout << "What is the number of births: " << endl;
cin >> xBirths;
cout << "What are the number of deaths: " << endl;
cin >> xDeaths;
// set functions
x.setPopulation(xPopulation);
x.setBirth(xBirths);
x.setDeath(xDeaths);
// calculating the values
cout << "The population is: " << x.getPopulation() << endl;
cout << "The birthrate is: " << x.getBirthrate() << endl;
cout << "The deathrate is: " << x.getDeathrate() << endl;
return 0;
}
I would guess that you have two problems
int Population::getDeathrate()
{
return deaths / population;
}
The first is, as you've identified, you are doing integer division. You can solve that with a type cast. The second problem is that your function returns an integer, when it seems from the question above that you want a floating point result, e.g. 10/100 = 0.1. The solution to that problem is to change the return type to double
.
So, putting both of those together
double Population::getDeathrate()
{
return (double)deaths / (double)population;
}