Have a assignment to hand in and could use some feedback

Hello, i'm new here but have heard great things about this community. So I figured I would sign up and check it out.

Now I don't nessicarily have a problem with my assignment I was just looking for some feedback or tips on anything that I should change or redo. I just figured having more then my eyes look over it might help me get a better grade :).

What the program is suppose to do: This program is suppose to act like a cash register. Where the user enters the amount a item costs and also the cash the customer has gave them. It then tells the user how much change is due along with how many dollars, quarters, dimes, nickels, and pennies to give the customer.

Here is my code it compiles and runs good as far as I can tell. I haven't put the finishing touches on the text formatting yet though.

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

double CalculateMoneyBack(double cost, double money);
void PrintChange(const double cost, const double money, double change);
int CalcQuarters(double &ChangeRemaining);
int CalcDimes(double &ChangeRemaining);
int CalcNickels(double &ChangeRemaining);
int CalcPennies(double &ChangeRemaining);

int main()
{
    double moneyGiven;
    double itemCost;

    cout << "Please enter the cost of the item: ";
    cin >> itemCost;

    cout << "Please enter the amount of money you were given: ";
    cin >> moneyGiven;

    PrintChange(itemCost, moneyGiven, CalculateMoneyBack(itemCost, moneyGiven));
}


double CalculateMoneyBack(double cost, double money)
{
    return money - cost;
}

void PrintChange(const double cost, const double money, double change)
{
    int dollarsRemaining = change;
    double changeRemaining = change - dollarsRemaining;

    if (money == cost)
    {
        cout << "The user has paid the full amount. You don't need to give them change." << endl;
    }
    else if (money > cost)
    {
        cout << "The remaining change is: $" << setprecision(3) << change << endl;
        cout << endl;

        cout << "Dollars: " << dollarsRemaining << endl;
        cout << "Quarters: " << CalcQuarters(changeRemaining) << endl;
        cout << "Dimes: " << CalcDimes(changeRemaining) << endl;
        cout << "Nickels: " << CalcNickels(changeRemaining) << endl;
        cout << "Pennies: " << CalcPennies(changeRemaining) << endl;
    }

}

int CalcQuarters(double &ChangeRemaining)
{
    int quarters = 0;
    while (ChangeRemaining >= 0.25)
    {
        quarters++;
        ChangeRemaining -= 0.25;
    }
    return quarters;
}

int CalcDimes(double &ChangeRemaining)
{
    int dimes = 0;
    while (ChangeRemaining >= 0.10)
    {
        dimes++;
        ChangeRemaining -= 0.10;
    }
    return dimes;
}

int CalcNickels(double &ChangeRemaining)
{
    int nickels = 0;

    // Having trouble here not sure why I have to have the if statement here
    // But if I don't it screws up the program sometimes.
    if (ChangeRemaining >= 0.05f)
    {
        while (ChangeRemaining >= 0.05);
        {
            nickels++;
            ChangeRemaining -= 0.05;
        }
    }
    return nickels;
}

int CalcPennies(double &ChangeRemaining)
{
    int pennies = 0;
    while (ChangeRemaining >= 0.01)
    {
        pennies++;
        ChangeRemaining -= 0.01;
    }
    return pennies;
}


Also one thing that has been giving me trouble is getting the remaining change to output as something like $6.50 instead of $6.5. I have tried setprecision(), but I don't think that will work because there is no extra 0 after the .5 as far as I can tell. Anyways any feedback would be great and try not to be to harsh I have only been programming for a month or so.
Last edited on
Use fixed (http://cplusplus.com/reference/ios/fixed/ ) with setprecision as shown in example.
Last edited on
write it using fixed before the output. So you would write it this way:

1
2
cout.setprecision(3);
cout << "The remaining change is: $" << fixed << change << endl;


fixed notation specifies how many digits you want to represent, regardless of if there is trailing zeroes or not. Hope that helps.

note: I set setprecision to 3 because that was the format in the original code. But yes, you would change it to 2 if you wanted to properly display change (as in 2 places after the decimal).
Last edited on
fixing error in randisking code:
cout << "The remaining change is: $" << fixed << setprecision(2)<< change << endl;
or
1
2
cout.precision(2);
cout << "The remaining change is: $" << fixed << change << endl;
Thank you so much guys that fixed it up.

Also any idea's on what to do about the if statement in the CalcNickels() function? When I enter 13.50 for the price and $20 it tells me that there is $6.50 change to be given which is right. But it then tells me I need to give

6 dollars
2 quarters
1 nickel

I can't figure out what it does that.
Please enter the cost of the item: 13.50
Please enter the amount of money you were given: 20
The remaining change is: $6.5

Dollars: 6
Quarters: 2
Dimes: 0
Nickels: 0
Pennies: 0

Your program output.
I will look into it now though.
That is with the if statement in there though or did you delete it? Sorry Relize I didn't explain that I was wondering why I had to have the if statement on in there and not on the others. Nickels was the only one that messed up the count on without a if statement out of all the tests I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int CalcNickels(double &ChangeRemaining)
{
    int nickels = 0;

    // Having trouble here not sure why I have to have the if statement here
    // But if I don't it screws up the program sometimes.
    if (ChangeRemaining >= 0.05f)
    {
        while (ChangeRemaining >= 0.05);
        {
            nickels++;
            ChangeRemaining -= 0.05;
        }
    }
    return nickels;
}
Last edited on
Found it.
Delete if statement.
Look at the line 9.
You put semicolon after while so next block isn't related to while! It will execute once and only once everytime.
That's why I prefer K&R brace style over Allman.
Ohhh thank you knew it would probably be something quite stupid lol

Perfect thanks again guys for the help fixing them things. Now to add some formatting to the output, and maybe add a extra feature and hope to get extra credit :).

If anyone else notices any bad practices I used or anything else let me know I love the criticism.
Last edited on
Line 39: comparsion floating point variables using == isn't safe.
You can use std::islessgreater(double, double) function from <cmath> instead of !=. Or use it with NOT operator (!) to replace ==
or if you are not using C++11 you can use something like:
1
2
3
4
bool is_equal(double x, double y, double epsilon = 0.00001)
{
    return (std::fabs(x - y) < epsilon);
}

Note: requires <cmath>!

EDIT: fixed error
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Some info on floating point problems.

sin(1.0) is equal sin(1.0)? Do you really think so?
http://ideone.com/MY6EYc
Last edited on
Topic archived. No new replies allowed.