从kg转换为宝石和磅

时间:2017-09-29 09:30:53

标签: c++

好的,所以当我以kg公斤输入我的体重时,给我正确的结石输出,但我的体重减轻了。

假设2.2磅是一公斤 和14磅是一块石头

它应该给你重量的石头和十进制的磅数,我的石头是好的但是英镑是任何帮助继承人脚本

#include <iostream>
using namespace std;
//declarations
const float LBS_PER_KG=2.2;
const float LBS_PER_STONE=14;
const float STONE_PER_KG=6.36;

int main()
{

float weightInKG;
float weightInSTONE;
float weightInLBS;

cout <<"enter a measurement in Kilograms";
cin >> weightInKG;

weightInSTONE = static_cast<int>(weightInSTONE*LBS_PER_KG/LBS_PER_STONE);
weightInSTONE = weightInKG/STONE_PER_KG;
weightInLBS = weightInKG / LBS_PER_KG;
cout <<weightInKG<<"KG = "<<weightInSTONE<<"Stones and"
  <<weightInLBS<<"Pounds";
cout <<endl;
 return 0;
}

1 个答案:

答案 0 :(得分:2)

您对weightInLBS的计算错误。你必须乘以而不是划分

weightInLBS = weightInKG * LBS_PER_KG; // [lbs] = [kg] * ([lbs] / [kg])

为了避免进一步更改程序的一些问题,您应该用零

初始化变量
float weightInKG = 0;
float weightInSTONE = 0;
float weightInLBS = 0;

如果您想在评论中显示您的石头值的十进制分隔符后的值,请在评论中提出问题,您应该使用此计算

weightInLBS = (weightInSTONE - static_cast<int>(weightInSTONE)) * LBS_PER_STONE;
// cut off the value before decimal separator and convert it to LBS

这将显示结果

cout <<weightInKG<<"KG = "<<static_cast<int>(weightInSTONE)<<"Stones and"<<weightInLBS<<"Pounds"<<endl;