Getting Significant Figures During Division

Apr 14, 2020 at 7:10pm
I'm trying to learn how to code and I decided to try and make a calculator. I've got everything working but I can't get decimals from division. I've tried double and float and some other suggestions I've found but implementing one of them results in all division becoming 0.
Here is a snipet of the code:
1
2
     else {if (operation.compare("D") == 0) {
        result = (first / second); }

Link to full code: http://cpp.sh/4z5mw
I know that there are probably better ways to do most of this but I wanted to try to figure it out on my own.
Last edited on Apr 14, 2020 at 7:26pm
Apr 14, 2020 at 8:08pm
I didn't bother to look at your "full code".
Presumably first and second are ints.
Just making result a double won't work.
Instead, you should make first and second doubles, which they obviously should be for a calculator anyway.

If you really needed a floating point result from an integer division, then you need to cast one of them to double:

 
    result = double(first) / second;


BTW, your brace placement is retarded. :)
Topic archived. No new replies allowed.