-6

The sample output for member which is, works fine:

Please input a one if you are a member of the dental plan
Input any other number if you are not
1
Please input the service charge
7.89
Please input the test charges
89.56
The total bill is $97.45. 

Whereas the sample output for non-member, gives me an output of 1.79214e-307 instead of 165.84

Sample output for non-member:
Please input a one if you are a member of the dental plan
Input any other number if you are not
2
Please input the service charge
75.84
Please input the test charges
49.78
Please input the medicine charges
40.22
The total bill is $165.84

Here's the code

#include <iostream>
using namespace std;

double totalmember( double serv, double test)
{
    double total;
    total = serv + test;
    return total;
}

double totalnot( double serv, double test, double med)
{
    double total;
    total = serv + test + med;
    return total;
}

int main()
{
    int memberstat;
    double servcharge, testcharge, medcharge , membertot, notmemtot;
    cout << "Please input a one if you are a member of the dental plan" << endl;
    cout << "Input any other number if you are not" << endl;
    cin >> memberstat;
    cout << endl;

    cout << "Please input the service charge" << endl;
    cin >> servcharge;

    cout << "Please input the test charges" << endl;
    cin >> testcharge;

    if (memberstat != 1)
    {
        cout << "Please input the medicine charges" << endl;
        cin >> medcharge;
    }

    if (memberstat == 1)
    {
        membertot = totalmember(servcharge, testcharge);
        cout << "The total bill is $" << membertot;
    }

    else {
        notmemtot = totalnot(servcharge, testcharge, medcharge);
        cout << "The total bill is $" << membertot;
    }


    return 0;
}

How do I fix this?

phuclv
  • 37,963
  • 15
  • 156
  • 475
nana
  • 25
  • 7
  • 1.79214e-307 **is** a normal floating point number – phuclv Sep 20 '20 at 14:29
  • 4
    First thought - never store money or time as a floating point number. Always use a sufficiently large integer and a base unit. For money, you'd use an integer number of cents, and then only convert that to/from dollars and cents when taking input or presenting output. – JohnFilleau Sep 20 '20 at 14:30
  • How is "1.79214e-307" not a "normal" number? It's just scientific notation. Why does notation matter to you? It's still the same number, regardless of how you write it. – Jesper Juhl Sep 20 '20 at 14:31
  • When you should output the `nonmembertot` you're actually outputting `membertot`, which is uninitialized, so it can take any value. – JohnFilleau Sep 20 '20 at 14:32
  • It's a number that is very, very close to 0. You need to be aware of the limitations of [floating point](https://stackoverflow.com/a/21895757/4641116). Those limitations are why floating point numbers are usually avoided for monetary or time values. – Eljay Sep 20 '20 at 14:32
  • Superman 2 should be mandatory study material when students are introduced to floating point numbers. – JohnFilleau Sep 20 '20 at 14:32
  • @phuclv I'm sorry, my bad. I don't want my output in scientific notation, how do I change it to 165.84 instead of 1.79214e-307? Do i have to use the iomanip library in any way? – nana Sep 20 '20 at 14:35
  • @nana The iomanip header is for changing the display format, it doesn't change the value of the numbers you display. `1.79214e-307` is simply the wrong number, no format changes are going to fix that. – john Sep 20 '20 at 14:37
  • [Warnings are your friends](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – n. m. could be an AI Sep 20 '20 at 14:38
  • @JohnFilleau you were right! I didn't notice that, It's not output-ing in scientific notation anymore. Thank you so much. – nana Sep 20 '20 at 14:39
  • 2
    @nana for future reference, in scientific notation, `XeY` means `X times 10^Y`. You can see how anything `e-307` is an extremely tiny number. There are only somewhere around `10^80` atoms in the entire universe. If the ENTIRE UNIVERSE was only worth one cent, then you couldn't even use a single atom to represent the value your program was outputting. – JohnFilleau Sep 20 '20 at 14:46

1 Answers1

2

You are printing the wrong variable.

    notmemtot = totalnot(servcharge, testcharge, medcharge);
    cout << "The total bill is $" << membertot;

should be

    notmemtot = totalnot(servcharge, testcharge, medcharge);
    cout << "The total bill is $" << notmemtot;

But even easier (so you can't make the mistake you made) would be not to use a variable in the first place, just write it like this

    cout << "The total bill is $" << totalnot(servcharge, testcharge, medcharge);

You don't have to use variables when you use cout, you can just use the function result directly.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you so much, I didn't notice that. I also had no idea i could cout the function result directly, I'll be more cautious next time. – nana Sep 20 '20 at 14:41