gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:17:46 24 4
gpt4 key购买 nike

我不明白为什么我的代码没有计算出生率和死亡率。我一直都得到 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;
}

最佳答案

您不小心将 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;
}

关于C++ 初学者逻辑错误 - 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com