How to make a console ignore something

Ok so i need a way to make it so if a user enters 6.71 the program will ignore the .
Are you saying you want the decimal ignored during user input or after user input when you are using the value?
after user input wheni am using the value

This is what i need help with it works fine with Loonies,quarters,dimes,nickels but if i enter say 6.01 it tells me i need pennies but as you can see the .01 means theres 1 penny needed. And if i enter say 6.72 is says i need 1 penny instead of 2. how do i fix this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{

    cout << "$$$$$$$$$$$$$$$$" << endl;
    cout << "$ Assignment 1 $" << endl;
    cout << "$$$$$$$$$$$$$$$$" << endl;

    double change;
    int L;
    int Q;
    int D;
    int N;
    int P;


        cout<<"Change Due? ";
        cin>>change;
        L =change / 1;
        Q =(change - L) / .25;
        D =((change - L) - (Q * .25)) / .10;
        N =(((change - L) - (Q * .25) - (D * .10))) / .05;
        P =(((change - L) - (Q * .25) - (D * .10) - (N * .05))) / .01;

                cout<<"You Need\n\n";
        if (L == 1)

            {
                cout<< L << " Loonie" <<"\n";
            }
            else

                {
                    cout<< L << " Loonies" <<"\n";

                                                        }
        if (Q == 1)

            {
                cout<< Q << " Quarter" <<"\n";
            }
            else

                {
                    cout<< Q << " Quarters" <<"\n";

                                                        }

        if (D == 1)

            {
                cout<< D << " Dime" <<"\n";
            }
            else

                {
                    cout<< D << " Dimes" <<"\n";

                }
        if (N == 1)

            {
                cout<< N << " Nickel" <<"\n";
            }
            else

                {
                    cout<< N << " Nickels" <<"\n";

                }
        if (P == 1)

            {
                cout<< P << " Penny" <<"\n";
            }
            else

                {
                    cout<< P << " Pennies" <<"\n";

                }
                   _getch();
                                        return 0;
                }
Last edited on
It is a problem with the way computers store floating point numbers. 6.72 is not stored exactly, so when you convert it truncate it to an integer you loose information that you need.

I recommend that instead of doing everything with floating-point arithmetic, you first turn the number the user entered into an integer. Round properly in order to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    double dchange;
    int    change;
    int    loonies;
    int    quarters;
    int    dimes;
    int    nickles;
    int    pennies;

    cout << "Please enter the amount of change due: ";
    cin >> dchange;

    // Convert to integer value, fixing for roundoff errors
    change = (dchange * 100) + 0.5;

    // Calculate the minimum number of loonies and coins
    loonies  =  change / 100;
    quarters = (change - (loonies * 100)) / 25;
    ...


BTW, your indentation needs help. You need to be very consistent about how you do it.
Also, you can use the ?: operator to avoid all those if statements:
1
2
3
    cout << loonies  << ((loonies  == 1) ? " Loonie\n"  : " Loonies\n");
    cout << quarters << ((quarters == 1) ? " Quarter\n" : " Quarters\n");
    ...


Hope this helps.
Last edited on
NVM thank you very much
Last edited on
Perhaps, it would be better to read it in as a string. Then you can easily separate out the dollars and cents part separately.
Topic archived. No new replies allowed.