Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C Pointer Problem (http://www.programmingforums.org/showthread.php?t=1399)

fwongmc Dec 3rd, 2004 12:48 PM

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:

:

[b]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]

--------------------------------------------------------------------------------------------------------------------------
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:

:

[b]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;}[/b]

--------------------------------------------------------------------------------------------------------------------------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

--------------------------------------------------------------------------------------------------------------------------
:

[b]#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);}


        }
}[/b]

--------------------------------------------------------------------------------------------------------------------------
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?

kurifu Dec 3rd, 2004 10:57 PM

int *c20 = new int;

does not mean you can do:

c20 = 20; (or any other number).

You need to dereference your pointers since those operations are only adding the the memory address, not the actual value the pointer dereferences.

do this instead....

*c20 = 20;

fwongmc Dec 4th, 2004 11:09 AM

Dear all,

Thanks for help these days.I tried to rewrite the program part by part,step by step again.Although finally error is reduced,but they still exists.And I am here to heop if your guys can help me to correct it,and if possible,help me to check if there's any undetected error from my source as well.

:

#include <stdio.h>
#include <math.h>
        /*initilize pointed values into 0*/
        int c5=0;
        int c2=0;
        int c1=0;
        int a_paid=1;
        int a_due=1;
        int dollars;
        int change;

/*assiging initialize value to the pointers*/
        paid=&a_paid;
        due=&a_due;       
/*function initialization*/
int getData(int *paid,int *due);
/*function initialization*/
void dispense(int change,int *dollars,int *c50,int *c20,int *c10);

int main()

{

        /*repeat the loop until user enters both 0*/
do {
        /*prompt user to enter 2 numbers*/
        getData (&paid,&due);
/*while the pointed values a_due(pointed by *due) and a_paid(pointed by *paid) not equals to 0 then do*/
while (&due!=0 && &paid!=0){
        /*calculate the change and the number of the respected pointers(cents)*/
        dispense (&a_paid,&a_due);

        printf ("\namount due:%d",due);
        printf ("amount paid:%d",paid);
        printf ("and thus change=%d",dollars);

        /*display specific output to user depending if the pointed values(cents) are 0*/
        if (change<5)        {
        if (c2==0)        {
  printf ("You are suggested to give him/her %d 10-cents coin(s)",c1);}
        if (c1==0)        {
  printf ("You are suggested to give him/her %d 20-cents coin(s)",c2);}
        else
  printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s)",c1,c2);}

        else if (change>5)        {
        if (c2==0)        {
  printf ("You are suggested to give him/her %d 10-cents coin(s) %d 50-cents coin(s)",c1,c5);}
        if (c1==0)        {
  printf ("You are suggested to give him/her %d 20-cents coin(s) %d 50-cents coin(s) ",c2,c5);}
        else
  printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s) %d 50-cents coin(s)",c1,c2,c5);}       

        else if (change==5) {
  printf ("You are suggested to give him/her %d 50-cents coin(s)",c5);}
  }
  } while (getData (&paid,&due)==1);
        }

int getData (int *paid,int *due){


 printf ("\nThe amount-paid and amount-due should be entered in cents(integer)");
 /*repeat until due<0 or paid<due*/
 do {
        printf ("\nTo terimate the program,enter 0 for both values.");
        printf ("\nEnter the amount-paid abd amount-due please:");
        scanf ("%d %d",&a_paid,&a_due);        } while (*due<0||*paid<*due);

        if (*paid==0&&*due==0){
  return 0;}
        else {
  return 1;}
}

void dispense (int change,int *dollars,int *c50,int *c20,int *c10){

/*By substract the paid and due and divide it by 10 to obtain the actual change,and round it up to the nearest 10*/
        change=(*paid-*due);
        dollars=&change;
      rounded=round(change);
        /*while the change is in range do the followings*/
        while (change>=10&&change<=90) {
 switch (rounded){
 case '10':c1+=1;
        break;
 
 case '20':c2+=1;
    break;

 case '30':c1+=1;
  c2+=1;
  break;

 case '40':c2+=2;
  break;

 case '50':c5+=1;
  break;

 case '60':c1+=1;
  c5+=1;
  break;

 case '70':c2+=1;
  c5+=1;
  break;

 case '80':c1+=1;
  c2+=1;
  c5+=1;
  break;

 case '90':c2+=2;
  c5+=1;
  break;

 default:printf ("\nCheck your input!");
        printf("\nEnter the amount paid and amount-due please:"); }
        }
}


Detected Errors:
warning C4047: 'initializing' : 'int ' differs in levels of indirection from 'int *'
A:\01.c(14) : warning C4047: 'initializing' : 'int ' differs in levels of indirection from 'int *'
A:\01.c(31) : warning C4047: 'function' : 'int ' differs in levels of indirection from 'int *'
A:\01.c(31) : warning C4024: 'dispense' : different types for formal and actual parameter 1
A:\01.c(31) : error C2198: 'dispense' : too few actual parameters
A:\01.c(81) warning C4013: 'roundoff' undefined; assuming extern returning int /*I don't know what is the function that can round up 11-->10,15-->20*/
A:\01.c(79) : error C2100: illegal indirection
A:\01.c(79) : error C2100: illegal indirection

Eggbert Dec 4th, 2004 4:06 PM

You're having quite a bit of trouble with the concept of pointers and functions. I would recommend starting over, but this time writing a little and compiling before writing again. This way you don't end up with a full program and pages of errors to fix. You can find and fix the errors as you create them.

fwongmc Dec 8th, 2004 10:34 AM

My program can run,but warning were display on Vc++

:

#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 () {
/*initialization*/
int paid,due,dollars,c50,c20,c10,trans;
 /*prompt user for data entry*/
 getData(&paid,&due);
 /*check if both input is not zero*/
 while (paid!=0&&due!=0){
 /*calculate for the change*/
 trans=paid-due;
  /*prompt user if suitable*/
  printf ("amount due:%d",due);
  printf (" amount paid:%d",paid);
  printf (" and thus change:%d",paid-due);
  dispense(trans,&dollars,&c50,&c20,&c10);
  /*Reprompt for user input in order to continue the looping*/       
          getData(&paid,&due);
  }
  }

/*function dispense main*/
void dispense (int change,int *dollars,int *c50,int *c20,int *c10) {
/*temp store variable*/

int tmp,tmp2;

/*check if the last digit>=5*/
tmp=(change%10);
/*if the last digit>=5*/
if (tmp>=5) {
        dollars=((change/10)*10)+10;       
        tmp2=dollars;        }
/*if the last digit<5*/
else if (tmp<5) {       
        dollars=(change/10)*10;       
        tmp2=dollars; }
/*different cases*/
        switch (tmp2) {
        case 10:c10=1;
  c20=0;
  c50=0;
  break;
        case 20:c10=0;
  c20=1;
  c50=0;
  break;
        case 30:c10=1;
  c20=1;
  c50=0;
  break;
        case 40:c10=0;
  c20=2;
  c50=0;
  break;
        case 50:c10=0;
  c20=0;
  c50=1;
  break;
        case 60:c10=1;
  c20=0;
  c50=1;
  break;
        case 70:c10=0;
          c20=1;
  c50=1;
    break;
        case 80:c10=1;
  c20=1;
  c50=1;
  break;
        case 90:c10=0;
  c20=2;
  c50=1;
  break;
  /*else*/
        default:printf ("You inputted number >90,range exceed.");        }
        if (dollars<50)        {
  /*if c20 is null*/
  if (c20==0)        {
          printf ("\nYou are suggested to give him/her %d 10-cents coin(s)\n",c10);        }
  /*if c10 is null*/
  if (c10==0)        {
          printf ("\nYou are suggested to give him/her %d 20-cents coin(s)\n",c20);        }
  /*if both c10/c20 are not null*/
  if (c10!=0&&c20!=0)
          printf ("\nYou are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s)\n",c10,c20);        }
  /*if the rounded change is larger than 50*/       
          else if (dollars>50)        {
  /*if c20 is null*/
  if (c20==0)        {
          printf ("\nYou are suggested to give him/her %d 10-cent coin(s) %d 50-cent coin(s)\n",c10,c50);        }
  /*if c10 is null*/
  if (c10==0)        {
          printf ("\nYou are suggested to give him/her %d 20-cent coin(s) %d 50-cent coin(s)\n",c20,c50);        }
  /*if both c10/c20 are not null*/
  if (c10!=0&&c20!=0)
          printf ("\nYou are suggested to give him/her %d 10-cent coin(s) %d 20-cent coin(s) %d 50-cent coin(s)\n",c10,c20,c50);        }
  /*if the rounded change=50*/       
          else if (dollars==50)       
  printf ("\nYou are suggested to give him/her %d 50-cent coin(s)\n",c50); }

        /*function getData main*/
        int getData (int *paid,int *due) {
 do { 
    printf ("The amount-paid and amount-due should be entered in cents(integer)");
  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;}



Error:

C:\Documents and Settings\User\桌面\proto.c(38) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int '
C:\Documents and Settings\User\桌面\proto.c(39) : warning C4047: '=' : 'int ' differs in levels of indirection from 'int *'
C:\Documents and Settings\User\桌面\proto.c(42) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int '
C:\Documents and Settings\User\桌面\proto.c(43) : warning C4047: '=' : 'int ' differs in levels of indirection from 'int *'
C:\Documents and Settings\User\桌面\proto.c(46) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(51) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(54) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(55) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(59) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(64) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(66) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(68) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(71) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(72) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(74) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(75) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(76) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(79) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(80) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(84) : warning C4047: '<' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(95) : warning C4047: '>' : 'int *' differs in levels of indirection from 'const int '
C:\Documents and Settings\User\桌面\proto.c(106) : warning C4047: '=' : 'int *' differs in levels of indirection from 'const int '


All times are GMT -5. The time now is 3:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC