How to subtract numbers after decimal and place in new column?

myka316 (11)
Is there a way to take numbers after a decimal and put them into a completely new category? (i.e. the conversion of feet to meters. Typically it would result in (feet * .3048) => (4 * .3048) = 1.2192 or 1 meter 21.91 centimeters), Is there a way to tell my program to assign the "1" to meters and place the "21.91" in a centimeters column that I create?
Last edited on
Chervil (812)
1
2
3
4
5
6
7
8
9
10
11
    const double ftom = 0.3048;
    double feet   = 4;
    double metres = feet * ftom;

    int m  = metres;
    int cm = (metres - m) * 100.0;

    cout << "feet: "   << feet   << endl;
    cout << "metres: " << metres << endl;
    cout << m  << "m  ";
    cout << cm << "cm" << endl;

Output:
feet: 4
metres: 1.2192
1m  21cm

If cm is of type double instead of int, you can keep the millimetres too.
Last edited on
myka316 (11)
Thank you very much for the response Chervil. Is there a way for me to create a function that will utilize that formula?

Would something like this work?

1
2
3
4
5
6
7
8
double Conversion (double feet, double ftom, double meters, double cm)
{

double meters = feet * ftom;

int m = meters;
int cm = (meters - m) * 100;
}


I'm basically trying to allow a user to input a number for feet/inches to be converted to meters/cm. My current coding allows for a user to get the calculations but will not convert the numbers following the decimal in "feet" to centimeters.
Last edited on
Chervil (812)
I would place the value 0.3048 inside the function. No point passing it as a parameter, the value won't change.

As it stands, your function doesn't return any results at all.

See the tutorial for how to return one or several values.
The first page covers the basics while the second describes passing by reference which is what you need for returning two or more values.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
Registered users can post here. Sign in or register to post.