keep getting errors with this program..CONFUSED

I am very confused, been working on this for hours

#include <iostream>
#include <iomanip>
using namespace std;

class Population
{
private:
int pop;
int births;
int deaths;

public:
void setPopulation (int);
void setBirths(int);
void setDeaths(int);

int getPopulation();
double getBirthRate();
double getDeathRate();

Population() : pop(0), deaths(0), births(0) {}
};

void Population::setPopulation(int p)
{
pop = p;
}

void Population::setBirths(int B)
{
births = B;
}

void Population::setDeaths(int d)
{
deaths = d;
}

int Population::getPopulation()
{
return pop;
}

double Population::getBirthRate()
{
return births / static_cast<double>(pop);
}
double Population::getDeathRate()
{
return deaths / static_cast<double>(pop);
}


int main()
{
Population Town;
int People;
int Births,
Deaths;

cout << "Enter total population: ";
cin >> People;
while (People < 1)
{ cout << "ERROR: Value has to be greater than 0. ";
cin >> People;
}
Town.setPopulation(People);

cout << "Enter annual number of births: ";
cin >> Births;
while (Births < 0)
{ cout << "ERROR: Value cannot be negative. ";
cin >> Births;
}
Town.setBirths(Births);

cout << "Enter annual number of deaths: ";
cin >> Deaths;
while (Deaths < 0)
{ cout << "ERROR: Value cannot be negative. ";
cin >> Deaths;
}
Town.setDeaths(Deaths);

cout << "\nPopulation Statistics ";
cout << fixed << showpoint << setprecision(3);
cout << "\n\tPopulation: " << setw(7) << Town.getPopulation();
cout << "\n\tBirth Rate: " << setw(7) << Town.getBirthRate();
cout << "\n\tDeath Rate: " << setw(7) << Town.getDeathRate() << endl;

return 0;
}
So what exactly are the errors you are getting that you are confused on? Also, please use code tags around your code either edit and click the <> button or type [code] before and [ /code] after(without space)
I am not sure what the Populate(): pop(0), deaths(0), births(0) {} is doing at the end of your class, but to start, every class should have a constructor and a method to set your variables as passable arguments, or else using them in those other functions will not do you any good.

As giblit said, please explain what the errors are (and code tags are your friends).
MrZ wrote:
I am not sure what the Populate(): pop(0), deaths(0), births(0) {} is doing at the end of your class
that is called an initializer list. That is also how you can initialize members that are constant or references.
that is called an initializer list. That is also how you can initialize members that are constant or references.


Hmm. You learn something new every day I suppose. Personally, I would rather use a separate method to make it as explicit as possible, but I guess its preference. The constructor is still lacking...
Running it makes it look perfectly fine. As for initializer lists, they are generally preferred over explicit initialization due to both efficiency and flexibility (for example, you can have a class type variable in your class with a non-default constructor).
Topic archived. No new replies allowed.