Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy program error?

Status
Not open for further replies.

mjb5038

Programmer
Feb 1, 2005
2
0
0
US
im creating a program that converts centimeters to inches then to yards,feet,inches. i have been working on it forever and cant seem to get the program to give me the right answers. can anyone help.please????????????
-Matt

Heres the code:

#include <iostream>
using namespace std;

int main()
{
int cm, inch, ft, yd;
float temp;

cout<<"Please enter a length in centimeters " <<endl;
cin >>cm;

//calculate centimeters to inches
inch=cm/2.54+0.5;

//calculate inches to yards
yd = inch/36;

//calculate the remainder of yards to find feet.
temp = inch%36;

//calculate yards into feet
ft = temp/3;

//calculate feet into inches
inch = ft%12;

// output into yards, feet, inches
cout<<"The answer is: " <<yd <<" yards " <<ft <<" feet " <<inch <<" inches ";

return 0;
}
 
Code:
int cm, inch, ft, yd;

All your variables are integers.

/Per
[sub]
www.perfnurt.se[/sub]
 
cm and inch should probably be of type double, not int, because you obviously won't be getting exact numbers. To calculate the number of feet, simply take the number of inches not in the yards (which you put in temp) and divide by 12, not 3, because there are 12 inches in a foot.

In other words, you need
ft = temp/12;

And your way of calculating inches doesn't make a lot of sense. Simply take the total number and subtract how many inches are in the ft and yd values:

inch -= 36 * yd + 12 * ft;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top