Okay, so i need to enter a random number that is in centimeters, this will then be converted into Yards, Feet, and inches... all of these must be integers in the output and should look something along the lines of this.

Example Output:

3 Yard(s)  20 Feet  6 Inches

The outputs im getting include large numbers in inches and feet but nothing in yards, however they are not converted for the propper measurements for each section.

Ive been dinking around with this problem for a couple hours and im just flat out stuck, any help would be appreciated.

Thanks,
Exo1337

My Code:

#include <iostream>

using namespace std;

const int INCHES = 2.54;
const int FEET = 30.48;
const int YARDS = 91.44;

int main()
{
    double centimeters;

    cout << "Please input a length that will be exspressed in centimeters: ";
    cin >> centimeters;
    cout << endl;

    cout << "You entered: " << centimeters << endl;

    cout << static_cast<int> (centimeters / INCHES) << "Inches" << endl;
    centimeters = centimeters % INCHES;

    cout << static_cast<int> (centimeters / FEET) << "Feet" << endl;
    centimeters = centimeters % FEET;

    cout << static_cast<int> (centimeters / YARDS) << "Yard(s)" << endl;
    centimeters = centimeters % YARDS;


    return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I suppose i should include my current output looks like:

You Entered ##
Inches ##
Feet: ##
Yards: 0

Question : do you want your output in integer or double form? You are using doubles and integers and it is creating issues.

Ok, if you want accurate conversion you should start by working all of your variables as doubles then convert them to integers when displaying. For example, make your constants doubles and work with them that way, then cast them to integers before displaying them.

If you would like I have made a code that does what you are trying to do, if you would like to do it yourself just with my help (that way would be better for you) then say that.

I also just noticed something else about your code, you need to start with yards. If you don't start with the largest conversion, the conversions will stop before you reach yards, I don't know, however, how you even reach feet. Anyway, start with yards, then feet, then inches.

I learn alot better by seeing so if you have a code i would love to see it because ive been working on this for quite some time but to no avail

The answer needs to be in integers ^^;

That's the ANSWER. The calcualation must be done in doubles.
your first situation is setting an integer equal to a double for yards.
It's undefined from get-go.
Also you will get a double in the the yards calculation unless the number generated for centimeters is greater than or equal to the the number centimeters in a yard ! It going to be <1 pretty much. How do you propose to dispaly that result in integer????

I apologize for the wait, I wrote the code correctly then confused myself and went back to fix an error that didn't exist :P. Anyway, here is the code.

#include <iostream>

using namespace std;

const double INCHES = 2.54;
const double FEET = 30.48;
const double YARDS = 91.44;

int main()
{
    double centimeters;
    int dispyard, dispfeet, dispinch;

    cout << "Please input a length that will be expressed in centimeters: ";
    cin >> centimeters;
    cout << endl;

    cout << "You entered: " << centimeters << endl;
                                          //typecasting occurs after calculation when to int, occurs before calculation when converting back
    dispyard = int (centimeters / YARDS); //converts centimeters to yards then typecasts
    centimeters -= double (dispyard) * YARDS;//subtracts what was used for yards converts dispyard back to double
    dispfeet = int (centimeters / FEET);  //uses remainder to calculate feet
    centimeters -= double (dispfeet) * FEET;//subtracts what was used for feet converting to double first
    dispinch = int (centimeters / INCHES);//uses overall leftover for inches

    cout << "Yard(s) : " << dispyard << endl;
    cout << "Feet    : " << dispfeet << endl;
    cout << "Inch(es): " << dispinch << endl;

return 0;
}

If you are converting cm to yd,ft,in, first thing to do is convert your centimeters into inches. All this can be done with integers, you'll only be off by about 1/2 inch as long as you round properly..

Then, using the division you can get the number of yards. The remainder will be the number of inches left. Then do the same for feet. What's left is inches.

Convert what I have above into a set of step-by-step instructions, then you should be able to code the program easily.

By the way, this will also alleviate two of the three conversion factors in your posted program.

That is true, but I was trying to accomplish his purposes with the methods he proposed.

Line 20 is still going fail. what is the input is 10cm?
The whole casting thing is generally frowned upon in the programming community because it defeats the purpose of C++ strong typing.
There are better ways to do this with functions like ceiling() and floor().
Did you test this code?

I apologize for the wait, I wrote the code correctly then confused myself and went back to fix an error that didn't exist :P. Anyway, here is the code.

#include <iostream>

using namespace std;

const double INCHES = 2.54;
const double FEET = 30.48;
const double YARDS = 91.44;

int main()
{
    double centimeters;
    int dispyard, dispfeet, dispinch;

    cout << "Please input a length that will be expressed in centimeters: ";
    cin >> centimeters;
    cout << endl;

    cout << "You entered: " << centimeters << endl;
                                          //typecasting occurs after calculation when to int, occurs before calculation when converting back
    dispyard = int (centimeters / YARDS); //converts centimeters to yards then typecasts
    centimeters -= double (dispyard) * YARDS;//subtracts what was used for yards converts dispyard back to double
    dispfeet = int (centimeters / FEET);  //uses remainder to calculate feet
    centimeters -= double (dispfeet) * FEET;//subtracts what was used for feet converting to double first
    dispinch = int (centimeters / INCHES);//uses overall leftover for inches

    cout << "Yard(s) : " << dispyard << endl;
    cout << "Feet    : " << dispfeet << endl;
    cout << "Inch(es): " << dispinch << endl;

return 0;
}

Thanks much, the awnswer was like right in front of me the whole tie hehehe

I did test the code, and it did work. I realize it is not the most efficient way, but like I said, I was accomplishing his purpose with his methods.

I did test the code, and it did work. I realize it is not the most efficient way, but like I said, I was accomplishing his purpose with his methods.

If his method is a problem, you are allowed to tell him so and explain why. I completely disagree with dividing the values by those floating point conversions. More can go wrong that way.

With my style, the fewest "errors by floating inexactness" will occur. And you don't need so much floating point math.

This article has been dead for over six months. Start a new discussion instead.