![]() |
|
#1
|
|||
|
|||
C Pointer problemHello 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: CPP / C++ / C Code:
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: CPP / C++ / C Code:
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 -------------------------------------------------------------------------------------------------------------------------- CPP / C++ / C Code:
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? |
|
#2
|
||||
|
||||
|
Please copy the errors from your output console and paste them to the forum.
Also, read this: http://www.gidforums.com/t-689.html __________________
-Aaron |
|
#3
|
||||
|
||||
|
Hello fwongmc. Please post your code using code tags. Simply put [c] at the first of where your code is in your post and the [/c] at the end of your code. You will find more people are willing to help if you use code tags. (also this may be a good read for you on formatting your c code)
Now, for the problem at hand. Like aaron said we may not be able to help with all of your errors, but there are a few things that caught my eye. First and foremost, the way you call your functions. You have a function header of: CPP / C++ / C Code:
and in main you call CPP / C++ / C Code:
getData is expecting a couple of pointers. Dispense has the exact same problem. You need to define these variables in your main and then pass them to the appropriate functions. ie CPP / C++ / C Code:
Now your problem with pointers comes down to your dereferencing. * - Means the data pointed to by the address. & - Means the address of the data. So in your function dispense, you have passed up addresses and you want to work on the data You have: CPP / C++ / C Code:
What scanf wants is the address of the data. What you are passing is the address of the address of the data. Remember, you already passed the address to the function, so your call should be: CPP / C++ / C Code:
Also, you have: CPP / C++ / C Code:
This is comparing the address not the data. You need to use: CPP / C++ / C Code:
Again, there are many things wrong with your program. I suggest that you take this in small steps and verify each step. Also, I highly recommend reading the formatting tutorial. Good luck! __________________
The best damn Sports Blog period. |
|
#4
|
|||
|
|||
|
I am trying to assign a new variable to initialize the pointers,and I think if the pointed variable chnages,the value of the pointer stored will also change respecivity.
The following are my codes: CPP / C++ / C Code:
But,still,pointer error exits.Can anyone just point out where my error and how to correct it?I will be very please if you can do so. |
|
#5
|
||||
|
||||
|
Quote:
__________________
Age is unimportant -- except in cheese |
|
#6
|
|||
|
|||
|
Quote:
oh..sorry.I forgot to do so.The pointer errors occurs in the function <dispense> and the main.Which pointer?Maybe all.I really need help now. ![]() |
|
#7
|
||||
|
||||
|
I repeat:
[quote]If you point out where the error happens we can help. You have a lot of code and a lot of statements that use pointers. Which ones do you suspect are wrong? If you get compiler errors, you need to tell us what they are and the line they are on. Also place a comment in the code so we don't have to count to the line.[endquote] And if the problem is in a specific area, just post that function or code segment. It's not that hard to tell us what the problem is. In detail. Please. __________________
Age is unimportant -- except in cheese |
|
#8
|
|||
|
|||
|
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. CPP / C++ / C Code:
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 |
|
#9
|
||||
|
||||
|
CPP / C++ / C Code:
A:\01.c(14) : warning C4047: 'initializing' : 'int ' differs in levels of indirection from 'int *' These are assignment statements and must be placed within a function (like main()). Also, paid and due are not defined CPP / C++ / C Code:
A:\01.c(31) : warning C4024: 'dispense' : different types for formal and actual parameter 1 your prototype for your first parameter is int. You are passing the address of value. That's int* A:\01.c(31) : error C2198: 'dispense' : too few actual parameters You defined your prototype for this function with 5 parameters. Where are the other 3? That's as far as I'm willing to count, especially when I mentioned twice to place a comment on the lines you are having problems with. __________________
Age is unimportant -- except in cheese |
Recent GIDBlog
Developing GUIs with wxPython (Part 2) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CD burner will not burn, new problem | sdshaman | Computer Hardware Forum | 4 | 06-Feb-2008 23:17 |
| newbie with pointer problems. | moltarim | C Programming Language | 1 | 14-May-2004 09:46 |
| C I/O problem | kelly80 | C Programming Language | 4 | 27-Apr-2004 20:15 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 07:53 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The