calculating conversion question...
-
This is more on the mathematical side but I'm trying to convert metrics to english and vice versa. I've got the main coding done but my equations are off. Maybe it's because I've been working on this for about 3 days straight and just cannot see what I'm doing wrong, OR I'm just have no clue as to what I am doing in the first place. LOL here is the code I have for converting metric to english (grams and kilograms to pounds and ounces): const double KILOGRAMS_PER_POUND = .454; const double POUNDS_PER_KILOGRAM = 2.2046; const double GRAMS_PER_KILOGRAM = 1000; const double OUNCES_PER_POUND = 16; const double GRAMS_PER_POUND = 453.59; void convert_M_to_E( double £s, double& ounces, double grams, double kilograms) // Parameters: Grams and kilograms values to be converted to pounds and ounces // and references to variables that hold pounds and ounces // Returns: None // Calls: None { double total_pounds; // Grams and kilograms converted to pounds double total_grams; // Pounds and ounces converted to grams total_grams = grams + kilograms / GRAMS_PER_KILOGRAM; total_pounds = total_grams / GRAMS_PER_POUND; pounds = int( total_pounds ); ounces = pounds/OUNCES_PER_POUND; } // End of convert_M_to_E() And here is the code I have to convert English to Metric: void convert_E_to_M( double pounds, double ounces, double& grams, double& kilograms) // Parameters: pounds and ounces values to be converted to grams and kilograms // and references to variables that hold grams and kilograms // Returns: None // Calls: None { double total_pounds; // pounds and ounces converted to pounds double total_grams; // pounds and ounces converted to grams total_pounds = (pounds + ounces)/OUNCES_PER_POUND; total_grams = GRAMS_PER_POUND * total_pounds; grams = double (total_grams); kilograms = grams/GRAMS_PER_KILOGRAM; } // End of convert_E_to_M() I know I've got it completely wrong... can anyone help?? Thanks, Ruby
-
This is more on the mathematical side but I'm trying to convert metrics to english and vice versa. I've got the main coding done but my equations are off. Maybe it's because I've been working on this for about 3 days straight and just cannot see what I'm doing wrong, OR I'm just have no clue as to what I am doing in the first place. LOL here is the code I have for converting metric to english (grams and kilograms to pounds and ounces): const double KILOGRAMS_PER_POUND = .454; const double POUNDS_PER_KILOGRAM = 2.2046; const double GRAMS_PER_KILOGRAM = 1000; const double OUNCES_PER_POUND = 16; const double GRAMS_PER_POUND = 453.59; void convert_M_to_E( double £s, double& ounces, double grams, double kilograms) // Parameters: Grams and kilograms values to be converted to pounds and ounces // and references to variables that hold pounds and ounces // Returns: None // Calls: None { double total_pounds; // Grams and kilograms converted to pounds double total_grams; // Pounds and ounces converted to grams total_grams = grams + kilograms / GRAMS_PER_KILOGRAM; total_pounds = total_grams / GRAMS_PER_POUND; pounds = int( total_pounds ); ounces = pounds/OUNCES_PER_POUND; } // End of convert_M_to_E() And here is the code I have to convert English to Metric: void convert_E_to_M( double pounds, double ounces, double& grams, double& kilograms) // Parameters: pounds and ounces values to be converted to grams and kilograms // and references to variables that hold grams and kilograms // Returns: None // Calls: None { double total_pounds; // pounds and ounces converted to pounds double total_grams; // pounds and ounces converted to grams total_pounds = (pounds + ounces)/OUNCES_PER_POUND; total_grams = GRAMS_PER_POUND * total_pounds; grams = double (total_grams); kilograms = grams/GRAMS_PER_KILOGRAM; } // End of convert_E_to_M() I know I've got it completely wrong... can anyone help?? Thanks, Ruby
You've done a good job of asking a detailed question, and showing hte code you've written to try and solve the problem. However, it looks to me like C++ code, and not C++/CLI. Please ask in the right forum.
RubyM wrote:
double total_pounds; // Grams and kilograms converted to pounds double total_grams; // Pounds and ounces converted to grams total_grams = grams + kilograms / GRAMS_PER_KILOGRAM; total_pounds = total_grams / GRAMS_PER_POUND; pounds = int( total_pounds ); ounces = pounds/OUNCES_PER_POUND;
1 - NEVER declare your variables at the top, unless you use them right away. If this is actually C code, then you must do that, and you really are in the completely wrong forum. 2 - NEVER declare a variable without giving it a default value 3 - I do think you're doing the conversion wrong, but if your answer is only a little bit wrong, even double does not give exact answers.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
You've done a good job of asking a detailed question, and showing hte code you've written to try and solve the problem. However, it looks to me like C++ code, and not C++/CLI. Please ask in the right forum.
RubyM wrote:
double total_pounds; // Grams and kilograms converted to pounds double total_grams; // Pounds and ounces converted to grams total_grams = grams + kilograms / GRAMS_PER_KILOGRAM; total_pounds = total_grams / GRAMS_PER_POUND; pounds = int( total_pounds ); ounces = pounds/OUNCES_PER_POUND;
1 - NEVER declare your variables at the top, unless you use them right away. If this is actually C code, then you must do that, and you really are in the completely wrong forum. 2 - NEVER declare a variable without giving it a default value 3 - I do think you're doing the conversion wrong, but if your answer is only a little bit wrong, even double does not give exact answers.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
This is more on the mathematical side but I'm trying to convert metrics to english and vice versa. I've got the main coding done but my equations are off. Maybe it's because I've been working on this for about 3 days straight and just cannot see what I'm doing wrong, OR I'm just have no clue as to what I am doing in the first place. LOL here is the code I have for converting metric to english (grams and kilograms to pounds and ounces): const double KILOGRAMS_PER_POUND = .454; const double POUNDS_PER_KILOGRAM = 2.2046; const double GRAMS_PER_KILOGRAM = 1000; const double OUNCES_PER_POUND = 16; const double GRAMS_PER_POUND = 453.59; void convert_M_to_E( double £s, double& ounces, double grams, double kilograms) // Parameters: Grams and kilograms values to be converted to pounds and ounces // and references to variables that hold pounds and ounces // Returns: None // Calls: None { double total_pounds; // Grams and kilograms converted to pounds double total_grams; // Pounds and ounces converted to grams total_grams = grams + kilograms / GRAMS_PER_KILOGRAM; total_pounds = total_grams / GRAMS_PER_POUND; pounds = int( total_pounds ); ounces = pounds/OUNCES_PER_POUND; } // End of convert_M_to_E() And here is the code I have to convert English to Metric: void convert_E_to_M( double pounds, double ounces, double& grams, double& kilograms) // Parameters: pounds and ounces values to be converted to grams and kilograms // and references to variables that hold grams and kilograms // Returns: None // Calls: None { double total_pounds; // pounds and ounces converted to pounds double total_grams; // pounds and ounces converted to grams total_pounds = (pounds + ounces)/OUNCES_PER_POUND; total_grams = GRAMS_PER_POUND * total_pounds; grams = double (total_grams); kilograms = grams/GRAMS_PER_KILOGRAM; } // End of convert_E_to_M() I know I've got it completely wrong... can anyone help?? Thanks, Ruby