Hi, I am trying to figure out a program that I have been working on. The output appears but the math seems incorrect. Would anyone point me to the right direction? Thank you.
Here is the code:
// Chapter 7 - Assignment 11, Population
// This program calculates birth and death
// rates using a Population class.
//Program 4
#include <iostream>
#include <iomanip>
using namespace std;
class Pop
{
private:
long numPeople;
int numBirths,
numDeaths;
/*-------------------*/
long popRate;
int birthRate,
deathRate;
public:
Pop()
{numPeople;
numBirths;
numDeaths;
/*-------------------*/
}
void setPopulation(long numPeople);
void setBirths(int numBirths);
void setDeaths(int numDeaths);
/*-------------------------*/
long getPopulation(){return popRate;};
int getBirthRate(){return birthRate;};
int getDeathRate(){return deathRate;};
//*********************** 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);
Could you edit your post so that the code lies between
[code]
and
[/code]
blocks? (see the top left button in the format options on the right hand side)
Also if you copy and paste from your IDE it should keep the indents. It's very hard to read code when it looks like this so no-one will really want to help
ok i think i have it figured out, firstly you are using longs when you should be using floats or doubles. A long is still an integer type (in fact i think they are actually the same type) so when you try and divide number of births by number of people, you will not get an integer, but when you cast to long using static_cast<long> this will round it to the nearest integer afaik. also you are dividing by numPeople in both your getBirthRate() and getDeathRate() functions, but this variable never gets set. (check your setPopulation function)