J_Ray93
J_Ray93

Reputation: 9

Having an issue with meter conversion to feet and inches in c++

My program converts feet and inches to meters just fine, but it will always add a random amount of feet on the meter to feet and inches conversion. I can not figure out why. Any advice would be greatly appreciated. EX) it states that 1.3208026m is equal to 8ft 4in when it is actually 4ft 4in.

#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;


int feet;
double meters, inches;

int main()
{

        cout << "Enter feet " << endl;
        cin >> feet;
        cout << "Enter inches " << endl;
        cin >> inches;

        inches = feet * 12 + inches;
        meters = inches / 39.370;

        cout << setprecision(8) << meters << "m" << endl << endl;

        cout << "Enter meters. " << endl;
        cin >> meters;
        inches = meters * 39.37;
        while (inches > 12)
        {
            if (inches > 12)
                inches = inches - 12;
            feet++;

        }

    cout << feet << " ft and " << setprecision(8) << inches << "in" << endl;

    return 0;
}

Upvotes: 0

Views: 753

Answers (2)

Martin James
Martin James

Reputation: 24847

    if (inches > 12)
        inches = inches - 12;
    feet++;

No. Needs braces, else your feet will fail you.

Upvotes: 0

Dave the Sax
Dave the Sax

Reputation: 328

You need to reset feet back to zero after the first conversion.

Upvotes: 1

Related Questions