|
-
March 5th, 2004, 01:11 AM
#1
I've run into a bit of a problem
I am taking a c++ class and I have a program due, the assignment is to write a program that uses functions with reference parameters that will have the user input the amount of a purchase and the amount tendered, then it will calculate how many 10 dollar bills, 5 dollar bills, 1 dollar bills, quarters, dimes, nickels, pennies are due back, then output it. I've run into a problem because I'm not quite exactly sure how to start it, mainly Im not sure how I would do the math (mainly the math is the part I'm having trouble in). I forgot to add that int main() can only include function calls and variable declarations, all the math and stuff must be done in functions...
my calcChangedue function is
void calcChanDue(double amtTen, double purchtotal, double &tendoll, double &fivedoll,double &onedoll, double &quarter, double &dime, double &nickel, double &penny);
{
double difference=amtTen-purchtotal;
//ten dollar bill
therest=difference%10;
tendoll=ceil(difference/10);
//fivedollar bill
therest=therest%5;
fivedoll=ceil(therest/5);
//onedollarbill
therest=therest%1;
onedoll=ceil(therest/1);
//quarters
therest=therest%.25;
quarter=ceil(therest/.25);
//dimes
therest=therest%.10;
dime=ceil(therest/.10);
//nickels
therest=therest%.05;
nickel=ceil(therest/.05);
//pennies
therest=therest%.01;
penny=ceil(therest/.01);
return;
}
//end function
Last edited by chaos9786; March 5th, 2004 at 01:13 AM.
-
March 5th, 2004, 01:26 AM
#2
I don't think the mod operator (%) works like that with doubles. When you get down to cents, I'd suggest multiplying by 100 and using integers.
-
March 5th, 2004, 01:30 AM
#3
thanks
wow, thanks. I should have picked that up...
-
March 5th, 2004, 01:35 PM
#4
Why even use a float or double to begin with? Fixed-level math can better be done with integers, and then only needs to be converted to decimal in the output.
Code:
int amountInCents = 12991;
int working = amountInCents;
int tens = working / 1000;
working -= tens * 1000;
int ones = working / 100;
working -= ones * 100;
int quarters = working / 25;
working -= quarters * 25;
int dimes = working / 10;
working -= dimes * 10;
int nickles = working / 5;
working -= nickles * 5;
int pennies = working;
printf("$%d.%02d is %d tens, %d ones, %d quarters, %d dimes, %d nickles, and %d pennies!\n", amountInCents / 100, amountInCents % 100, tens, ones, quarters, dimes, nickles, pennies);
OMG, that ran right the first time, too! 12 minutes, including manhandling Visual C++ to create a new project.
Adding extra denominations (twenties, fives, 50-cent pieces, 2-dollar bills, 3.14-dollar bills, etc.) is left as an exercise.
-
March 5th, 2004, 01:44 PM
#5
Originally posted by Gorgor
OMG, that ran right the first time, too! 12 minutes, including manhandling Visual C++ to create a new project.
Funny how those "killer programming assignments" back in college days now take just a few minutes to complete 
The old "convert a number to words" assignment was the classic problem where the students would fret and bite their nails to complete, and some never completed it (given three weeks to finish it). In reality, a good programmer could finish that assignment in half an hour.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 5th, 2004 at 01:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|