Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

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


(!)

supergreene's Avatar
supergreene supergreene is offline
Junior Member with 26 posts.
THREAD STARTER
 
Join Date: May 2001
31-Oct-2001, 03:43 AM #1
Question Converting feet & inches to meters & centimeters in C++
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
MacFromOK's Avatar
Senior Member with 1,741 posts.
 
Join Date: Mar 2001
Location: Oklahoma
Experience: idiota de la aldea
31-Oct-2001, 09:02 AM #2
Post what you have done on it so far.
Rockn's Avatar
Rockn Rockn is offline   Rockn has a birthday soon!
Computer Specs
Member with 21,189 posts.
 
Join Date: Jul 2001
Location: Somalia of the North, MN
Experience: Disenfranchised American
31-Oct-2001, 11:13 AM #3
Just set up variables for each item...meters, and feet. Feet = inches/12. Do the same thing with centimeters and milimeters.
supergreene's Avatar
supergreene supergreene is offline
Junior Member with 26 posts.
THREAD STARTER
 
Join Date: May 2001
31-Oct-2001, 05:09 PM #4
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
MacFromOK's Avatar
Senior Member with 1,741 posts.
 
Join Date: Mar 2001
Location: Oklahoma
Experience: idiota de la aldea
31-Oct-2001, 10:29 PM #5
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
supergreene's Avatar
supergreene supergreene is offline
Junior Member with 26 posts.
THREAD STARTER
 
Join Date: May 2001
01-Nov-2001, 02:04 AM #6
That solved my problem. Thank you.

-Mike
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑