Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 3 of 3
Thread: revisedC program with pointers

12062004, 02:59 PM #1
 Join Date
 Dec 2004
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
revisedC program with pointers
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 cents5 cents=11 cents meaning that one 10cents is returned for charge.However 16 cents1 cents=15 cents meaning that one 20cents is required(roundup))

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 amountpaid and amountdue 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 10cents coin(s)
The amountpaid and amountdue 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 20cents coin(s)
The amountpaid and amountdue 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 50cents coin(s) 1 10cents coin(s)
The amountpaid and amountdue 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 50cents coin(s) 1 20cents coin(s) 1 10cents coin(s)
The amountpaid and amountdue 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

Code:#include <stdio.h> /*function getData initialization*/ int getData(int *paid,int *due); /*function dispense initialization*/ void dispense(int change,int *dollars,int *c50,int *c20,int *c10); void main () { /*pass value initialization*/ int paid,due,dollars,c50,c20,c10,trans; /*repeat until both input equals to 0*/ do { /*prompt user for data entry*/ getData(&paid,&due); /*calculate for the change*/ trans=paiddue; /*do while both entry is larger than 0*/ if (paid>0&&due>0) { /*calculation*/ dispense(trans,&dollars,&c50,&c20,&c10); /*prompt for user input*/ printf ("\namount due:%d",due); printf (" amount paid:%d",paid); printf ("and thus change:%d",paiddue); /*if the rounded change smaller than 50*/ if (dollars<50) { /*if c20 is null*/ if (c20==0) { printf ("You are suggested to give him/her %d 10cents coin(s)",c10); } /*if c10 is null*/ if (c10==0) { printf ("You are suggested to give him/her %d 20cents coin(s)",c20); } /*if both c10/c20 are not null*/ else printf ("You are suggested to give him/her %d 10cents coin(s) %d 20cents coin(s)",c10,c20); } /*if the rounded change is larger than 50*/ else if (dollars>50) { /*if c20 is null*/ if (c20==0) { printf ("You are suggested to give him/her %d 10cent coin(s) %d 50cent coin(s)",c10,c50); } /*if c10 is null*/ if (c10==0) { printf ("You are suggested to give him/her %d 20cent coin(s) %d 50cent coin(s)",c20,c50); } /*if both c10/c20 are not null*/ else printf ("You are suggested to give him/her %d 10cent coin(s) %d 20cent coin(s) %d 50cent coin(s)",c10,c20,c50); } /*if the rounded change=50*/ else if (dollars==50) printf ("You are suggested to give him/her %d 50cent coin(s)",c50);} }while (getData(&paid,&due)==1); } /*function dispense main*/ void dispense (int change,int *dollars,int *c50,int *c20,int *c10) { /*temp store variable*/ int tmp; tmp=(change%10); if (tmp>=5) *dollars=((change/10)*10)+10; else if (tmp<5) *dollars=(change/10)*10; switch (*dollars) { case '10':*c10+=1; break; case '20':*c20+=1; break; case '30':*c10+=1; *c20+=1; break; case '40':*c20+=2; break; case '50':*c50+=1; break; case '60':*c10+=1; *c50+=1; break; case '70':*c20+=1; *c50+=1; break; case '80':*c10+=1; *c20+=1; break; case '90':*c20+=2; *c50+=1; break; default:printf ("\nPlease check your input"); } } /*function getData main*/ int getData (int *paid,int *due) { printf ("The amountpaid and amountdue should be entered in cents(integer)"); do { printf ("\nTo terminate the program,enter 0 for both values."); printf ("\nEnter the amount paid and amount due please:"); scanf ("%d %d",paid,due); } while (*due<0  *paid<*due); if (*paid==0&&*due==0) { return 0; } else return 1;}
12062004, 04:49 PM
#2
 Join Date
 Dec 2004
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
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?
12072004, 10:53 PM
#3
 Join Date
 Oct 2004
 Posts
 230
 Thanks
 0
 Thanked 0 Times in 0 Posts
A few things should help you get this thing working. You need to work on code formatting to make things easier to read and debug, so people can help easier.
First, you REALLY need to initialize variables, so you don't see junk values in your output..
Next, move your first call to GetData() outside of the do/while loop and resond properly so you don't ask twice.Code:int paid=0, due=0, dollars=0, c50=0, c20=0, c10=0, trans=0;
Next, your dispense function needs some work, dollars is an int, so why are you switching on characters?Code:/*prompt user for data entry*/ if( getData(&paid,&due)== 0 ) { puts("goodbye"); return 0; } /*repeat until both input equals to 0*/ do { /* rest of code */
Fix those first, then you can move on to fixing the other problems. Also remember that void main() is not standard, you should always use int main().Code:switch (*dollars) { /* case '10': < this is wrong, switch on value instead */ case 10: *c10+=1; break; /* rest of code */