New to C++, help please!

Write your question here.

I having a problem with converting centimeters to yards, feet and inches. I can get the first test variable(312cm) to come close to the output that it's suppose to be but 'm always off by an inch. I tried my next variable of 300cm and I started getting negative numbers. please help, where did I mess up?
Put the code you need help with here.

#include <iostream>
using namespace std;

const double CENT_PER_IN = 2.54;
const int IN_PER_FT = 12, IN_PER_YD = 36;

int main()
{
float cent;
int in,
ft,
yd,
totalinches;

cout << "Enter the centimeter amount to be calculated: " << endl;
cin >> cent;
cout << endl;

cout << "The centimeter(s) you entered is:" << endl;
cout << cent << endl;

totalinches = static_cast <float> (cent) / CENT_PER_IN;
yd = totalinches / IN_PER_YD;
ft = (totalinches - (IN_PER_YD * yd)) / 12;
in = (totalinches % (IN_PER_YD * yd)) - IN_PER_FT;

cout << "Your metric to standard breakdown is: " << endl;

cout << yd << "yards" << endl;
cout << ft << "feet" << endl;
cout << in << "inch(es)" << endl;

system("pause");

return 0;
}
1
2
3
4
5
totalinches =  (cent) / CENT_PER_IN;
yd = totalinches / IN_PER_YD;
int remaining = totalinches - yd*36;
ft = remaining / IN_PER_FT;
in = remaining - ft*12;


I'm pretty sure this is right, but I'd check it if I were you. Also, you could combine equations to remove the remaining variable, but I thought it would show what was happening better with remaining included.
ill try it and see if it works out and post feedback. Thank you for the idea!
So I havent tried it yet but the more I look at your example, wouldn't it be easier to just learn how to use the "%" for remainders?
Topic archived. No new replies allowed.