# Thread: Here is my code I cant figure it out

1. ## Here is my code I cant figure it out

//Problem is i cant get my inches left over i cant think in my head what i have to do!!!!! ive gotten this far

Code:
```#include <iostream>

using namespace std;

//CONSTANTS
const double CM_PER_INCH	= 2.54;
const double INCH_PER_FOOT	= 12;
const double INCH_PER_YARD	= 36;

int main ()
{

//VARIABLES
int feet;
double cm;
double totalinches;
int totalyards;
int roundedinch;
int inchesleft;
//END

cout << "Please enter the length in centimeters: ";
cin >> cm;

//converting to inch

totalinches = cm / CM_PER_INCH;

//roundedinches

roundedinch = totalinches + .05;

//TOTAL YARDS

totalyards = roundedinch / INCH_PER_YARD;

//TOTAL FEET

feet = (roundedinch - (totalyards * INCH_PER_YARD )) / INCH_PER_FOOT;

//LEFT OVER INCHES

inchesleft =

//Results

cout << " Total Yards: " <<totalyards << " Total Feet: " <<feet << " Inches: "<<inchesleft <<endl;

return 0;

}```

2. You were getting 2.16667 yards as a double, which would mean you have 2 yards, and .16667 left over to convert to inches. The problem was that you were using int for yards, meaning it left you with 2 yards, so no feet would be needed, and your inches algorthm wasn't filled in.. it would have been whatevers left over in feet (e.g. 3.0374 would be 3 feeet, .0374 feet to convert to inches).

You could do this by static casting<int> and float, or (float) and (int) a lot, but I suggest just doing all the calculations in double, then static_cast the final outcome.

Code:
```#include <iostream>

using namespace std;

//CONSTANTS
const double CM_PER_INCH	= 2.54;
const double INCH_PER_FOOT	= 12;
const double INCH_PER_YARD	= 36;

int main ()
{
//VARIABLES
double totalfeet;
double cm;
double totalinches;
double totalyards;
double inchesleft;
//END

cout << "Please enter the length in centimeters: ";
cin >> cm;

//converting to inch

totalinches = cm / CM_PER_INCH;

//TOTAL YARDS

totalyards = totalinches / INCH_PER_YARD;

//TOTAL FEET
totalfeet = ((totalyards - static_cast<int>(totalyards)) * INCH_PER_YARD) / INCH_PER_FOOT;

//LEFT OVER INCHES

inchesleft = (totalfeet - static_cast<int>(totalfeet)) * INCH_PER_FOOT;

//Results
cout << "Total Yards: " << static_cast<int>(totalyards)
<< " Total Feet: " << static_cast<int>(totalfeet)
<< " Inches: "<< static_cast<int>(inchesleft) <<endl;

cin.ignore();
cin.get();

return 0;
}```
The reason its totalfeet - static_cast<int>(totalfeet) is because you are taking the 2.16667 yards, then making yards an int, making it 2 yards, and minusing: 2.1667 yards - 2 yards = 0.1667 yards left to convert to feet. And the same goes for feet-> inches.