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

Revised--C/Pointers problem


Well,I read my book again and I understand the use of functions and pointers.But I am still experiencing a problem,is one of my function cannot return accurate result.I gues my function is correct,but don't know why it cannot return accurate result,can anyone try to look and tell me where I made a mistake?

Question again(for reference)
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

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

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 () {
/*pass value initialization*/
int paid,due,dollars,c50,c20,c10,trans;
/*repeat until both input equals to 0*/
	do	{
		/*prompt user for data entry*/
		getData(&paid,&due);
		/*calculate for the change*/
		trans=paid-due;
		/*do while both entry is larger than 0*/
			if  (paid>0&&due>0)	{
				/*calculation*/
				dispense(trans,&dollars,&c50,&c20,&c10);
				/*prompt for user input*/
				printf ("\namount due:%d",due);
				printf (" amount paid:%d",paid);
				printf ("and thus change:%d",paid-due);
				/*if the rounded change smaller than 50*/
					if (dollars<50)	{
						/*if c20 is null*/
						if (c20==0)	{
							printf ("You are suggested to give him/her %d 10-cents coin(s)",c10);	}
						/*if c10 is null*/	 
						if (c10==0)	{
							printf ("You are suggested to give him/her %d 20-cents coin(s)",c20);	}
						/*if both c10/c20 are not null*/
						else	
							printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s)",c10,c20);	}
				/*if the rounded change is larger than 50*/	
					else if (dollars>50)	{
						/*if c20 is null*/
						if (c20==0)	{
							printf ("You are suggested to give him/her %d 10-cent coin(s) %d 50-cent coin(s)",c10,c50);	}
						/*if c10 is null*/
						if (c10==0)	{
							printf ("You are suggested to give him/her %d 20-cent coin(s) %d 50-cent coin(s)",c20,c50);	}
						/*if both c10/c20 are not null*/
						else 
							printf ("You are suggested to give him/her %d 10-cent coin(s) %d 20-cent coin(s) %d 50-cent coin(s)",c10,c20,c50);	}
				/*if the rounded change=50*/	
					else if (dollars==50)	
						printf ("You are suggested to give him/her %d 50-cent coin(s)",c50);} 
							}while (getData(&paid,&due)==1);	}

/*function dispense main*/
void dispense (int change,int *dollars,int *c50,int *c20,int *c10) {
/*temp store variable*/
int tmp;
tmp=(change%10);

if (tmp>=5) 
	*dollars=((change/10)*10)+10;	
else if (tmp<5)	
	*dollars=(change/10)*10;	

	switch (*dollars) {
	case '10':*c10+=1;
			  break;
	case '20':*c20+=1;
			  break;
	case '30':*c10+=1;
			  *c20+=1;
			  break;
	case '40':*c20+=2;
			  break;
	case '50':*c50+=1;
			  break;
	case '60':*c10+=1;
			  *c50+=1;
			  break;
	case '70':*c20+=1;
			  *c50+=1;
			  break;
	case '80':*c10+=1;
			  *c20+=1;
			  break;
	case '90':*c20+=2;
			  *c50+=1;
			  break;
	default:printf ("\nPlease check your input");	}
	}

	/*function getData main*/
	int getData (int *paid,int *due) {
		printf ("The 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;}
  #2  
Old 06-Dec-2004, 08:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
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,I read my book again and I understand the use of functions and pointers.But I am still experiencing a problem,is one of my function cannot return accurate result.I gues my function is correct,but don't know why it cannot return accurate result,can anyone try to look and tell me where I made a mistake?


Not likely, since you didn't tell us what function you are having problems with, what input you gave it, what output you expected and what you got.

Here's some general directions for debugging:

When you get input from the user, print out the values that the program is actually using, so that you can see it's working on what you think it is.

When your program enters a function, print out all of the values of the arguments, so that you can see that the function is working on the things that you think it is.

Before calculations, print out values of variables that are used in the calculation. After calculations print out the value of the result.

For example in main()
CPP / C++ / C Code:
  do  {
    /*prompt user for data entry*/
    getData(&paid,&due);
    /*calculate for the change*/
    trans=paid-due;
    printf("paid = %d, due = %d, trans = %d\n", paid, due, trans);/* for debug */
    /*do while both entry is larger than 0*/
      if  (paid >0 && due >0)  {
        /*calculation*/
        dispense(trans,&dollars,&c50,&c20,&c10);

In function dispense:

CPP / C++ / C Code:
  /*temp store variable*/
  int tmp;

  printf("Entering dispense: change = %d\n", change);
  printf("*c50 = %d, *c20 = %d, *c10 = %d\n", *c50, *c20, *c10);

  tmp=(change%10);
  printf("tmp = %d\n", tmp);

and

CPP / C++ / C Code:
 if (tmp>=5) 
    *dollars=((change/10)*10)+10;  
  else if (tmp<5)  
    *dollars=(change/10)*10;  
  printf("*dollars = %d\n", *dollars);

etc.


Regards,

Dave
  #3  
Old 06-Dec-2004, 09:46
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
Well,what I can tell you is the problem probably occurs in the pointers in main() function,but it may happen if I declar the function dispense wrongly.

and one more thing,the program should terminate when I enter 0,0,the function will repeat when Ienter a<b(assume a is the first number,b is the second number) and thus enter (a>b) will do the calculations.When run the program,in no doublt,when I enter (a>b) and (a<b) it is functable,but when Ienter 0,0,it will loop itself once and if I enter 0,0,it will then terminate.Can anyone tell me if it is error from my looping or I haven't set the initial value?
  #4  
Old 06-Dec-2004, 09:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
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 can tell you is the problem probably occurs in the pointers in main() function,but it may happen if I declar the function dispense wrongly.


Did you put the print statements in dispense() that I showed? Are you initializing all variables used in the "+=" cases?

Quote:
and one more thing,the program should terminate when I enter 0,0,the function will repeat when Ienter a<b(assume a is the first number,b is the second number) and thus enter (a>b) will do the calculations.When run the program,in no doublt,when I enter (a>b) and (a<b) it is functable,but when Ienter 0,0,it will loop itself once and if I enter 0,0,it will then terminate.Can anyone tell me if it is error from my looping or I haven't set the initial value?

Put the following in place to see that the program is reading what you think it is. Then check the logic of your loop condition.

CPP / C++ / C Code:
    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);  
        printf("You entered %d, %d\n", *paid, *due);
    }  while (*due<0 || *paid<*due);

Then, if you think the logic in dispense() is OK, look at the place(s) where you call dispense(). Check the return value to see if it's really returning the value you meant for it to.

Regards,

Dave
  #5  
Old 06-Dec-2004, 10:06
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
I did tried,but the answer is still the same after initialization,the answer is still -855993460
  #6  
Old 06-Dec-2004, 10:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
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
I did tried,but the answer is still the same after initialization,the answer is still -855993460

Show me your most recent code.

Regards,

Dave
  #7  
Old 06-Dec-2004, 10:22
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
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 () {
/*pass value initialization*/
int paid,due,dollars,c50,c20,c10,trans;
/*repeat until both input equals to 0*/
	do	{
		/*prompt user for data entry*/
		getData(&paid,&due);
		/*calculate for the change*/
		trans=paid-due;
		/*do while both entry is larger than 0*/
			if  (paid>0&&due>0)	{
				/*calculation*/
				dispense(trans,&dollars,&c50,&c20,&c10);
				/*prompt for user input*/
				printf ("\namount due:%d",due);
				printf (" amount paid:%d",paid);
				printf (" and thus change:%d",paid-due);
				/*if the rounded change smaller than 50*/
					if (dollars<50)	{
						/*if c20 is null*/
						if (c20==0)	{
							printf ("You are suggested to give him/her %d 10-cents coin(s)",c10);	}
						/*if c10 is null*/	 
						if (c10==0)	{
							printf ("You are suggested to give him/her %d 20-cents coin(s)",c20);	}
						/*if both c10/c20 are not null*/
						else	
							printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s)",c10,c20);	}
				/*if the rounded change is larger than 50*/	
					else if (dollars>50)	{
						/*if c20 is null*/
						if (c20==0)	{
							printf ("You are suggested to give him/her %d 10-cent coin(s) %d 50-cent coin(s)",c10,c50);	}
						/*if c10 is null*/
						if (c10==0)	{
							printf ("You are suggested to give him/her %d 20-cent coin(s) %d 50-cent coin(s)",c20,c50);	}
						/*if both c10/c20 are not null*/
						else 
							printf ("You are suggested to give him/her %d 10-cent coin(s) %d 20-cent coin(s) %d 50-cent coin(s)",c10,c20,c50);	}
				/*if the rounded change=50*/	
					else if (dollars==50)	
						printf ("You are suggested to give him/her %d 50-cent coin(s)",c50);} 
							}while (getData(&paid,&due)==1);	}

/*function dispense main*/
void dispense (int change,int *dollars,int *c50,int *c20,int *c10) {
/*temp store variable*/
int tmp;
int initial1=0;
int initial2=0;
int initial3=0;
c10=&initial1;
c20=&initial2;
c50=&initial3;

tmp=(change%10);

if (tmp>=5) 
	*dollars=((change/10)*10)+10;	
else if (tmp<5)	
	*dollars=(change/10)*10;	

	switch (*dollars) {
	case '10':*c10+=1;
			  break;
	case '20':*c20+=1;
			  break;
	case '30':*c10+=1;
			  *c20+=1;
			  break;
	case '40':*c20+=2;
			  break;
	case '50':*c50+=1;
			  break;
	case '60':*c10+=1;
			  *c50+=1;
			  break;
	case '70':*c20+=1;
			  *c50+=1;
			  break;
	case '80':*c10+=1;
			  *c20+=1;
			  break;
	case '90':*c20+=2;
			  *c50+=1;
			  break;
	default:;	}
	}

	/*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;}
  #8  
Old 06-Dec-2004, 10:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
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
CPP / C++ / C Code:
#include <stdio.h>

/
/*function dispense main*/
void dispense (int change,int *dollars,int *c50,int *c20,int *c10) {
/*temp store variable*/
int tmp;
int initial1=0;
int initial2=0;
int initial3=0;
c10=&initial1;
c20=&initial2;
c50=&initial3;



You are changing the local copies of c10, c20 and c50 to point to things that you will calculate. Storing values in initial1, initial2, and initial3 will not change anything in the variables c10, c20 and c30 in the main().

Try this instead:

CPP / C++ / C Code:
int tmp;

*dollars = 0;
*c50 = 0;
*c20 = 0;
*c10 = 0;

tmp=(change%10);
  printf("*c50 = %d, *c20 = %d, *c10 = %d\n", *c50, *c20, *c10);
if (tmp>=5)  {
  *dollars=((change/10)*10)+10;  
  printf ("*dollars = %d\n", *dollars);
}
else if (tmp<5)  {
  *dollars=(change/10)*10;  
  printf ("*dollars = %d\n", *dollars);
}

Then, take it from there to debug the logic.

Regards,

Dave
  #9  
Old 06-Dec-2004, 11:00
fwongmc fwongmc is offline
New Member
 
Join Date: Nov 2004
Posts: 26
fwongmc is on a distinguished road
I have to say thanks for your help.
But one thing,if I put the statment by your on my function dispense,all output will be zero.Does it related to the function been called?
  #10  
Old 06-Dec-2004, 11:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
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
I have to say thanks for your help.
But one thing,if I put the statment by your on my function dispense,all output will be zero.Does it related to the function been called?

I'm not sure what you mean. Since you are initializing them (*c50, *c20, etc.) inside the function, they will be set to 0 each time the function is called.

Now, if you are getting zeros after the function has done its stuff, you put printf statements as each calculation is performed:

CPP / C++ / C Code:
void dispense (int change,int *dollars,int *c50,int *c20,int *c10) 
{

  int tmp;

  *dollars = 0;
  *c50 = 0;
  *c20 = 0;
  *c10 = 0;

  tmp=(change%10);
  printf("tmp = %d\n", tmp);

  if (tmp>=5) 
    *dollars=((change/10)*10)+10;  
  else if (tmp<5)  
    *dollars=(change/10)*10;  

  printf ("*dollars = %d\n", *dollars);

Then follow the logic through the switch() statement. Is it doing what you need for it to do?

Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

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
Mother board problem mrkamran Computer Hardware Forum 2 07-Oct-2004 10:31
Mpeg2 SVCD disc problem mrnobody Computer Software Forum - Windows 0 13-Aug-2004 08:51
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 05:54.


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