user3400146
user3400146

Reputation: 1

C++ cashier code

The question:

Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Display the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return.

What i have so far:

#include <iostream>
using namespace std;


int main()
{
double amount_due;
double amount_recieved;

cout << "Enter amount due: " << endl;
cin >> amount_due;
cout << "Enter amount received: ";
cin >> amount_recieved;

int change = amount_recieved - amount_due;
int dollar = 100;
int quarters = 25;
int dimes = 10;
int nickels = 5;
int pennies = 1;



//Return change in full dollars

cout << "dollars: " << change % 100  << endl;


//Return change in quarters

cout << "quarters:  " << (change % 100) % 25 << endl;

//Return change in dimes

cout << "dimes: " << ((change % 100) % 25) % 10 << endl;

// Return change in nickels

cout << "nickels: " << (((change % 100) % 25) % 10) % 5 << endl;

//Return change in pennies

cout << "pennies:  " << ((((change % 100) % 25) % 10) % 5) % 1 << endl;



system("pause");
return 0;

}

I realize there are some other one of these answered but they may be to advanced to use in my code, what am i doing wrong?

Upvotes: 0

Views: 12565

Answers (4)

garakchy
garakchy

Reputation: 584

#include <iostream>
using namespace std;

int main() {
double amount_due;
double amount_recieved;

cout << "Enter amount due: " << endl;
cin >> amount_due;
cout << "Enter amount received: ";
cin >> amount_recieved;

int change = amount_recieved - amount_due;
int dollar = 100;
int quarters = 25;
int dimes = 10;
int nickels = 5;
int pennies = 1;

//Return change in full dollars

cout << "dollars: " << change / 100  << endl;    

//Return change in quarters

cout << "quarters:  " << (change % 100) / 25 << endl;

//Return change in dimes

cout << "dimes: " << ((change % 100) % 25) / 10 << endl;

// Return change in nickels

cout << "nickels: " << (((change % 100) % 25) % 10) / 5 << endl;

//Return change in pennies

cout << "pennies:  " << ((((change % 100) % 25) % 10) % 5) / 1 << endl;

return 0;
}

Upvotes: 0

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

As others have mentioned, you are not doing the step required to get this working, and that is repeated subtraction. Yes, you subtracted the price from the amount given, and determined the dollars to give, but you failed to subtract out those dollars, giving you a new total to determine how many quarters.

Something like this (assuming you're working in pennies):

    change_left = customer_payment - original_cost;

    //...
    number of dollars = change_left  / 100;
    change_left = change_left - (100 * number of dollars);  <-- where is this?

Now how do you determine the number of quarters? You have a "running total" called change_left that is being reduced by the change you currently have given the customer. You repeat similar steps to get the number of quarters, then number of dimes, nickels, etc., i.e. divide by 25, then subtract out the number of quarters giving a new "change_left". Then repeat for 10 to get the dimes, 5 to get the number of nickels, etc.

So again, the problem isn't C++ -- the issue is that you are not thinking this out as a discrete series of steps that lead to a final goal.

Upvotes: 0

aruisdante
aruisdante

Reputation: 9075

There are several problems. The first is that you ask the user to input values, but don't specify what they are. If the user enters a number that's not in pennies, you're not going to get the value you expect. I expect this input should be in dollars. So, first, change change into:

change =  int((amount_recieved - amount_due) * 100.0)

Next:

 cout << "dollars: " << change % 100 << end;

Will return the remainder of dividing change by 100, which is not what you want. You simply want to divide dollars by 100. You also want to likely modify change to stop you having to repeat this math.

 dollars = change / 100;
 cout << "dollars: " << dollars << endl;
 change -= dollars*100;

From there, the rest of the code should work as expected minus the % 100 parts. As others have mentioned in the comments to the question, your problem is arising from not thinking the math through first, not from doing anything inherently wrong with C++. This would have produced the wrong result in any language, including writing it down as math on a blackboard.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881323

What you want to do is the same as a cashier would do.

First ensure the change is represented as whole pennies.

Then provide enough dollars until the change remaining is less than a dollar. Then move on to quarters, then dimes, nickels and pennies.

So for the dollar case, pseudo-code would be:

dollars = change / 100        # get integral number of dollars
change = change % 100         # and reduce change-left-to-go accordingly
print dollars, " dollars"

It should then be a simple matter to apply that logic to the other coin types in order of reducing value.

Upvotes: 1

Related Questions