Money Breakage

Alrite, how do break money down using c++
eg:the user enters a value, and then the program breaks it up
for this example $20.20 to be broken down to how many, 1 dollars, .50 cents, 20 cents, .10 cents and .05 cents...

#include <iostream>
#include <math.h>


using namespace std;

int main()
{
int Mod, Modu, value, deposit, change;

cout<<"Enter deposit:";
cin>>deposit;
cout<<"Enter value:";
cin>>value;



Mod = change / 100;
Modu = change % 100;
//cout<<Mod<<" and "<<Modu<<endl;

Mod = Mod / 1;
cout<<Mod/1<<" 1 dollar"<<endl;




Modu = Modu / 50;
cout<<Modu/50<<" 50c coins"<<endl;


Modu = Modu / 20;
cout<<(Modu/20)<<" 20c coins"<<endl;






system("pause");
return 0;
}

I haven't finished it because I was having trouble at 20 cents, what am I doing wrong......thanks
Your problem is that Modu/50 gets evaluated as 0. The safe way of doing it is to not use modulo. Then the algorithm becomes something like:

1
2
3
4
5
6
7
8
9
10
11
12
int deposit=2020 // as per your example
int coins=deposit/100 // evaluating to 20
cout << coins << " 1 dollar" << endl;
change=deposit-(100*coins); // this is now 20.

int coins=change/50 // evaluating to 0
cout << coins << " 50c coins" << endl;
change=deposit-(50*coins); // this is still 20.

int coins=deposit/20 // evaluating to 1
cout << coins << " 20c coins" << endl;
change=deposit-(20*coins); // this is now 0. 


If you want to use modulo, then change "Modu = Modu / 50;" to "change= Modu/50; Modu = Modu%50" and repeat for the rest

edit: wow, grammar fail lol
Last edited on
Its called typing in short hand......duhhh
Thanks, may your life be blessed abundantly....
Topic archived. No new replies allowed.