Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. calculating conversion question...

calculating conversion question...

Scheduled Pinned Locked Moved Managed C++/CLI
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RubyM
    wrote on last edited by
    #1

    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

    C L 2 Replies Last reply
    0
    • R RubyM

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      R 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        R Offline
        R Offline
        RubyM
        wrote on last edited by
        #3

        sorry about posting in the wrong forum... I'm a student, and to be honest, I don't know what the difference is between C++/CLI or any other C++ programing. All I know is that it's C++ and this forum has "C++" in the title. Sorry again. Ruby

        1 Reply Last reply
        0
        • R RubyM

          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

          L Offline
          L Offline
          lafleon
          wrote on last edited by
          #4

          Hello, Jus try: 1. total_grams = grams + kilograms * GRAMS_PER_KILOGRAM; 2. ounces = pounds*OUNCES_PER_POUND; 3. total_pounds = pounds + ounces/OUNCES_PER_POUND; Regards,

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups