![]() |
Well,I read my book again and I understand the use of functions and pointers.But I am still experiencing a problem,is one of my function cannot return accurate result.I gues my function is correct,but don't know why it cannot return accurate result,can anyone try to look and tell me where I made a mistake?
Question again(for reference) Write a program to dispense change.The user enters the amount paid and the amount due.The program determines how many dollars, 50cents,20cents,10cents should be given as change. a)Write a function with the heading: void dispense(int change,int *dollars,int *c10,int *c20,int *c50) that determines and return the quantity of each kind of coin. (Note:16 cents-5 cents=11 cents meaning that one 10cents is returned for charge.However 16 cents-1 cents=15 cents meaning that one 20-cents is required(round-up)) -------------------------------------------------------------------------- b)Write a function int getData(int *paid,int *due) that doe the following: inform the user that amount paid and amount due should be entered in cents(integer) do prompt the user to enter the amount paid and amount due read in the data while (amount due<0 or amount paid<amount due); if both amount paid and amount due are zeros return 0 otherwise return 1 --------------------------------------------------------------------------c)Your main program should repeat the following until both the amount paid and amount due are zeros. 1.call the function getData to ask for the amount paid and amount due 2.print out the charge and the # of each kind of coins to dispense. Program output: The amount-paid and amount-due should be entered in cents (integer) To terminate the program,enter 0 for both values Enter the amount paid and amount due please:16 5 amount due:5,amount paid:16,and thus change=11 You are suggested to give him/her 1 10-cents coin(s) The amount-paid and amount-due should be entered in cents (integer) To terminate the program,enter 0 for both values Enter the amount paid and amount due please:26 5 amount due:5,amount paid:26,and thus change=21 You are suggested to give him/her 1 20-cents coin(s) The amount-paid and amount-due should be entered in cents (integer) To terminate the program,enter 0 for both values Enter the amount paid and amount due please:66 5 amount due:5,amount paid:66,and thus change=61 You are suggested to give him/her 1 50-cents coin(s) 1 10-cents coin(s) The amount-paid and amount-due should be entered in cents (integer) To terminate the program,enter 0 for both values Enter the amount paid and amount due please:81 5 amount due:5,amount paid:81,and thus change=76 You are suggested to give him/her 1 50-cents coin(s) 1 20-cents coin(s) 1 10-cents coin(s) The amount-paid and amount-due should be entered in cents (integer) To terminate the program,enter 0 for both values Enter the amount paid and amount due please:5 81 Enter the amount paid and amount due please:0 0 -------------------------------------------------------------------------- :
|
Well,what I can tell you is the problem probably occurs in the pointers in main() function,but it may happen if I declar the function dispense wrongly.
and one more thing,the program should terminate when I enter 0,0,the function will repeat when Ienter a<b(assume a is the first number,b is the second number) and thus enter (a>b) will do the calculations.When run the program,in no doublt,when I enter (a>b) and (a<b) it is functable,but when Ienter 0,0,it will loop itself once and if I enter 0,0,it will then terminate.Can anyone tell me if it is error from my looping or I haven't set the initial value? |
| All times are GMT -5. The time now is 3:26 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC