5 Replies - 303 Views - Last Post: 28 January 2013 - 07:23 PM Rate Topic: -----

#1 lilxm00n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 receive_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: ";
    cin >> receive_amount;
    
    total_change = receive_amount - purchase_amount;
    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  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 lilxm00n  Icon User is offline

  • New D.I.C Head

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

Re: Question about Cash Register change in change?

Posted 28 January 2013 - 07:06 PM

View Postjimblumberg, 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.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 lilxm00n  Icon User is offline

  • New D.I.C Head

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

Re: Question about Cash Register change in change?

Posted 28 January 2013 - 07:23 PM

View PostCTphpnwb, 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.

View PostSkydiver, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1