# Thread: Doing the opposite in a conversion?

1. ## 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.

2. 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?

3. Ah ok thanks for making me feel so stupid *thumbs up*

4. 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.

5. 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.

6. 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.

7. 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?

8. 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.

9. 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.

10. Originally Posted by sufoode
my teacher does... but i was home schooled
I think you have a unique teacher...

11. Well I don't know about you guys, but I actually learn better if I'm just spoon fed the answers, then I can easily do something of the same level naturally. But that's just me.

Anyways I was able to combine both programs and ask the user which converter they would like to use with an if/else statement with my friend.

You guys really helped though, however sufoode actually used the same thing I was doing, like the void functions and nothing like print or something because I haven't even learned that material yet.

For some reason, whenever I ask a question here and show my code, half of everyone that replies uses some different output like //print and etc that I haven't learned yet which makes it really hard to even progress when I'm reading your advice. For example that person that also posted code for me used something that I wasn't requesting and I had no idea what it was.

But I do agree when it comes to code it's better to let me be taught or else I won't understand at all for upcoming programming projects.

12. Originally Posted by sufoode
Why do they put answers in the back of math textbooks?
So that you know if the solution you got was correct. They put answers only, not the step by step process. With programming, the answer is given in as the objective, and you write tests to make sure your program has the desired behavior.

The point of writing practice programs is to gain a familiarity with the language and how to implement other principles taught in the course. Looking at working code can be helpful too, and that's why textbooks come with examples, but people come here with questions of how to get their practice code to work. Showing solution is rarely helpful, because it is not practice, and even though they may see how to solve that particular problem, the broader purpose of the excersize is not achieved.

13. Just some food for thought...

Code:
```

/*
The strange template business is a workaround for not
being able to call virtual functions within constructors
*/
template < typename Derived >
struct length
{
template < typename Other >
length( length< Other > const& rhs )
: data( rhs.standard( ) / ratio( ) )
{    }

length( double value )
: data( value )
{    }

/*
The simulated virtual function
*/
double ratio( void ) const
{
return static_cast< Derived const* >( this )->ratio( );
}

/*
Standard conversion (eg: to metric)
*/
double standard( void ) const
{
return data * ratio( );
}

/*
Implicit conversion, for convenience
*/
inline operator double ( void ) const
{
return data;
}

/*
The data, in the derived format, obviously
*/
double data;
};

#define DEFINE_LENGTH( type, factor )\
struct type : length< type >\
{\
template < typename Other >\
type( length< Other > const& rhs )\
: length< type >( rhs )\
{    }\
\
type( double value = 0 )\
: length< type >( value )\
{    }\
\
double ratio( void ) const\
{\
return factor;\
}\
}

/*
Define some new types, and their relationship to metric
*/
DEFINE_LENGTH( meters, 1.0 );
DEFINE_LENGTH( miles, 1609.344 );
DEFINE_LENGTH( yards, 0.9144 );
DEFINE_LENGTH( feet, 0.3048 );
DEFINE_LENGTH( inches, 0.0254 );

#include <iostream>

/*
Now we can automatically convert between any type!
*/
int main( void )
{
using namespace std;
miles mile = 1;
cout << "Meters per mile: " << meters( mile ) << endl;
cout << "Yards per mile: " << yards( mile ) << endl;
cout << "Feet per mile: " << feet( mile ) << endl;
cout << "Inches per mile: " << inches( mile ) << endl;
/*
Or whatever...
*/
cout << "Miles per mile: "
<< miles( inches( meters( yards( inches( mile ) ) ) ) ) << endl;
return 0;
}```

14. Using structs instead of classes, using static_cast, using macros, not hiding class member data, lack of inheritance-type keyword...
Tsk, tsk. This example is convulted, and some of even bad practice.

EDIT:
My version.
No using of struct instead of class. Proper hiding of member data. No pesky macros, no static cast.
Code:
```#include <iostream>

template<int Type>
class length
{
public:
template<int Other>
length(const length<Other>& rhs): m_data( rhs.data() / factor() ) { }
length(double value): m_data(value) { }

double factor() const { return Factors<Type>::factor; }
double data() const { return m_data * factor(); }
operator double () const { return m_data; }

protected:
double m_data;
};

template<int Type> struct Factors { static const double factor; };
const double Factors<0>::factor = 1.0;
const double Factors<1>::factor = 1609.334;
const double Factors<2>::factor = 0.9144;
const double Factors<3>::factor = 0.3048;
const double Factors<4>::factor = 0.0254;

typedef length<0> meters;
typedef length<1> miles;
typedef length<2> yards;
typedef length<3> feet;
typedef length<4> inches;

int main()
{
miles mile = 1;
std::cout << "Meters per mile: " << meters( mile ) << std::endl;
std::cout << "Yards per mile: " << yards( mile ) << std::endl;
std::cout << "Feet per mile: " << feet( mile ) << std::endl;
std::cout << "Inches per mile: " << inches( mile ) << std::endl;

std::cout << "Miles per mile: " << miles( inches( meters( yards( inches( mile ) ) ) ) ) << std::endl;
return 0;
}```

15. Originally Posted by Sebastiani
Just some food for thought...
I think it was CornedBee who pointed out Boost.Units the last time something like this was discussed.