c++输出转换错误

C++ Output Conversion Error

本文关键字:错误 转换 输出 c++      更新时间:2023-10-16

我应该做一个代码,从英尺和英寸转换到米和厘米。但是当我运行我的代码时,我没有得到我应该得到的。例如,我输入1英尺和0厘米。我应该得到0.3048米和0厘米但我得到的是1米和0厘米。的帮助!

#include <iostream>
using namespace std;
void getLength(double& input1, double& input2);
void convert(double& variable1, double& variable2);
void showLengths(double output1, double output2);
int main()
{
    double feet, inches;
    char ans;
    do
    {
        getLength(feet, inches);
        convert(feet, inches);
        showLengths(feet, inches);
        cout << "Would you like to go again? (y/n)" << endl;
        cin >> ans;
        cout << endl;
    } while (ans == 'y' || ans == 'Y');
}
void getLength(double& input1, double& input2)
{
    cout << "What are the lengths in feet and inches? " << endl;
    cin >> input1 >> input2;
    cout << input1 << " feet and " << input2 << " inches is converted to ";
}
void convert (double& variable1, double& variable2)
{
    double meters = 0.3048, centimeters = 2.54;
    meters *= variable1;
    centimeters *= variable2;
}
void showLengths (double output1, double output2)
{
    cout << output1 << " meter(s) and " << output2 << " centimeter(s)" << endl;
}

任何帮助都是感激的。谢谢!

meters *= variable1;
centimeters *= variable2;
应该

variable1 *= meters;
variable2 *= centimeters;

上一条评论说:你没有将产品分配给你通过引用传递的变量(variable1variable2),所以这些值不会从你的原始输入1和0改变。