Hello everybody!I am a new to C.My lecture asked us to write a program with pointers.But I do think I get stuck on pointer's stuff.Can anyone please do me a flavour?It is kinda urgent stuffs.
The following are the questions:
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))
----------------------------------------------
The following is my work:
void dispense(int change,int *dollars,int *c10,int *c20,int *c50){
dollars=round(paid-due);
change=dollars/10;
while (change>=1&&change<=9){
switch (change) {
case '1' :c10+=1;
break;
case '2' :c20+=1;
break;
case '3' :c10+=1;
c20+=1;
break;
case '4' :c20+=2;
break;
case '5' :c50+=1;
break;
case '6' :c10+=1;
c50+=1;
break;
case '7' :c20+=1;
c50+=1;
break;
case '8' :c10+=1;
c20+=1;
c50+=1;
break;
case '9' :c20+=2;
c50+=1;
break;
default:printf ("\nCheck your input!");
printf ("\nEnter the amount-paid and amount-due please:");
}
}
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
The following is my work:
int getData(int *paid,int *due){
printf ("\nThe amount-paid and amount-due 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;}
c)Your main program should repeat the following until both the amount paid and amount due are zeros.
- call the function getData to ask for the amount paid and amount due
- 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
My code
#include <stdio.h>
int getData(int *paid,int *due){
printf ("\nThe amount-paid and amount-due 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;}
void dispense(int change,int *dollars,int *c10,int *c20,int *c50){
dollars=round(paid-due);
change=dollars/10;
while (change>=1&&change<=9){
switch (change) {
case '1' :c10+=1;
break;
case '2' :c20+=1;
break;
case '3' :c10+=1;
c20+=1;
break;
case '4' :c20+=2;
break;
case '5' :c50+=1;
break;
case '6' :c10+=1;
c50+=1;
break;
case '7' :c20+=1;
c50+=1;
break;
case '8' :c10+=1;
c20+=1;
c50+=1;
break;
case '9' :c20+=2;
c50+=1;
break;
default:printf ("\nCheck your input!");
printf ("\nEnter the amount-paid and amount-due please:");
}
}
int main ()
{
do {
getData();} while getData==1;
dispense();
printf ("\namount due:%d",due);
printf ("amount paid:%d",paid);
printf ("and thus change=%d",change);
if (change<5) {
if (c20==0){
printf ("You are suggested to give him/her %d 10-cents coin(s)",c10);}
if (c10==0){
printf ("You are suggested to give him/her %d 20-cents coin(s)",c20);}
else
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coiu(s)",c10,c20);}
else if (change>5) {
if (c10==0){
printf ("You are suggested to give him/her %d 20-cents coin(s) %d 50-cents coin(s)",c20,c50);}
if (c20==0){
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 50-cents coin(s)",c10,c50);}
else
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s) %d 50-cents coin(s)",c10,c20,c50);}
else if (change==5) {
printf ("You are suggested to give him/her %d 50-cents coin(s)",c50);}
}
}
When I complier my program,the debugger told me that my program have 22 errors.
I am trying to find out the errors(some of the error are pointers errors)
Can anyone of your help me to debug my program?