![]() |
|
#1
|
|||
|
|||
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:
C:\Documents and Settings\User\桌面\proto.c(3 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 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
|
|||
|
|||
|
Quote:
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:
Did you look at line 38? CPP / C++ / C Code:
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:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
So: first of all do what you know that you are supposed to do: CPP / C++ / C Code:
(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:
As you are going through the calculations, put printf() that shows what values are being used and what the results are. Regards, Dave |
|
#6
|
|||
|
|||
|
Quote:
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 GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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