modulo operator problems

Oct 20, 2009 at 4:06am
My assignment was to create a kind of check cashing program, that when the check amount is entered, the correct denominations of bills and coins to be returned are shown. I have the bills returning fine, but I need to pull the value to the right of the decimal (cents) to return the correct coins.

The bills and coins are to be done in two separate functions using overloaded methods and the modulo operator. I think thats it. Here is the code I have so far.

NOTE: My output is not refined. It is currently read.
" hundreds fifties twenties tens fives ones
quarters dimes nickles pennies "


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void change(double, int&, int&, int&, int&, int&, int&);
void change(int, int&, int&, int&, int&);
int main()															//main
{
	int hund=0, fifty=0, twenty=0, ten=0, five=0, one=0, qtr=0, dm=0, nk=0, pn=0;
	double amt;		//change<=$1000
	
	cout<<"Enter check amount: ";
	cin>>amt;
	
	change(amt, hund, fifty, twenty, ten, five, one);
	change(amt, qtr, dm, nk, pn);
	
	cout<<hund<<" "<<fifty<<" "<<twenty<<" "<<ten<<" "<<five<<" "<<one<<" "<<endl;
	cout<<qtr<<" "<<dm<<" "<<nk<<" "<<pn<<" "<<endl;

}//end main


void change(double a, int&h, int&f, int&w, int&t, int&v, int&o)		//bills
{
	double chg = a;
	
	int c = static_cast<int> (chg*1);
	
	h=c/100;
	c%=100;
	f=c/50;
	c%=50;
	w=c/20;
	c%=20;
	t=c/10;
	c%=10;
	v=c/5;
	c%=5;
	o=c;
	

}

void change(int a, int&q, int&d, int&n, int&p)					//coins
{
	int chg = a%1;
	
	int c = static_cast<int> (chg*100);
	
	q=c/25;
	c%=25;
	d=c/10;
	c%=10;
	n=c/5;
	c%=5;
	p=c;

	
}
Last edited on Oct 20, 2009 at 12:58pm
Oct 20, 2009 at 6:16am
Theoritically you shouldnt have anything to the right of the decimal aka change. This is because it is an integer. An integer has no numbers to the right of the decimal. You see what im saying?? So im thinking you might need to change it to floats instead of integers
Oct 20, 2009 at 12:46pm
the assignment has to be done using integer math
Oct 20, 2009 at 12:49pm
Now, the bills return correct, but the coins return the bill+coins in coin denominations. I need a way to pull the decimal from the amount, but have no clue how and I've been google'ing for hours and hours.

UPDATE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void change(double, int&, int&, int&, int&, int&, int&);
void change(double, int&, int&, int&, int&);

int main()															//main
{
	int hund=0, fifty=0, twenty=0, ten=0, five=0, one=0, qtr=0, dm=0, nk=0, pn=0;
	double amt;		//change<=$1000
	
	cout<<"Enter check amount: ";
	cin>>amt;
	
	change(amt, hund, fifty, twenty, ten, five, one);
	change(amt, qtr, dm, nk, pn);
	
	cout<<hund<<" "<<fifty<<" "<<twenty<<" "<<ten<<" "<<five<<" "<<one<<" "<<endl;
	cout<<qtr<<" "<<dm<<" "<<nk<<" "<<pn<<" "<<endl;

}//end main

void change(double a, int&h, int&f, int&w, int&t, int&v, int&o)		//bills
{
	double chg = a;
	int c = static_cast<int> (chg*1);
	
	h=c/100;
	c%=100;
	f=c/50;
	c%=50;
	w=c/20;
	c%=20;
	t=c/10;
	c%=10;
	v=c/5;
	c%=5;
	o=c;
	
}

void change(double a, int&q, int&d, int&n, int&p)					//coins
{
	double chg = a;
	int c = static_cast<int> (chg*100);
	
	q=c/25;
	c%=25;
	d=c/10;
	c%=10;
	n=c/5;
	c%=5;
	p=c;
	
}
Oct 20, 2009 at 5:10pm
http://cplusplus.com/forum/general/15148/

See that thread for an integer method of determining what change to give.
Oct 21, 2009 at 2:57am
thanks everyone for all of their input.
Topic archived. No new replies allowed.