2016-11-10 6 views
0

Ich soll einen Code machen, der von Fuß und Zoll in Meter und Zentimeter konvertiert. Aber wenn ich meinen Code ausführe, bekomme ich nicht, was ich bekommen soll. Zum Beispiel gebe ich 1 Fuß und 0 Zentimeter ein. Ich sollte 0,3048 Meter und 0 Zentimeter bekommen, aber stattdessen bekomme ich 1 Meter und 0 Zentimeter. Hilfe!C++ Output Conversion Error

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

Jede Hilfe wird geschätzt. Vielen Dank!

+1

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

+0

'Meter * = Variable1' entspricht" Meter = Meter * Variable1', dh Sie ordnen das Ergebnis der Multiplikation 'me zu ". –

Antwort

1
meters *= variable1; 
centimeters *= variable2; 

sollte

variable1 *= meters; 
variable2 *= centimeters; 

sein Was die letzte Bemerkung sagte: Sie sind nicht das Produkt zu den Variablen zuweisen, die Sie als Referenz übergeben haben (variable1 und variable2), so dass diese Werte nicht Ändern von Ihrer ursprünglichen Eingabe von 1 und 0.

+0

Jederzeit mein Freund, froh zu helfen –