0

I'm supposed to make a code that converts from feet and inches to meters and centimeters. But when I run my code, I don't get what I should get. For example, I input 1 foot and 0 centimeters. I should get 0.3048 meters and 0 centimeters but instead I'm getting 1 meters and 0 centimeters . Help!

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

Any help is appreciated. Thanks!

2

1 Answer 1

1
meters *= variable1;
centimeters *= variable2;

should be

variable1 *= meters;
variable2 *= centimeters;

What the last comment said: you're not assigning the product to the variables that you've passed by reference (variable1 and variable2), so those values are not changing from your original input of 1 and 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.