0

我不明白为什么我的代码没有计算出生率和死亡率。我继续得到0。我包括在内static_cast<double>以确保不会发生这种情况。任何反馈/帮助?

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

double calculateBirthRate();
double calculateDeathRate();

class PopInfo
{
    private:
        string cityName;
        long totalCityPopulation;
        int numberOfBirths;
        int numberOfDeaths;
        double birthrate;
        double deathrate;
        int bir;
        int dea;
        long citpop;

    public:
        PopInfo()
    {
        cityName = "";
        totalCityPopulation = numberOfBirths = numberOfDeaths = 0;
    }

    long getPopulation()
    {
        return totalCityPopulation;
    }

    int getBirths()
    {
        return birthrate;
    }

    int getDeaths()
    {
        return deathrate;
    }

    string getCity()
    {
        return cityName;
    }

    void setCityName(string nameOfCity)
    {
        cityName = nameOfCity;
    }

    void setTotalCityPopulation(long populationOfCity)
    {
        totalCityPopulation = populationOfCity;
    }

    void setNumberOfBirths(int birthNumbers)
    {
        numberOfBirths = birthNumbers;
    }

    void setNumberOfDeaths(int deathNumbers)
    {
        numberOfDeaths = deathNumbers;
    }

    void calculateBirthRate(PopInfo);
    void calculateDeathRate(PopInfo);

};

int main()
{
    PopInfo newCity;

    string cit;
    long citpop;
    int bir;
    int dea;


   cout << "What is the city name?: " << endl;
   cin >> cit;
   cout << "What is the total city population?: " << endl;
   cin >> citpop;
   while (citpop < 1)
   {
       cout << "Please enter a valid total city population: " << endl;
       cin >> citpop;
   }
   cout << "What are the number of births?: " << endl;
   cin >> bir;
   while (bir < 0)
   {
       cout << "Please enter a valid number of births: " << endl;
       cin >> bir;
   }
   cout << "What are the number of deaths?: " << endl;
   cin >> dea;
   while (dea < 0)
   {
       cout << "Please enter a vaild number of deaths: " << endl;
       cin >> dea;
   }

   newCity.setCityName(cit);
   newCity.setTotalCityPopulation(citpop);
   newCity.setNumberOfBirths(bir);
   newCity.setNumberOfDeaths(dea);

    cout << endl;
    cout << "The city name is " << newCity.getCity() << endl;
    cout << "The total city population is " << newCity.getPopulation() << endl;
    cout << "The birth rate is " << newCity.getBirths() << endl;
    cout << "The death rate is " << newCity.getDeaths() << endl;

   return 0;
}


void PopInfo::calculateBirthRate(PopInfo newCity)
{
    double birthrate = static_cast<double>(newCity.bir) / newCity.citpop;
}

void PopInfo::calculateDeathRate(PopInfo newCity)
{
    double deathrate = static_cast<double>(newCity.dea) / newCity.citpop;
}
4

3 回答 3

1

你不小心把birthratedeathrate作为局部变量。删除前导关键字double,使其:

void PopInfo::calculateBirthRate(PopInfo newCity)
{
    birthrate = static_cast<double>(newCity.bir) / newCity.citpop;
}

void PopInfo::calculateDeathRate(PopInfo newCity)
{
    deathrate = static_cast<double>(newCity.dea) / newCity.citpop;
}

即便如此,您newCity按值传递还是有点奇怪——您是不是要将费率存储回同一个对象中,例如:

void PopInfo::calculateBirthRate(PopInfo& newCity)
{
    newCity.birthrate = static_cast<double>(newCity.bir) / newCity.citpop;
}

void PopInfo::calculateDeathRate(PopInfo& newCity)
{
    newCity.deathrate = static_cast<double>(newCity.dea) / newCity.citpop;
}

还是您的意思是对对象进行就地操作,例如:

void PopInfo::calculateBirthRate()
{
    birthrate = static_cast<double>(bir) / citpop;
}

void PopInfo::calculateDeathRate()
{
    deathrate = static_cast<double>(dea) / citpop;
}
于 2013-05-01T00:26:28.537 回答
1

我认为您从未调用过计算出生率和死亡率的函数!这是已经确定的问题之上,但我很确定这很重要......cout在那里放一个调试语句,看看我是否正确......

另一个问题:由于“rate”是一个介于 0 和 1 之间的数字,并且您的函数getBirths返回一个int,因此您将遇到舍入问题...

也不确定你是否曾在类的上下文中设置deabir(你在main级别声明它们)。很多地方都在招惹问题……

最简单的解决方案是重写这两个函数:

double getBirths()
{
    return (double)numberOfBirths/citypop;
}

double getDeaths()
{
    return (double)numberOfDeaths/citypop;
}

但是请阅读您的代码,并问自己变量的范围是什么,设置它们的位置(以及是否设置它们......),使用它们的位置,执行类型转换的位置......你可以学习很多。

编辑

我忍不住,决定复制你的程序并调试它。在对结构进行了一些简化后,我想出了以下内容(请注意,为了保持一致性,我将两个函数calculateBirthRate和类定义移到calculateDeathRate了类定义中;并且我使用了“内部已知”变量totalCityPopulation等,而不是您使用的一些“替代”变量使用......它变得非常令人困惑。最后,正如我在原始答案中提到的 - 我确保实际计算了出生率和死亡率。我已将更改的行标记为//***

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

double calculateBirthRate();
double calculateDeathRate();

class PopInfo
{
    private:
        string cityName;
        long totalCityPopulation;
        int numberOfBirths;
        int numberOfDeaths;
        double birthrate;
        double deathrate;
        int bir;
        int dea;
        long citpop;

    public:
        PopInfo()
    {
        cityName = "";
        totalCityPopulation = numberOfBirths = numberOfDeaths = 0;
    }

    long getPopulation()
    {
        return totalCityPopulation;
    }

    double getBirths() //*** was int
    {
        return birthrate;
    }

    double getDeaths() //*** was int
    {
        return deathrate;
    }

    string getCity()
    {
        return cityName;
    }

    void setCityName(string nameOfCity)
    {
        cityName = nameOfCity;

    }

    void setTotalCityPopulation(long populationOfCity)
    {
        totalCityPopulation = populationOfCity;
    }

    void setNumberOfBirths(int birthNumbers)
    {
        numberOfBirths = birthNumbers;
    }

    void setNumberOfDeaths(int deathNumbers)
    {
        numberOfDeaths = deathNumbers;
    }

    //*** this function moved into the class definition
    void calculateBirthRate()
    {
        birthrate = (double)numberOfBirths/totalCityPopulation; //*** using different variables
    }

    //*** this function moved into the class definition
    void calculateDeathRate()
    {
        deathrate = (double)numberOfDeaths / totalCityPopulation; //*** using different variables
    }
};

int main()
{
    PopInfo newCity;

    string cit;
    long citpop;
    int bir;
    int dea;


   cout << "What is the city name?: " << endl;
   cin >> cit;
   cout << "What is the total city population?: " << endl;

   cin >> citpop;
   while (citpop < 1)
   {
       cout << "Please enter a valid total city population: " << endl;
       cin >> citpop;
   }
   cout << "What are the number of births?: " << endl;
   cin >> bir;
   while (bir < 0)
   {
       cout << "Please enter a valid number of births: " << endl;
       cin >> bir;
   }
   cout << "What are the number of deaths?: " << endl;
   cin >> dea;
   while (dea < 0)
   {
       cout << "Please enter a vaild number of deaths: " << endl;
       cin >> dea;
   }

   newCity.setCityName(cit);
   newCity.setTotalCityPopulation(citpop);
   newCity.setNumberOfBirths(bir);
   newCity.setNumberOfDeaths(dea);
   newCity.calculateBirthRate(); //*** added, or it's never calculated
   newCity.calculateDeathRate(); //*** added, or it's never calculated

    cout << endl;
    cout << "The city name is " << newCity.getCity() << endl;
    cout << "The total city population is " << newCity.getPopulation() << endl;
    cout << "The birth rate is " << newCity.getBirths() << endl;
    cout << "The death rate is " << newCity.getDeaths() << endl;

   return 0;
}

当我运行此代码时,我得到以下信息:

What is the city name?: 
Amsterdam
What is the total city population?: 
1234567
What are the number of births?: 
12345
What are the number of deaths?: 
54321

The city name is Amsterdam
The total city population is 1234567
The birth rate is 0.00999946
The death rate is 0.044

你的代码和我的diff代码之间是:

33c33
<     double getBirths()
---
>     int getBirths()
38c38
<     double getDeaths()
---
>     int getDeaths()
68,71c68,69
< void calculateBirthRate()
< {
<     birthrate = (double)numberOfBirths/totalCityPopulation;
< }
---
>     void calculateBirthRate(PopInfo);
>     void calculateDeathRate(PopInfo);
73,76d70
< void calculateDeathRate()
< {
<     deathrate = (double)numberOfDeaths / totalCityPopulation;
< }
117,118d110
<    newCity.calculateBirthRate();
<    newCity.calculateDeathRate();
129a122,125
> void PopInfo::calculateBirthRate(PopInfo newCity)
> {
>     double birthrate = static_cast<double>(newCity.bir) / newCity.citpop;
> }
130a127,130
> void PopInfo::calculateDeathRate(PopInfo newCity)
> {
>     double deathrate = static_cast<double>(newCity.dea) / newCity.citpop;
> }
于 2013-05-01T00:30:24.597 回答
0
double birthrate = static_cast<double>(newCity.bir) / newCity.citpop;
...
double deathrate = static_cast<double>(newCity.dea) / newCity.citpop;

在这里,您正在驱动两个新的变量名称birthratedeath rate. 您没有将值写入两个类数据成员。在名称覆盖之前写入类型。要更改,只需将其删除。

birthrate = static_cast<double>(newCity.bir) / newCity.citpop;
...
deathrate = static_cast<double>(newCity.dea) / newCity.citpop;
于 2013-05-01T00:27:00.337 回答