![]() |
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){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){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>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? |
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; |
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>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 |
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.
|
My program can run,but warning were display on Vc++
:
#include <stdio.h>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