Rainfall Statistics c++

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

};

void Pop::setPopulation(long numPeople)
{popRate = numPeople;};



void Pop::setBirths(int numBirths)
{ birthRate = static_cast<long>(numBirths)/numPeople;}



void Pop::setDeaths(int numDeaths)
{deathRate = static_cast<long>(numDeaths)/numPeople;};

//*********************** 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 << "\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;
}


//OutPut:
Enter total population: 2000
Enter annual number of births: 500
Enter annual number of deaths: 300

Population Statistics
Population: 2000
Birth Rate: 0
Death Rate: 0
Press any key to continue . . .
Last edited on
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
like this?


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

};

void Pop::setPopulation(long numPeople)
{popRate = numPeople;};



void Pop::setBirths(int numBirths)
{ birthRate = static_cast<long>(numBirths)/numPeople;}



void Pop::setDeaths(int numDeaths)
{deathRate = static_cast<long>(numDeaths)/numPeople;};

//*********************** 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 << "\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;
}


//OutPut:
Enter total population: 2000
Enter annual number of births: 500
Enter annual number of deaths: 300

Population Statistics
Population: 2000
Birth Rate: 0
Death Rate: 0
Press any key to continue . . .
That's better!

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)
Ok thanks slice,

I will try to fix it and if I cant figure it out I will repost in a bit. :)
Topic archived. No new replies allowed.