# Doing the opposite in a conversion?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-10-2009
CplusplusNewb
Doing the opposite in a conversion?
Ok the conversion from feet and inches to meters and centimeters (meters being an int and the remainder being converted into cm), but how can I do just the opposite?

See here's the code for feet and inches to meters and cm and it works perfectly:

Code:

```#include <iostream> using namespace std; void userInput(double& feet, double& inches); void convertUStoMetric(double& meters, double& centimeters); void output(double meters, double centimeters); int main() {         double unit, subunit;         char redo = 'y';                 while (redo == 'y' || redo == 'Y')         {                                 userInput(unit, subunit);                         convertUStoMetric(unit, subunit);                         output(unit, subunit);                                 cout << "Would you like to run it again? Y/N: ";             cin >> redo;             cout << "\n";         }                 system("pause");     return 0;  } void userInput(double& feet, double& inches) {         cout << "Enter the feet and inches with a space in between each: ";         cin >> feet;         cin >> inches;         cout << "\n";        } void convertUStoMetric(double& meters, double& centimeters) {         double feet = meters;         double inches = centimeters;         meters = (feet + inches/12) * 0.3048;         centimeters = (meters - (int)meters) * 100;         //5 feet = 1.524 meters         //.524 meters = 52.4 cm         //6 inches = 15.24 cm         //total = 1 meter and 67.64 cm         meters = (int)meters; } void output(double meters, double centimeters) {         cout << "The converted measurements are " << meters << " meters and " << centimeters << " centimeters.";         cout << "\n\n";        }```
But when I try just the opposite for meters and cm to feet and inches, the feet is correct, but the cm is completely messed up.

Code:

```#include <iostream> using namespace std; void userInput(double& meters, double& centimeters); void convertMetricToUS(double& feet, double& inches); void output(double feet, double inches); int main() {         double unit, subunit;         char redo = 'y';                 while (redo == 'y' || redo == 'Y')         {                                 userInput(unit, subunit);                         convertMetricToUS(unit, subunit);                         output(unit, subunit);                                 cout << "Would you like to run it again? Y/N: ";             cin >> redo;             cout << "\n";         }                 system("pause");     return 0;  } void userInput(double& meters, double& centimeters) {         cout << "Enter the meters and centimeters with a space in between each: ";         cin >> meters;         cin >> centimeters;         cout << "\n";        } void convertMetricToUS(double& feet, double& inches) {         float meters = feet;         float centimeters = inches;         feet = (meters + centimeters*2.54) / 0.3048;         inches = (feet - (int)feet) / 100;         feet = (int)feet;                 //5 meter = 16.404 199 475 feet         //0.404199475 feet = 0.00404199 inches         //6 centimeter = 2.362 204 724 4 inch         //total = 16 feet and         //onlineconversion.com } void output(double feet, double inches) {         cout << "The converted measurements are " << feet << " feet and " << inches << " inches.";         cout << "\n\n";        }```
If you want to try it out, put in 5 0, and you should get 16 feet and 0 inches. But when I put 5 6, I get some crazy numbers. The cm to inches conversion is working how it's supposed to.

Any advice or tips on what I can do to fix this?

P.S. It's also not the exact opposite because I tried fixing it and failed, but I get essentially the same thing.
• 12-10-2009
CplusplusNewb
Quote:

Originally Posted by sufoode
Code:

```void convertMetricToUS(double& feet, double& inches) {         float meters = feet;         float centimeters = inches;         feet = (meters + centimeters/100) * 3.2808399;         inches = (feet - (int)feet) * 12;         feet = (int)feet; }```

Where'd you get 3.2808399 from?
• 12-10-2009
CplusplusNewb
Ah ok thanks for making me feel so stupid *thumbs up*
• 12-10-2009
CplusplusNewb
Now my final question. In my book it says I have to ask the user whether they would like to covert from feet and inches to meters and cm, or from meters and cm to feet and inches.

And I would have an if/else statement.

But I'm kind of confused. So combining these two functions, there would be 6 functions in total? And how would the if/else statement look like? We haven't exactly learned how to make an if/else statement that chooses between two programs yet.

I'm ahead of the rest of the class because I got these two done, so he hasn't explained it yet.
• 12-10-2009
strange
What you can do, is something like this:

Code:

```#include <iostream> . . . double convert_m_to_cm(double m) {     return m/100.0; } double convert_cm_to_m(double cm) {     return m * 100.0; } double convert_feet_to_inches(double feet) {     return feet * 12.0; } double convert_inches_to_feet(double inches) {     return inches / 12.0; } double inches_to_cm(double inches) {     return inches / (whatever the ratio is); } double cm_to_inches(double cm) {     return cm / (the other ratio); } int main(int argc, char *argv[]) {     do {         double value = 0.0;         /* Step one: what is the original unit? */         int first_unit_type = -1;         /* Print out a message stating that 1 is cm, 2 is m, 3 is inches, 4 is feet . . . */         if(first_unit_type == 2) value = convert_m_to_cm(value);         else if(first_unit_type == 3) value = convert_inches_to_cm(value);         else if(first_unit_type == 4) value = convert_feet_to_inches(convert_inches_to_cm(value));         /* Step two: what is the desired unit? */         int second_unit_type = -1;         if(first_unit_type == 2) value = convert_cm_to_m(value);         /* And so on for the other types . . . */     } while(true);     return 0; }```
See, by converting to a standardized base (I chose cm here, but you can use anything), you can then convert it into any desired base. I left out the printing, input, and error-checking statements, mainly because I didn't feel like typing them all out. ;) When the user no longer wants to convert more measurements, then use a break statement to break out of the do/while loop.

Hope that helps.
• 12-11-2009
Elysia
sufoode, you need to stop simply handing out all solutions!
The OP needs to work this out him/herself. We are to guide, not solve.
It would also be better if you used constants instead of "magic numbers." A constant with a value tells thousand words rather than a number somewhere in the midst of code.
• 12-11-2009
Elysia
A book and a teacher are different things.
They put solutions in books, but you don't see teachers giving out the solutions directly, do you?
• 12-11-2009
laserlight
Quote:

Originally Posted by sufoode
my teacher does... but i was home schooled

The culture here is to avoid giving out a working solution, though here are exceptions, e.g., it is clear that the homework deadline is over (but this is dubious since it can be faked), the learner has arrived at a working solution but you wish to demonstrate a better solution, or the learner has put in sufficient effort and made some progress but appears truly stuck despite his/her best efforts. In the latter case, a related example would likely still be better than a direct solution.
• 12-11-2009
IceDane
Quote:

Originally Posted by sufoode
my teacher does... but i was home schooled

Cool story bro. I'm not sure why this isn't obvious to you, but we aren't this guy's mother. We won't spoon feed him or hand him answers. He doesn't learn that way.
• 12-11-2009
Elysia
Quote:

Originally Posted by sufoode
my teacher does... but i was home schooled

I think you have a unique teacher...
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last