Converting height from decimal to wholenumber int

How can i change a persons height form decimal format in c++ to a whole number int. an example would be a 6'4 person would be 6.04 and i need to convert that decimal to a whole number int of 76 inches. I tried an assignment statement that took the user enter decimal height and multiplied it by 12 and made the new variable an int but that of course drops the decimal and some of the inches.

[code]
I didn't understand what exactly you are trying to say, but from what I could make of your post, I understood that you basically want to convert from feet to inches? (also if I am not mistaken you mean 6,4 not 6,04, excuse me if I am wrong but I am not familiar with the American metric system)

If that's the case it can be done the following way. Read the value in a double variable. Then, create a new variable where you store the previous value * 12. Here's a pseudo code example to help you

declare height_initial (in C++, double)
Read height_initial
declare height_final (also double)
height_final = height_initial*12
Print height_final
No. I'm having the user enter there height in decimal format so an individual who is 6 feet 5 inches would enter into the program 6.05 or an individual who's 4 feet 9 inches would enter 4.09. Then i'm taking that number and converting it into only inches with no remainder and having the program display the final height as inches only. I originally tried multiplying the decimal imputed by the user by 12 but that still leaves the remainder of the decimal unaccounted for. so if i entered 6.04 the program outputted 72 inches but really its 76.
Ah I get it now. So if you want to say for example 6 feet 11 you'd use 6.11. First of all, to do this, you need to find out how to take apart the decimal from the integer value in a double variable. The following code shows exactly how. I'm not sure if you've gone into functions yet, so I'll just show the non-functional way of doing this.

1
2
3
4
5
6
7
8
9
double initialValue;
cin>>initialValue;

int naturalValue = 0;
double decimalValue = 0;

decimalValue = initialValue;
naturalValue = initialValue;
decimalValue -= naturalValue;

Let's see how this works. Let's take it step by step using an example, let's say 5.11

First of all you declare the variables, that's easy.
Then decimalValue = initialValue so here decimalValue is 5.11
Then naturalValue = initialValue so here naturalValue is 5 because it just leaves out the decimal
Final step decimalValue -= initialValue which here is 5.11 - 5 = 0.11.

So we've seperated the decimal value from the integer value. What do you think we must do now? I'll leave that to yourself (because it's simple math) but if you have any questions just ask.
Last edited on
If you convert a floating point value into integer, the remainder is dropped.

If you deduct the the created integer from the original floating point value, you should get the reminder.

However, floating point math is not what you intuitively would expect. Therefore, do consider reading the "whole" into integer, skipping the dot, and then reading the inches to second integer.


PS. As a regular user I'm 5.99 tall, envious to those of full 6 feet. In other words, no matter what you do, do validate the user input first.
@keskiverto
Here, the system which is used is not the normal numbering system where a decimal point is a fraction of 10, 100, 1000 etc (eg 5/10, 45/100) but the decimal place represents the amount of inches there are in the height of the person. For example, 6.11 here, is six feet plus 11 inches. So in inches it would be
(0.11 * 100) = 11
plus
(6*12) = 72.
So it will be 83 (72+11)
So, you just need to check if the inches value is bigger or equal to 0.13 to proceed, that way you validate the info. If it isn't read again or return error or close program or whatever.
Last edited on
Thanks jgg2002 it worked perfectly I appreciate the insight.
Topic archived. No new replies allowed.