GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Dec-2004, 08:41
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road

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


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


My program can run,but warning were display on Vc++
CPP / C++ / C 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 () {
/*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;}

C:\Documents and Settings\User\桌面\proto.c(3 : 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(6 : 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 '
  #2  
Old 08-Dec-2004, 08:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
My program can run,but warning were display on Vc++

When you say your program can run, what do you mean? Are you saying that it gives the correct answer?

As for the warnings: They are significant, and give you some clues of bugs in your program.

For example, the first one:
Quote:

------------------------------------------------

[b]C:\Documents and Settings\User\桌面\proto.c(38) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int '

Did you look at line 38?

CPP / C++ / C Code:
dollars=((change/10)*10)+10; 

What is dollars: (OK, I'll tell you: it's a pointer to an int)

You are trying to store an integer value (the expression on the right hand size) into a pointer to an int. I'm sure that's a bug.

I am reasonably sure that you meant to say something like

CPP / C++ / C Code:
*dollars=((change/10)*10)+10; 

Now, after you go through and reconcile the compiler messages, try running the program. If I see it correctly, your input would be something like

1500 1234

for a bill if 12.34 and a submission of 15.00

It doesn't seem to be consistent with the problem statement. (But maybe I read it wrong.)

What did you use as input? What do you expect to see as the program output? What do you see?

Regards,

Dave
Last edited by davekw7x : 08-Dec-2004 at 09:33.
  #3  
Old 08-Dec-2004, 09:43
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Well,what I mean that my program can run is it can response with accurate result.

Sure,I know that is an int pointer.When I dd *dollars=(change/10)*10+10
it will gives out only 8XXXX .

I don't know what's the problem.I tried to initialize the pointer into zreo *dollars=0 in the function dispense,but it returns only zero.

Actually i know those were a pointer error,but please,tell me how these problem would happen?for a '*',for example,Initialize four variables,dollars=&a(a=0)..etc the result are still the same.* means the pointed values and & means the pointed address.But in a function,it seems not necessary for using '&' to reslove the address of the pointed stuff.But when Itry to add '*' to those they need to have(*dollars,*c50,*c20,*c10),the program returns wrong value.I also tried to assiging a new variable to the pointer,let the pointer points to this variable(*dollars,*c50,*c20,*c10),and put the variable to subsuize the *variable.The result are still wrong,theorically the value change and if I called to pointer again,new value can be called since I pointed to the variable.I know what pointer is.But,in this program,I am weak.I need someone to explain and clearify my problems,can you do that?I would be appricate if anyone can.
  #4  
Old 08-Dec-2004, 09:48
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
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


The blue one are the user input the first output from loop.
  #5  
Old 08-Dec-2004, 09:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
Well,what I mean that my program can run is it can response with accurate result.

Sure,I know that is an int pointer.When I dd *dollars=(change/10)*10+10
it will gives out only 8XXXX .

I don't know what's the problem.I tried to initialize the pointer into zreo *dollars=0 in the function dispense,but it returns only zero.

So: first of all do what you know that you are supposed to do:

CPP / C++ / C Code:
*dollars = (change/10)*10+10;

(And fix all the other references that give warnings.)

Then put printf() statements to see what you are actually computing and to see what values the various conditional statements are working with:

CPP / C++ / C Code:
  tmp=(change%10);
  printf("change = %d, tmp = %d\n", change, tmp);
  /*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;
 }
  printf("*dollars = %d, tmp2 = %d\n", *dollars, tmp2);

  /*different cases*/
  switch (tmp2) {
   /* etc. */


As you are going through the calculations, put printf() that shows what values are being used and what the results are.

Regards,

Dave
  #6  
Old 08-Dec-2004, 10:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by fwongmc
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 blue one are the user input the first output from loop.

OK, I'm sorry my response was not more helpful. Here's my take on it:

You are storing integer values in a space that is supposed to be a pointer to an int. Since you don't actually use it as a pointer, you retrieve the integer value and it gives the same value back, so it is possible to get the "right answer". This is a guaranteed recipe for disaster, even though it doesn't lead to immediate bad things for your example.

The fact is that your program as you posted it is wrong. You need to understand what pointers are and how to use them (and, in some cases, when to use them and when they don't help you).

My question is: Why did you make dollars an argument to the function? You aren't using it in your main() program at all. If you did try to use it (that is, by trying to print the value it is pointing to) the program would bomb.
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
RSA program fwongmc C Programming Language 11 08-Nov-2004 22:15
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.