TechArena Community How to convert centimeter into feets and inches

#1
02-09-2009
 Member Join Date: Jun 2009 Posts: 1,205
How to convert centimeter into feets and inches

I would like to write a C++ program where a user is asked to enter the height in centimeters and then it displays the height in centimeters, feet and inches. Now here the condition is that the program should allow centimeters and inches in decimals. Another big question: how to do so? How can create a C++ conversion program from centimeters to feet and inches?
#2
02-09-2009
 Member Join Date: May 2008 Posts: 2,293
Re: How to convert centimeter into feets and inches

Well I can help you and give you the basic idea of the code but sorry to tell you that I don't have enough time to explain you the full code. You will have to do the trick. The main step (statement) of your code would be

Code:
`inches = inches - feet*12;`
First convert the cms to inches and then calculate the feet from the calculates inches.
#3
02-09-2009
 Member Join Date: Nov 2005 Posts: 1,323
Re: How to convert centimeter into feets and inches

Below is the code to convert centimeters into feets and inches:

Code:
```const float cmtoinch = 2.54;

void main()
{
float cm, inch;
int feet;
do {
cout<<"Enter a height in centimeters: ";
cin>>cm;
if (cm <= 0) break;
inch = cm / cmtoinch;;
feet = inch/12;
inch = inch - (feet*12);
cout<< cm << " = " << feet<< " feet, " << inch << " inches" << endl;
}while(cm > 0);
cout<<"Done!";
}```
#4
02-09-2009
 Member Join Date: May 2008 Posts: 2,383
Re: How to convert centimeter into feets and inches

You can even add the "try...catch" block to handle any unconditional exceptions. It should be something as below:

Code:
```try
{
if(inch < 0)
throw inch;

if(feet < 0)
throw feet;
}

catch (Exception e)
{
cout << "Negative values not accepted." << error << endl;
}```
__________________
The FIFA Manager 2009 PC Game

 Tags: