C++转换不起作用

use*_*229 1 c++

#include <iostream>
using namespace std;
/*Use void functions, they perform some action but do not return a value*/

//Function Protoype, input,read in feet and inches:
void input (double&  feet, double& inches);
//Function Prototype, calculate, calculate with given formulae.
void calculate(double& feet, double& inches);
//Function Prototype, output, outputs calculations to screen.
void output (double meters, double centimeters);  

int main () 
{
    double feet;
    double inches;
    char repeat;

    do
    {
        //Call input:
        input(feet, inches);
        //Call calculate:
        calculate(feet, inches);
        //Call output:
        output (feet, inches);
        cout << "\n";
        cout << "Repeat? (Y/N): ";
        cin >> repeat;
        cout << "\n";
    }
    while (repeat == 'Y' || repeat == 'y');
}

//Input function definition:
void input (double&  feet, double& inches)
{
    cout << "Please enter the length in feet" << endl;
    cin >> feet;

    cout << "Please enter the length in inches" << endl;
    cin >> inches;
}

//Calculate function definition, insert formulae here:
void calculate (double& feet, double& inches)
{

    feet = (feet * 0.3048);
     inches = (inches * 2.54);
}

//Output function definition:
void output (double meters, double centimeters)
{
    cout << meters << " meters & " << centimeters << " cm's. " << endl;
}
Run Code Online (Sandbox Code Playgroud)

为什么我的转换不起作用?或者我做错了什么?

目标:给出以英尺和英寸为单位的长度,我想输出一个以米和厘米为单位的等效长度.

//Calculate function definition, insert formula here:
void calculate (double& feet, double& inches)
{
     feet = (feet * 0.3048);
  inches = (inches * 2.54);
}
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 7

这似乎是一种奇怪的方式,改变了实际的输入变量.我会选择改为:

void calculate (double feet, double inches, double& meters, double& centimeters) {
    double all_inches = feet * 12.0 + inches;
    centimeters = all_inches * 2.54;
    meters = int (centimeters / 100.0);
    centimeters -= (meters * 100.0);
}
Run Code Online (Sandbox Code Playgroud)

在任何情况下,即使你确实使用相同的变量进行输入和输出(然后它们应该重命名为更合适的东西),它仍然最容易转换为单个形式(英寸)然后转换为厘米然后返回到米/厘米.

在你当前的代码中,如果你传入1英尺,0英寸,你将得到0.3048米,0厘米,而不是更正确的30.48厘米.通过使用此答案中的代码,它将首先转换为12.0英寸,然后从那里转换为30.48厘米,然后转换为0米,30.48厘米.

同样,4.5英尺(4英尺6英寸)将首先转换为54英寸,然后转换为137.16厘米,然后转换为1米,37.16厘米.