1

Screenshot of the outputs i got for the codeHow to code to separate the whole part and decimal part

I wrote this code but it gives different values at times. I don't know why?

#include <iostream>    
using namespace std;

int main()
{
    float f, a, b;
    int x, y, c;

    cout << "enter the float value" << endl; cin >> f;

    x = (int)f;  cout << "before " << x;

    a = b = f;
    c = 1;

    while (b != int(a))
    {
        b = b * 10;
        a = a * 10;
        c = c * 10;
    }

    y = (f * c) - (x * c);
    cout << "after " << y;
}

enter the float value 22.47
before 22 after 46 user@user:~/cpp$ ./a.out enter the float value 2234.127 before 2234 after 126 user@user:~/cpp$ ./a.out enter the float value 22.335 before 22 after 334 user@user:~/cpp$ ./a.out enter the float value 222.88 before 222 after 88

these are a few values i tried.

Ashish Kumar S
  • 117
  • 1
  • 7
  • 1
    What is your desirable output? Input and output example would be good – gameon67 Jan 25 '19 at 05:11
  • What sort of different values? Which times? – Lightness Races in Orbit Jan 25 '19 at 16:13
  • @LightnessRacesinOrbit I do not know how to open a chat; that is why I am asking here: Why do you hate *ternary operator*? Hope you know why I am asking. – JeJo Jan 25 '19 at 16:55
  • @JeJo It's like calling Russia "the big country". Russia's identity is not defined by its size, and there are other big countries. With the added pain & suffering that in the case of "the ternary operator" the brokenness is widespread! ISTR the PHP manual literally calls it that. Ugh! Setting PHP aside, the "real" name for this particular ternary operator (whether or not other operators that take three operands have been provided) would be better; in C++, that's "the conditional operator". #OCD – Lightness Races in Orbit Jan 25 '19 at 16:58
  • @LightnessRacesinOrbit Even though I didn't get your words completly, I agree that in the following (old)code simple had double of the braches than now. So I changed. On the otherhand, I found `?:` more intutive than `typing` long `if-elseif-else` statements. However, using `comma operator` along with `?:` might makes the reader's life harder. – JeJo Jan 25 '19 at 17:08
  • 1
    @JeJo Oh, hey, _using_ the conditional operator is fine. It's calling it "the ternary operator" that I don't like! – Lightness Races in Orbit Jan 25 '19 at 17:18
  • 1
    @LigtehtnessRacesinOrbit i like the way you poured out your feelings for terinary opperator, though i didt understand the maximum part of it – Ashish Kumar S Jan 25 '19 at 18:29
  • what is wrong with `if (x>=0.0){ whole=floor(x); fractional=x-floor(x); } else { whole=ceil(x); fractional=x-ceil(x); }` ? all variables are floats ... if you want n decimal places just do `int(pow(10,n) * fractional)` or even `int(pow(10,n)*x)` – Spektre Jan 26 '19 at 07:56

1 Answers1

0

How to code to separate the whole part and decimal part?

I assume that you want to only represent the floating value into non-decimals part and decimals part. In that case, take the user input as an std::string, and do as follows. I hope the comments will get you through the code:

(See live example)

#include <iostream>
#include <string>
#include <cstddef>  // std::size_t

int main()
{
    std::cout << "enter the float value\n";
    std::string strInput;   std::cin >> strInput;   // take the user input as std::string

    std::size_t pos{ 0 };
    // find the position of charector decimal point('.') using std::string::find
    pos = strInput.find(".", pos);

    // if the decimal point is found in the user input: x = substring starting from 0 to pos of user input.
    const std::string x = pos != std::string::npos ? strInput.substr(0, pos) : strInput;

    // if the decimal point is found in the user input: y = substring starting from pos + 1 to end of user input.
    const std::string y = pos != std::string::npos ? strInput.substr(pos + 1) : std::string{ "" };

    std::cout << "before " << x << " after " << y << '\n';
    return 0;
}

sample input:

enter the float value
0123.04560

Output:

before 0123 after 04560

PS: However, like in the example given above, having zero before the separated decimal parts(both 0123 and 04560) might be unwanted. In that case, use any of the standard functions to convert them back to integers or remove zeros from from the beginning using erase-remove idiom.

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    This is a good approach, and you could use atoi function to get the ineteger values, but i was trying to find a way to do that without making the value as string. – Ashish Kumar S Jan 25 '19 at 17:04
  • @AshishKumarS Hmmm, there we need to explain [the story of floating points](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate). Since `float` is not really reliable, for such cases, you need either [external liberaries](https://en.wikipedia.org/wiki/List_of_C%2B%2B_multiple_precision_arithmetic_libraries) which assure the precision or some other ways(for e.x. above one) to achieve the goal. – JeJo Jan 25 '19 at 17:15
  • I did't understand by what you mean by "float is not really reliable" – Ashish Kumar S Jan 25 '19 at 18:27