![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
pointer number alteration
Purpose: To take whole dollar ammounts inputed by the user and break it down into the least ammount of bills.
Error: The tens denomination either adds or subtracts from itself. Ex.$77 is put in, the display shows 1:50,1:20,1:10,1:5 and 2:1 that ten isn't needed void calculate (int cash_input,int *fifties,int *twenties,int *tens,int
*fives,int *ones)
//declares calculate function
{
int money_rem=0; //intializes money_rem to 0
*fifties=cash_input/50; //sets number of fifties to input/50
money_rem=cash_input%50; //takes money minus the fifties and sets $
*twenties=money_rem/20; //sets number of twenties to money_rem/20
money_rem=cash_input%20; //takes money minus the tewties and sets $
*tens=money_rem/10; //sets number of tens to money_rem/10
money_rem=cash_input%10; //takes money minus the tens and sets to $
*fives=money_rem/5; //sets number of fives to money_rem/5
money_rem=cash_input%5; //takes money minus fives and sets to mon$
*ones=money_rem; //sets ones to remaining money
return ; //returns power to main function
}Blight |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
what is the code you call the function with it? It could be a round down error, but i recalculated it and i don't think that will be the problem.
welcome to the forums, btw ![]() |
|
|
|
|
|
#3 | |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
Quote:
void calculate (int,int*,int*,int*,int*,int*); Declaration: calculate(cash_input,&fifties,&twenties,&tens,&fives,&ones); Cash input is a variable I saved in main that was created in another fuction. And the calculation and displaying are done in seperate functions if that changes anything. |
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2005
Location: Missouri
Posts: 9
Rep Power: 0
![]() |
This works for me
void count(int cash_input, int *fifties, int *twenties, int *tens, int *fives, int *ones)
{
*fifties=cash_input/50; //sets number of fifties to input/50
cash_input -= ((*fifties) * 50); //takes money minus the fifties and sets $
*twenties=cash_input/20; //sets number of twenties to money_rem/20
cash_input -= ((*twenties) * 20); //takes money minus the tewties and sets $
*tens=cash_input/10; //sets number of tens to money_rem/10
cash_input -= ((*tens) * 10); //takes money minus the tens and sets to $
*fives=cash_input/5; //sets number of fives to money_rem/5
cash_input -= ((*fives) * 5); //takes money minus fives and sets to mon$
*ones=cash_input;
}I think your mismatch had something to do with how you were setting money_rem |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In case you haven't picked up on it yet, you can't use the original "cash input" over and over without adjusting it for what you've removed. You either have to adjust it as Sysop did, or quit using it and use "money rem" in the same way. A little exercise with pencil and paper will show you what I mean, or a few bills and coins.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Oct 2005
Posts: 17
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|