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 03-Dec-2004, 10:43
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road

C Pointer problem


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:
CPP / C++ / C Code:
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)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:
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;}
--------------------------------------------------------------------------------------------------------------------------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

--------------------------------------------------------------------------------------------------------------------------
CPP / C++ / C Code:
#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);}


			}
}
--------------------------------------------------------------------------------------------------------------------------
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  
Old 03-Dec-2004, 15:28
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 04-Dec-2004, 06:55
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
int getData(int *paid,int *due)

and in main you call
CPP / C++ / C Code:
int getData()

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:
  int paid;
  int due;
  while(getData(&paid,&due)){
(also your use of getData return value is very wrong)

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:
 scanf ("%d %d",&paid,&due);

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:
scanf("%d %d",paid,due)

Also, you have:
CPP / C++ / C Code:
if (paid==0&&due==0)

This is comparing the address not the data. You need to use:
CPP / C++ / C Code:
if(*paid == 0 && *due == 0)

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!
  #4  
Old 04-Dec-2004, 09:17
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
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:
#include <stdio.h>
#include <math.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){
	int ten=0,twenty=0,fifty=0,total=0;
	c10=&ten;
	c20=&twenty;
	c50=&fifty;
	dollars=&total;
dollars=round(*paid-*due);
change=total/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()

{

	int paid;
	int due;
	int c10,c20,c50,dollars,change;
		do {
		getData();	
		
		while (*due!=0&&*paid!=0)
		{
		dispense();

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

			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);}

		}
		} while (getData()==1);
	}


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  
Old 04-Dec-2004, 09:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by fwongmc
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.
If you point out where the error happens we can. 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.
__________________

Age is unimportant -- except in cheese
  #6  
Old 04-Dec-2004, 09:36
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Quote:
Originally Posted by WaltP
If you point out where the error happens we can. 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.

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  
Old 04-Dec-2004, 10:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
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  
Old 04-Dec-2004, 12:10
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
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:
#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
  #9  
Old 04-Dec-2004, 12:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
CPP / C++ / C Code:
#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;	
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 *'

These are assignment statements and must be placed within a function (like main()). Also, paid and due are not defined

CPP / C++ / C Code:
/*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);
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
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 GIDBlogDeveloping GUIs with wxPython (Part 2) 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
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

All times are GMT -6. The time now is 06:41.


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