### Simple question regarding code and modulus.

I'm currently pretty new to programming, and I'm needing some assistance with a specific problem.

I'm supposed to make a program that takes an amount paid, and an amount owed, then yields the change in the most efficient use of dollars, quarters, dimes, nickels, and pennies. fmod would fit this perfectly, but my issue is the professor doesn't want us using anything that hasn't been discussed in class yet.

I'm positive modulus would have to play a role in this, but given that modulus can only work with integers, how could I go about making this work?

I'll exclude the code, simply due to the fact that I know why it isn't working and I understand my calculations are going to have to be rewritten.

The exact instructions are as follows:
"A third-grade teacher at Plano Elementary School wants a program that allows a student to enter the amount of money a customer owes, then an amount of money the customer has paid. The program should calculate the amount of change, as well as how many dollars, quarters, dimes, nickels, and pennies to give back to the customer. Display an appropriate message if the amount paid is less than the amount owed."

The last part is easy, but I'm really not sure how to get the exact change needed since we're using double values for currency, and can't use fmod. :(
Truncate and multiply by 100 to work with cents at the integer level.
 ``123456789101112131415161718192021222324252627282930313233`` ``````struct change { int toonie; int loonie; int quarter; int dime; int nickel; int penny; }; change GetChange(int cents) { change c; c.toonie = cents / 200; cents = cents % 200; c.loonie = cents / 100; cents = cents % 100; c.quarter = cents / 25; cents = cents % 25; c.dime = cents / 10; cents = cents % 10; c.nickel = cents / 5; cents = cents % 5; c.penny = cents; return c; }``````
Last edited on
Josue, that should work perfectly! I knew I was probably missing something silly.

Stewbond, sadly, due to the professor not wanting anything but stuff we've discussed, I'm not sure he'd accept that.
What haven't you discussed? Structures? You should still be able to use the exact same algorithm anywhere though.

 ``123456789`` ``````int quarter = cents / 25; // This give you a whole number for the number of quarters. cents = cents % 25; // This gives us the remainder (or the cents left over) int dime = cents / 10; // how many times can we use cents = cents % 10; // And this is the remainder // repeat. ``````

alternative:

 ``12345`` ``````int quarter = cents / 25; cents = cents - (quarter * 25); int dime = cents / 10; cents = cents - (dime * 10);``````
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include "stdafx.h" #include "iostream" using namespace std; int main() { //Variables double amountPaid = 0.0; double amountOwed = 0.0; double difference = 0.0; const double dollar = 1.0; const double quarter = 0.25; const double dime = 0.1; const double nickel = 0.05; const double penny = 0.01; int dollarsNeeded = 0; int quartersNeeded = 0; int dimesNeeded = 0; int nickelsNeeded = 0; int penniesNeeded = 0; double currentOwed = 0.0; //Input cout << "Please enter the amount owed: "; cin >> amountOwed; cout << "Please enter the amount paid: "; cin >> amountPaid; //Calculations difference = amountPaid - amountOwed; dollarsNeeded = difference/dollar; quartersNeeded = (difference - dollarsNeeded) / quarter; dimesNeeded = (difference - dollarsNeeded - quartersNeeded*quarter) / .1; nickelsNeeded = (difference - dollarsNeeded - quartersNeeded*quarter - dimesNeeded*dime) / .5; penniesNeeded = (difference - dollarsNeeded - quartersNeeded*quarter - dimesNeeded*dime - nickelsNeeded*nickel) / .01; //Output if (amountPaid >= amountOwed) //Subtracts the amount owed from the payment, then displays exact change { cout << "The change is: " << difference << endl; cout << "Please give the customer: " << endl; cout << dollarsNeeded << " Dollars" << endl; cout << quartersNeeded << " Quarters" << endl; cout << dimesNeeded << " Dimes" << endl; cout << nickelsNeeded << " Nickels" << endl; cout << penniesNeeded << " Pennies" << endl; } else { currentOwed = (amountOwed - amountPaid); cout << "The customer still owes: " << currentOwed << endl; }//Endif system("pause"); return 0; }``````

I went with this code, which I actually think is an adaptation of what you wrote Stew.

I apologize, as I said, I'm really new, and for some reason the "change GetChange(int cents)" line and "struct change" messed with my head. I thought they were some reserved word for something we weren't taught yet.
Last edited on
Topic archived. No new replies allowed.