help with modulus

okay so i understand modulus
i understand that you can only use int when working with modulus
i understand that it calculates the remainder

the program im writing prompts the user for purchase and payment amount, then calculates the amount of dollars, half-dollars, quarters, nickles, dimes in change.

im completely stuck on the modulus part.
i can't figure out what to declare as a int or a double.
i know i can't have the variables listed under as const doubles as int, since that would make any sense.
i know change has to be in double,
but don't i have to use change in the mod calculations?
maybe the answers obvious, but i'm not getting it.

a little help???

here is a better description of what im trying to accomplish

1. Prompt the user for the amount of the purchase (in cents.)
Read user input.
2. Prompt the user for the amount received in payment (in cents.)
Read user input.
3. Calculate the change. (in cents)
4. Calculate the number of dollar bills to be given in change. (Use the
division operator.)
*Calculate the remaining change (Use the mod operator.)
5. Calculate the number of half-dollars to be given in change. (Use the
division operator.)
*Calculate the remaining change (Use the mod operator.)
6. Repeat the process for the remaining coins/change.
7. Display the author’s identifying information to the output file.
8. Display the results to the output file.
9. Display the author’s identifying information on the screen.
10. Display an appropriate message on the screen indicating to the user the
name of the output file to which the results have been written.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ( )
{
	const double DOLLARS = 1.0,
	             HALF_DOLLARS = 0.5,
	             QUARTERS = 0.25,
	             DIMES = 0.1,
	             NICKELS = 0.05,
	             PENNIES = 0.01;

	double change,
	       payment_amount,
	       purchase_amount;

	int amount_dollars,
	    amount_half_dollars,
	    amount_quarters,
	    amount_dimes,
	    amount_nickels,
	    amount_pennies;

	cout << "Enter amount of the purchase (in cents): ";
	cin >> purchase_amount;

	cout << "Enter amount received in payment (in cents): ";
	cin >> payment_amount;

	change = payment_amount - purchase_amount;

	amount_dollars = change / DOLLARS;
	// modulus calc here for remainder

	amount_half_dollars = change / HALF_DOLLARS;
        // modulus calc here for remainder

	amount_quarters = change / QUARTERS;
        // modulus calc here for remainder

	amount_dimes = change / DIMES;
        // modulus calc here for remainder

	amount_nickels = change / NICKELS;
        // modulus calc here for remainder

	amount_pennies = change / PENNIES;
	// modulus calc here for remainder

	ofstream outfile;

	outfile.open ("file_name.txt");

	outfile << fixed << setprecision(2)
                << endl << endl << "Received " << payment_amount << 
                << " for a purchase of " <<
                << purchase_amount << endl
                << endl << endl << "Change in coins:" << endl
                << "Coin           " << setw(6) << "Number" << endl
                << "_____________________" << endl
                << "Change         " << setw(6) << change << endl
                << "Dollars        " << setw(6) << amount_dollars << endl
                << "Half-Dollars   " << setw(6) << amount_half_dollars << endl
                << "Quarters       " << setw(6) << amount_quarters << endl
                << "Dimes          " << setw(6) << amount_dimes << endl
                << "Nickels        " << setw(6) << amount_nickels << endl
                << "Pennies        " << setw(6) << amount_pennies << endl <<
                << endl;

	cout << "Program results will be written to file_name.txt.";

	outfile.close();

    return 0;
}


rough example of output file results:
(ignore any setw errors or w/e i totally just wrote this out myself)
Recieved 40.00 for a purchase of 34.01
   
   Change in coins:
Coins           Number
______________________
Dollars             5
Half Dollars        1
Quarters            1
Dimes               1
Nickels             0
Pennies             4
closed account (o3hC5Di1)
Hi there,

Your way is quite good actually, I'm not sure why your teacher requires you to use the modulus operator. Often they have a solution in their head and they don't consider any other.

You could do the following:

1
2
3
4
5
6
const int DOLLARS = 100,
HALF_DOLLARS = 50,
QUARTERS = 25,
DIMES = 10,
NICKELS = 5,
PENNIES = 1;


Then you could do:

1
2
3
4
5
6
7
change = payment_amount - purchase_amount;

	amount_dollars = (change*DOLLARS) / DOLLARS;
	change = (amount_dollars*DOLLARS) % DOLLARS;

	amount_half_dollars = (change*HALF_DOLLARS) / HALF_DOLLARS;
        change = (amount_dollars*HALF_DOLLARS) % HALF_DOLLARS;



Another way would be to do (using your double constants) (note that this doesn't use the modulus operator and may be inaccurate because integer division can round up or down as the machine likes):

1
2
amount_dollars = change / DOLLARS;
        change = change - amount_dollars;



Yet another way would be to encapsulate a money amount into a struct - but I'm guessing you haven't learned about those yet.

Hope that helps.

All the best,
NwN
I tried the method above, but it gave me a line breakpoint and line 35, and the output file as:
Received 40.00 for a purchase of 34.01


Change in coins:
Coin           Number
---------------------
Change           0.00
Dollars             5
Half-Dollars        0
Quarters            0
Dimes               0
Nickels             0
Pennies             0


changing the const doubles to int like that makes sense to me.
i've never had a line breakpoint before, so i'm not quite sure what that means...
i'll keep trying to figure it out.
Could you do something like this?

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
#include <iostream>

int main() {
	const int DOLLAR = 100 , HALF_DOLLAR = 50 ,
			  QUARTER = 25 , DIME = 10 , NICKEL = 5 ,
			  PENNY = 1;
	double price = 0.0 , paid = 0.0;
	int number_dollar = 0 , number_half = 0 ,
	    number_quarter = 0 , number_dime = 0 ,
		number_nickel = 0 , number_penny = 0 ,
		change = 0;
	
	std::cout << "Please enter the price($): ";
	std::cin >> price;
	std::cout << "Please enter what you paid($): ";
	std::cin >> paid;
	
	change = paid * DOLLAR - price * DOLLAR;
	
	number_dollar = change / DOLLAR;
	change %= DOLLAR;
	
	number_half = change / HALF_DOLLAR;
	change %= HALF_DOLLAR;
	
	number_quarter = change / QUARTER;
	change %= QUARTER;
	
	number_dime = change / DIME;
	change %= DIME;
	
	number_nickel = change / NICKEL;
	change %= NICKEL;
	
	number_penny = change / PENNY;
	change %= PENNY;
	
	std::cout << "Dollars - " << number_dollar << endl
			  << "Half    - " << number_half   << endl
			  << "Quarter - " << number_quarter<< endl
			  << "Dime    - " << number_dime   << endl
			  << "Nickel  - " << number_nickel << endl
			  << "Penny   - " << number_penny  << endl;
}


*added ideone line because it looks funny after I copy/pasted


http://ideone.com/L8pavu

*edit after reading your assignment it wants you to input as cents and not dollars so it should look like this

I fixed the code on ideone so it now looks like this

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
#include <iostream>

int main() {
	const int DOLLAR = 100 , HALF_DOLLAR = 50 ,
			  QUARTER = 25 , DIME = 10 , NICKEL = 5 ,
			  PENNY = 1;
	int price = 0 , paid = 0; //int not double
	int number_dollar = 0 , number_half = 0 ,
	    number_quarter = 0 , number_dime = 0 ,
		number_nickel = 0 , number_penny = 0 ,
		change = 0;
	
	std::cout << "Please enter the price(cents): ";
	std::cin >> price;
	std::cout << "Please enter what you paid(cents): ";
	std::cin >> paid;
	
	change = paid - price; //dont need to convert to dollars anymore
	
	number_dollar = change / DOLLAR;
	change %= DOLLAR;
	
	number_half = change / HALF_DOLLAR;
	change %= HALF_DOLLAR;
	
	number_quarter = change / QUARTER;
	change %= QUARTER;
	
	number_dime = change / DIME;
	change %= DIME;
	
	number_nickel = change / NICKEL;
	change %= NICKEL;
	
	number_penny = change / PENNY;
	change %= PENNY;
	
	std::cout << "Dollars - " << number_dollar << std::endl
			  << "Half    - " << number_half   << std::endl
			  << "Quarter - " << number_quarter<< std::endl
			  << "Dime    - " << number_dime   << std::endl
			  << "Nickel  - " << number_nickel << std::endl
			  << "Penny   - " << number_penny  << std::endl;
}


so 40 dollars in cents would be 4000 kind of odd way to input but thats what it seems to be asking.
Last edited on
giblit, you got it.
thanks so much!!
yeah i was about to say "i need to state the change", but i looked at the assignment and it doesn't require me to do that in the output.
thanks again lol.
Topic archived. No new replies allowed.