# Question about Cash Register change in change?

Page 1 of 1

## 5 Replies - 790 Views - Last Post: 28 January 2013 - 07:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309966&amp;s=4b6ecb5d6b0261a75616b4f67abd8dc7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lilxm00n

Reputation: 0
• Posts: 6
• Joined: 19-January 13

# Question about Cash Register change in change?

Posted 28 January 2013 - 06:55 PM

So, the point of this assignment is to get the change for the customer in the exact quarters, dimes, nickels, and pennies. I have already calculated how to get the total change and everything. I feel like I have done how to get that exact change. I think think the only problem right now is changing the double to int for how many dollars, quarters, dimes, etc. that is needed (because you can't split coins, etc.) I know that static_cast<int> should be used, but I'm not quite sure how to use it. Here's what I have:
```#include <iostream>
using namespace std;

int main()
{
double purchase_amount;
double total_change;
double dollars;
double quarters;
double dimes;
double nickels;
double pennies;

cout << "Enter purchase amount: ";
cin >> purchase_amount;

cout << "Enter amount received: ";

cout << "Total change: " << total_change;

dollars = total_change % 1;
cout << "Dollars: " << endl;
total_change = total_change - dollars * 1;

quarters = total_change % .25;
cout << "Quarters: " << endl;
total_change = total_change - quarters * .25;

dimes = total_change % .10;
cout << "Dimes: " << endl;
total_change = total_change - dimes * .10;

nickels = total_change % .05;
cout << "Nickels: " << endl;
total_change = total_change - nickels * .05;

pennies = total_change % .01;
cout << "Pennies: " << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Question about Cash Register change in change?

### #2 jimblumberg

Reputation: 4647
• Posts: 14,572
• Joined: 25-December 09

## Re: Question about Cash Register change in change?

Posted 28 January 2013 - 06:59 PM

Does this program even compile? You should be getting a few errors. The modulus operator% can't be used on floating point types, it only works with integer types.

Jim

### #3 lilxm00n

Reputation: 0
• Posts: 6
• Joined: 19-January 13

## Re: Question about Cash Register change in change?

Posted 28 January 2013 - 07:06 PM

jimblumberg, on 28 January 2013 - 06:59 PM, said:

Does this program even compile? You should be getting a few errors. The modulus operator% can't be used on floating point types, it only works with integer types.

Jim

that's what the problem is. I know that using % only works with int. That's why I stated that I needed to use static_cast. and that's where the problem lies. I don't know how to implement it.

### #4 CTphpnwb

• D.I.C Lover

Reputation: 3325
• Posts: 11,776
• Joined: 08-August 08

## Re: Question about Cash Register change in change?

Posted 28 January 2013 - 07:14 PM

Why aren't you using int for dollars, quarters, dimes, etc.? Can you have 1.1 quarters?

### #5 Skydiver

• Code herder

Reputation: 4251
• Posts: 13,612
• Joined: 05-May 12

## Re: Question about Cash Register change in change?

Posted 28 January 2013 - 07:15 PM

If you consider that there are 100 pennies in a dollar, you can convert your floating point change amount to an integer value that represents the number of pennies.

### #6 lilxm00n

Reputation: 0
• Posts: 6
• Joined: 19-January 13

## Re: Question about Cash Register change in change?

Posted 28 January 2013 - 07:23 PM

CTphpnwb, on 28 January 2013 - 07:14 PM, said:

Why aren't you using int for dollars, quarters, dimes, etc.? Can you have 1.1 quarters?

oh, thanks for that. I've changed it to int.

Skydiver, on 28 January 2013 - 07:15 PM, said:

If you consider that there are 100 pennies in a dollar, you can convert your floating point change amount to an integer value that represents the number of pennies.

our professor said we should be using static_cast to change the total change, or whatever into an int.