1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Converting feet & inches to meters & centimeters in C++

Discussion in 'Software Development' started by supergreene, Oct 31, 2001.

Thread Status:
Not open for further replies.
Advertisement
  1. supergreene

    supergreene Thread Starter

    Joined:
    May 15, 2001
    Messages:
    26
    Me = Beginner. I'm trying to convert feet and inches to meters and centimeters in C++. I also need to convert back to feet and inches. This is for school and I've just about burned out trying to figure this out. I need to keep both units of measurement separate because output has to read:

    " " meters, " " centimeters
    or
    " "feet, " "inches

    Anybody have a suggestion?

    -Mike
     
  2. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    Post what you have done on it so far. :)
     
  3. Rockn

    Rockn

    Joined:
    Jul 29, 2001
    Messages:
    21,189
    Just set up variables for each item...meters, and feet. Feet = inches/12. Do the same thing with centimeters and milimeters.
     
  4. supergreene

    supergreene Thread Starter

    Joined:
    May 15, 2001
    Messages:
    26
    I don't really have anything yet. I can do the basic conversions but I don't know how to seperate the integer and the decimal portion. For example:

    int feet, inches;
    double centimeters, meters;

    feet = 5
    inches = 6

    centimeters = ((feet * 12) + inches) * 2.54
    meters = centimeters / 100

    Result: 5ft 6in = 1.6764m
    Problem: It should read, 5ft 6in = 1m 67.64cm

    How can I convert "1.6764" to "1", ".6764". I thought I would be able to use the % operator but it is not allowed with a variable of type double.

    -Mike
     
  5. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    You can use the "modf" function to separate
    decimal numbers:
    Code:
    #include &ltiostream.h>
    // Some compilers may require "math.h" to be included.
    int main()
    {
      double number = 1.6764;
      double part1, part2;
    
      part2 = modf(number, &part1);
    
      cout <<  part1 << " and " << part2;
      return 0;
    }
    
    Cheers, Mac
     
  6. supergreene

    supergreene Thread Starter

    Joined:
    May 15, 2001
    Messages:
    26
    That solved my problem. Thank you.

    -Mike
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/57180