Hello,
I am writing the code for a cash register, and I have searched the site and nothing comes back that helps me, it basically has to take the change and break it up to the fewest 100's, 50's, 20's, 10's, 5's, and 1's. I have already written the cout and cin for the messages and input prompts. I need help with using the modulus to get the correct number of bills for any change combo for example change of 999.00 would need to output 9 hundreds, 1 fifty, 2 twenties, 0 tens, 1 five, and 4 ones. Needs to also work with small change for example 14 or 18 dollars.


thanks

Oh one tip I was given was I should multiply the change amount by 100 to get it in pennies and then divide by 10000 to get denomination in 100's and use modulus function to get rest of bills whether there are any or not.

The mod division is the key to this whole thing, it sounds like you've pretty much got the answer.

What do you mean you need help using the modulus?


I did this program a few weeks ago, I think I multiplied by 100 and then used a typecast to change my doubles to int. Not sure if that's necessarily constituted "good" programming, but int division made the whole deal VERY simple.


EDIT: Nevermind, I used floor rounding and subtraction. Don't think I knew about typecasting when I wrote this program. Either way should work though.

I am very new to programming and don't understand what I need to do to implement the modulus. I don't see how it is going to get me the correct number of bills. What is floor rounding and subtraction?
thanks

I am very new to programming and don't understand what I need to do to implement the modulus. I don't see how it is going to get me the correct number of bills. What is floor rounding and subtraction?
thanks

110 dollars...

110/5 (int division) = 5 * 20, or 5 20's.
110%20 means 5 remainder 10, (10 dollars left to split up) basically this is the remaining amount of cash you have to divide into other forms of cash.

Hope this helps.

Edit: also if the remaining amount divided by a cash-type is zero, don't bother using modulus for that particular number.

I am very new to programming and don't understand what I need to do to implement the modulus. I don't see how it is going to get me the correct number of bills. What is floor rounding and subtraction?
thanks

sorta like this...

int x = 1234.56, thousands = 0;
 double remainder;

 remainder = x % 1000; // remainder is 234.56
 thousands = x / 1000; // store how many thousands you have

//... you get the picture?

subtraction is like reverse addition... here is a simple function to subtract B from A

double subtraction(double A, double B){
double negB;
negB = B * -1;

return A + negB;

}
Comments
Nice examples

Bored....i made this for you...

Just enter what amount you want sorting... simple

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 int n100 = 0;
 int n50 = 0;
 int n20 = 0;
 int n10 = 0;
 int n5 = 0;
 int c2 = 0;
 int c1 = 0;

 int input;
 cout << "Pounds: ";
 cin >> input;

 while(input >= 100){ input=input-100; n100++;}
 while(input >= 50){  input=input-50;  n50++;}
 while(input >= 20){  input=input-20;  n20++;}
 while(input >= 10){  input=input-10;  n10++;}
 while(input >= 5){   input=input-5;   n5++;}
 while(input >= 2){   input=input-2;   c2++;}
 while(input >= 1){   input=input-1;   c1++;}

 
 if(n100>0){cout << "Hundred pound notes: " << n100 << "\n";}
 if(n50>0){cout << "Fiftey pound notes  : " << n50 << "\n";}
 if(n20>0){cout << "Twenty pound notes : " << n20 << "\n";}
 if(n10>0){cout << "Ten pound notes  : " << n10 << "\n";}
 if(n5>0){cout << "Five pound notes  : " << n5 << "\n";}
 if(c2>0){cout << "Two pound coins : " << c2 << "\n";}
 if(c1>0){cout << "Poind coins : " << c1 << "\n";}

 system("pause");
}

This code was brought to you courtesy of James :icon_cheesygrin:

This article has been dead for over six months. Start a new discussion instead.