#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
//Declare variables
double pounds, grams, kilograms;
//Declare constants
const double LB2GRM = 453.592;
//Give title to program
cout << "Pound to kilograms converter" << endl;
//Prompt the user to enter a weight
cout << "Please enter a weight in pounds: " << endl;
cin >> pounds;
//Displaying weight with two decimal points
cout << setiosflags(ios::showpoint) << setprecision(2);
//Round off weight
static_cast<double>(static_cast<double>(pounds +.5));
//Formula for conerversion
double fmod(pounds * LB2GRM);
cin >> grams;
//Show results
cout << pounds << " pounds are equal to " << kilograms << " kgs and " << grams << " grams" << endl;
return 0;
}
如何将克转换成千克?我找到了第一部分,只是不确定如何完成它?我只需输入千克常数吗?
答案 0 :(得分:0)
在打印之前,您永远不会向kilograms
分配任何内容。你应该从公式中指定它。
grams = pounds * LB2GRM;
// Divide grams by 1000 to get the kg part
kilograms = floor(grams / 1000));
// The remaining grams are the modulus of 1000
grams = fmod(grams, 1000.0);
此外,这句话没有做任何事情:
static_cast<double>(static_cast<double>(pounds +.5));
首先,将某些内容转换为相同类型无效。其次,你没有将演员表的结果分配给任何东西(演员没有修改它的参数,它只返回转换后的值)。我怀疑你想要的是:
pounds = static_cast<double>(static_cast<int>(pounds +.5));
但更简单的方法是使用floor()
函数:
pounds = floor(pounds + .5);
将double
投射到int
将删除分数。