英文:
C++ Project: converting a interger into a double
问题
你好,这是你的翻译:
"Hi to all I am new to programming with C++, and I have been assigned a project where I have to build a program that calculates the average birthrate & death rate percentage of a population. I am currently stuck on this step where I need to convert an integer value into a double value to get the correct output. For example, a function for finding the birthrate would be (births/population) and since dividing integers it causes the floating point number to be truncated giving me a result of 0 instead of getting (10/100 which would equal 0.1). I understand that I have declared integers, but that is the requirement. I have thought about typecasting but had no luck converting the value. I apologize for the long explanation I hope it makes sense. Thank you in advance!"
这是你的代码,我已经过滤掉不需要翻译的部分:
// Population class declaration
class Population
{
private:
int population, births, deaths;
public:
// default constructor: with no parameters
Population()
{
population = 2;
births = 0;
deaths = 0;
}
// constructor
Population(int p, int b, int d)
{
population = p;
births = b;
deaths = d;
}
// birthrate member function
int getBirthrate();
// deathrate member function
int getDeathrate();
void setPopulation(int);
void setBirth(int);
void setDeath(int);
int getPopulation();
int getBirth(int);
int getDeaths(int);
};
// member functions
void Population::setPopulation(int p)
{
if (population < 2)
{
population = 2;
cout << "Error: Invalid population input!" << endl;
}
else
{
population = p;
}
}
void Population::setBirth(int b)
{
if (births < 0)
{
births = 0;
cout << "Error: Invalid births input!" << endl;
}
else
{
births = b;
}
}
void Population::setDeath(int d)
{
if (deaths < 0)
{
deaths = 0;
cout << "Error: Invalid deaths input!" << endl;
}
else
{
deaths = d;
}
}
int Population::getPopulation()
{
return population;
}
int Population::getBirth(int)
{
return births;
}
int Population::getDeaths(int)
{
return deaths;
}
// calculating functions
int Population::getBirthrate()
{
return births / population;
}
int Population::getDeathrate()
{
return deaths / population;
}
// main function
int main()
{
// instantiation of a population object
Population x;
int xBirths, xDeaths, xPopulation;
// getting the values
cout << "What is the population: " << endl;
cin >> xPopulation;
cout << "What is the number of births: " << endl;
cin >> xBirths;
cout << "What are the number of deaths: " << endl;
cin >> xDeaths;
// set functions
x.setPopulation(xPopulation);
x.setBirth(xBirths);
x.setDeath(xDeaths);
// calculating the values
cout << "The population is: " << x.getPopulation() << endl;
cout << "The birthrate is: " << x.getBirthrate() << endl;
cout << "The deathrate is: " << x.getDeathrate() << endl;
return 0;
}
希望对你有所帮助!如果有任何问题,请随时提出。
英文:
Hi to all I am new to programming with C++, and I have been assigned a project where I have to build a program that calculates the average birthrate & death rate percentage of a population. I am currently stuck on this step where I need to convert an integer value into a double value to get the correct output. For example, a function for finding the birthrate would be (births/population) and since dividing integers it causes the floating point number to be truncated giving me a result of 0 instead of getting (10/100 which would equal 0.1). I understand that I have declared integers, but that is the requirement. I have thought about typecasting but had no luck converting the value. I apologize for the long explanation I hope it makes sense. Thank you in advance!
This is the following criteria:
-
private variables: have to be integers (population, births, deaths)
-
public functions:
- default constructor with no parameters, initialize (population = 2), (births & deaths = 0)
- constructor with three parameters. The constructor should pass the population, the number of births, and deaths for the Population object.
-
"Set" member function for each of the three-member variables. If a population figure is less than 2 and is passed to the class, use a default value of 2. If a birth or a death figure less than 0 is passed, use a default value of 0.
-
getBirthrate member function. calculates the birth rate based on the following formula: (Birthrate = Number of births/population)
-
getDeathrate member function. calculates the death rate based on the following formula: (Deathrate = Number of deaths/ population)
#include <iostream> #include <string> #include <iomanip> using namespace std; // Population class declaration class Population { private: int population, births, deaths; public: // default constructor: with no parameters Population() { population = 2; births = 0; deaths = 0; } // constructor Population(int p, int b, int d) { population = p; births = b; deaths = d; } // birthrate member function int getBirthrate(); // deathrate member function int getDeathrate(); void setPopulation(int); void setBirth(int); void setDeath(int); int getPopulation(); int getBirth(int); int getDeaths(int); }; // member functions void Population::setPopulation(int p) { if (population < 2) { population = 2; cout << "Error: Invalid population input!" << endl; } else { population = p; } } void Population::setBirth(int b) { if (births < 0) { births = 0; cout << "Error: Invalid births input!" << endl; } else { births = b; } } void Population::setDeath(int d) { if (deaths < 0) { deaths = 0; cout << "Error: Invalid deaths input!" << endl; } else { deaths = d; } } int Population::getPopulation() { return population; } int Population::getBirth(int) { return births; } int Population::getDeaths(int) { return deaths; } // calculating functions int Population::getBirthrate() { return births / population; } int Population::getDeathrate() { return deaths / population; } // main function int main() { // instantiation of a population object Population x; int xBirths, xDeaths, xPopulation; // getting the values cout << "What is the population: " << endl; cin >> xPopulation; cout << "What is the number of births: " << endl; cin >> xBirths; cout << "What are the number of deaths: " << endl; cin >> xDeaths; // set functions x.setPopulation(xPopulation); x.setBirth(xBirths); x.setDeath(xDeaths); // calculating the values cout << "The population is: " << x.getPopulation() << endl; cout << "The birthrate is: " << x.getBirthrate() << endl; cout << "The deathrate is: " << x.getDeathrate() << endl; return 0; }
</details>
# 答案1
**得分**: 0
替代这个:
```cpp
int Population::getBirthrate()
{
return births / population;
}
使用这个:
int Population::getBirthrate()
{
return (births * 1.0) / population;
}
将一个整数与1.0相乘将其转换为双精度浮点数。
英文:
instead of this:
int Population::getBirthrate()
{
return births / population;
}
do this:
int Population::getBirthrate()
{
return (births * 1.0) / population;
}
multiplying an integer with 1.0 convert them into double.
答案2
得分: 0
我猜你有两个问题
int Population::getDeathrate()
{
return deaths / population;
}
第一个问题就是,正如你已经指出的,你在进行整数除法。你可以通过使用类型转换解决这个问题。第二个问题是,你的函数返回一个整数,而从上面的问题看来,你想要一个浮点数的结果,例如 10/100 = 0.1。解决这个问题的方法是将返回类型更改为 double
。
因此,将这两个问题合并在一起:
double Population::getDeathrate()
{
return (double)deaths / (double)population;
}
英文:
I would guess that you have two problems
int Population::getDeathrate()
{
return deaths / population;
}
The first is, as you've identified, you are doing integer division. You can solve that with a type cast. The second problem is that your function returns an integer, when it seems from the question above that you want a floating point result, e.g. 10/100 = 0.1. The solution to that problem is to change the return type to double
.
So, putting both of those together
double Population::getDeathrate()
{
return (double)deaths / (double)population;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论