2016-11-10 80 views
0

我應該做一個代碼,從腳和英寸轉換爲米和釐米。但是當我運行我的代碼時,我沒有得到我應該得到的。例如,我輸入1英尺和0釐米。我應該得到0.3048米和0釐米,但我得到1米和0釐米。幫幫我!C++輸出轉換錯誤

#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; 
} 

任何幫助表示讚賞。謝謝!

+1

http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen

+0

'米* =變量1'相當於'米=米*變量1「,即你把乘積的結果賦給'我ters'。 –

回答

1
meters *= variable1; 
centimeters *= variable2; 

應該

variable1 *= meters; 
variable2 *= centimeters; 

什麼最後評論說:你並沒有使用該產品,你已經通過引用(variable1variable2)傳遞的變量,因此這些值不從原來的1和0的輸入改變。

+0

任何時候我的朋友,很高興提供幫助 –