Converting a Decimal into Inches

Hi guys, so in my program I need to have the user enter in 1 cin statement their height in decimal-like format, ie 5 feet 10 inches = 5.10 . I'm then required to take this piece of information and display the user's total height in inches. I understand that 12 inches = 1 foot so it would be (5x12) + 10 = 70 inches, and I could do that easily if I had the user enter in the feet/inches separately. I just don't understand how to perform it when its entered as just 1 piece of information.

Thank you.
Hi,

Try the std::floor() function to get the feet. Should be easy to get the inches after that :+)
Okay i'll bite, I don't know in the slightest how or where i'm suppose to apply that code. I've checked all my notes and our book. We're suppose to be inputting in the decimal as 1 piece of code..

cin >> height...The challenge is to separate the feet from the .inches....
You should also take advice given to you as genuine help instead of just blowing it off.
http://www.cplusplus.com/reference/cmath/floor/

Given a decimal value like 5.10, there exists a function (hint hint) that will allow you to get either that 5 or that 10 by itself.

How does your professor say you should distinguish 5'10" from 5'1". Because the naïve way to input both would be "5.1". (The correct way, according to what you've given us, would be 5.1 and 5.01, respectively.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    std::cout << "enter height as feet.inches (eg. 5' 1\" as 5.1, 5' 10\" as 5.10): " ;

    unsigned int feet ;
    unsigned int inches ;
    char dot ;

    if( std::cin >> feet >> dot >> inches && dot == '.' )
    {
        std::cout << feet << " feet and " << inches << " inches\n" ;

        if( inches > 12 ) std::cerr << "invalid value: inches must be in [0,12]\n" ;

        else { /* calculate and display total height in inches. */ }
    }

    else
    {
        std::cerr << "badly formed input.\n" ;
    }
}
If we assume that no one is more than 10 feet tall, then you can use std::isdigit with each character. This will determine the feet, then skip whatever after that until the next digit is found (possibly two digits), which will be the inches. Then the user can freely type in 5.10 or 5'10 or 5'10" or 5 foot 10 or 5 feet 10 or whatever and the feet and inches will always be obtained using std::stoi (and checking that the inches is less than 12).
Last edited on
@prestokeys
Very true. Unfortunately, the OP's assignment is specifically to separate a floating point value into integer and fractional components...

If he could read it as a string then then input might as well be the simple 5'10" .
Topic archived. No new replies allowed.