C ++初学者逻辑错误 - 返回0

时间:2013-05-01 00:20:48

标签: c++ runtime-error

我不明白为什么我的代码不计算出生率和死亡率。我两个都保持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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

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

另一个问题:由于“rate”是0到1之间的数字,而你的函数getBirths返回int,你将遇到一个舍入问题...

也不确定是否在课程的上下文中设置了deabir(您在main级别声明了它们)。你邀请问题的地方很多......

最简单的解决方案是重写这两个功能:

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

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

但请阅读您的代码,并问自己变量的范围是什么,它们的设置位置(如果您设置了它们......),它们的使用位置,执行类型转换的位置....可以从中学到很多东西。

修改

我无法自拔,并决定复制你的程序并进行调试。在结构中进行了一些简化之后,我想出了以下内容(注意我在类定义中移动了两个函数calculateBirthRatecalculateDeathRate以保持一致性;并且我使用了“内部已知”变量{{1等等,而不是你正在使用的一些“替代”......它变得非常混乱。最后,正如我在原始答案中提到的那样 - 我确保实际计算了出生率和死亡率。我已经标记了使用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

答案 2 :(得分: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;