C++ 如何将磅换算成克和千克-C++; #包括 #包括 #包括 使用名称空间std; int main() { //声明变量 双磅,克,公斤; //声明常量 常数双LB2GRM=453.592; //为节目命名 cout

C++ 如何将磅换算成克和千克-C++; #包括 #包括 #包括 使用名称空间std; int main() { //声明变量 双磅,克,公斤; //声明常量 常数双LB2GRM=453.592; //为节目命名 cout,c++,fmod,C++,Fmod,在打印之前,您永远不会将任何内容分配给kg。您应该根据公式进行分配 #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main () { //Declare variables double pounds, grams, kilograms; //Declare constants const double LB2GRM = 453

在打印之前,您永远不会将任何内容分配给
kg
。您应该根据公式进行分配

#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;
}
此外,此语句不起任何作用:

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);

double
转换为
int
将删除分数。

读取fmod肯定会有助于解释错误消息(需要两个参数)程序怎么可能知道一公斤有多少克呢?谢谢@Borleader:)在每条语句后面加上一条与语句内容相同的注释可能有点过头了。注释应该保留在不明显的地方。当调用像
fmod()这样的函数时
你需要把结果分配给某个东西。在声明之前加上
double
,这不是你所说的。哦,我不知道我只是想把磅数四舍五入。回到fmod。使用'cin>>不会分配结果吗?我必须从main之外调用它吗?
cin>
是为了从t获取输入用户。例如,当您执行
cin>>磅数时,用户可以输入磅数。听起来您需要回到课本,复习基础知识。您对如何使用函数感到非常困惑。当您调用类似
fmod()
的函数时,您可以按
result=fmod(x,y);
static_cast<double>(static_cast<double>(pounds +.5));
pounds = static_cast<double>(static_cast<int>(pounds +.5));
pounds = floor(pounds + .5);