GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Oct-2005, 19:29
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Need help understand function prototypes!


hi, i take programming at school and i need some help understanding functions better becuase i have an assigntment due in 2 weeks...

so far my main code looks like this

CPP / C++ / C Code:
main() {
   int cheques = 0, bounced = 0, deposits = 0, withdrawals = 0;
   double balance, serviceCharge;
   char trans;
   getBalance(&balance);
   while ((trans = getTrans()) != 'q') {
      if (trans == 'c')
         updateCheque(&balance, &serviceCharge, &cheques, &bounced);
      else if (trans == 'd')
         updateDeposit(&balance, &serviceCharge, &deposits);
      else if (trans == 'w')
         updateWithdrawal(&balance, &serviceCharge, &withdrawals);
   }
   updateServiceCharge(balance, serviceCharge, cheques, bounced, deposits, withdrawals);
}


my problem is i need to use functions to add and subtract values from one another and keep a running total of how many "deposits, withdrawals, and cheques" a user uses...

thanks alot for any help...

Brendan.
Last edited by JdS : 20-Oct-2005 at 19:31. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 20-Oct-2005, 19:41
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Need help understand function prototypes!


Hi Blstretch

Welcome to the GID Forums.

The function declaration should be of this type:
CPP / C++ / C Code:
return-type function-name(parameter list)
{
declarations and statements;
return expression;
}
The return-type specifies the type of data that the function returns. The parameter list is a comma-separated list of variable names and their associated types that receive the values of the arguments when the function is called . All function parameters must be declared individually, each including both the type and name. That is, the parameter declaration list for a function takes this general form:
funtion_name(type varname1, type varname2, . . . , type varnameN);

Here is the information about pass by value and pass by reference:

C always passes arguments `by value' by default: a copy of the value of each argument is passed to the function; the function cannot modify the actual argument passed to it:

CPP / C++ / C Code:
void foo(int j)
 {
  j = 0;  /*  modifies the copy of the argument received by the function  */
}

int main(void) {
  int k=10;
  foo(k);
  /*  k still equals 10  */
}
If you do want a function to modify its argument you can obtain the desired effect using pointer arguments instead:
CPP / C++ / C Code:
void foo(int *j) {
  *j = 0;               //note how the variable is used here.
}

int main(void) {
  int k=10;
  foo(&k);
  /*  k now equals 0  */
}
This is also known as `pass by reference'.

You can learn more about functions in this link:
Using Functions in C

In your case you have to use the pass by reference as you want to update the values of balance, cheques etc.
So for using this statement,
CPP / C++ / C Code:
         updateCheque(&balance, &serviceCharge, &cheques, &bounced);

Use the function like this:
CPP / C++ / C Code:
//this is the prototype for the function. You have to prototype the function to inform the compiler about the function declaration.
void updateCheque( double* balance, double* serviceCharge, int* cheques, int* bounced);


int main()
{
//your code goes here
}

void updateCheque( double* balance, double* serviceCharge, int* cheques, int* bounced)
{
//your code for the function goes here.
}

Similarly, you can define the other functions updateDeposit and updateWithdrawal.

Please read the Guidelines before posting again.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 20-Oct-2005, 20:00
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Re: Need help understand function prototypes!


thanks alot...that clears things up a bit
  #4  
Old 20-Oct-2005, 21:20
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Re: Need help understand function prototypes!


umm...just a quick thing, when i tried to compile my program, the first part of it anyway, it had like 100 errors in it like


Code:
operation between types char* and int is not allowed operation between types double and double* is not allowed

mostly those, i understood some of the other ones, and fixed a few, but i do not understand what the debugger means by this.

and btw i know i didnt mention before but this is simple C prgramming, not C++ or anything...and i am using crimson editor to write my code as its got syntax hi-liting.

any help would be great, thanks.
  #5  
Old 20-Oct-2005, 21:36
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
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

Re: Need help understand function prototypes!


Since you didn't post the code that gives the errors, all we can do is guess. If you read the guidelines as requested you'll know what to tell us and how.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #6  
Old 20-Oct-2005, 21:42
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Re: Need help understand function prototypes!


ohh sorry, i thought i posted, my mistake i must have screwed something up....

CPP / C++ / C Code:
void getBalance( double *balance); 
void getTrans(char *trans);
void updateCheque( double *balance, double *serviceCharge, int *cheques, int *bounced);
void updateDeposit( double *balance, double *serviceCharge, int *cheques, int *bounced);
void updateWithdrawal( double *balance, double *serviceCharge, int *cheques, int *bounced);

int main() {
   int cheques = 0, bounced = 0, deposits = 0, withdrawals = 0;
   double balance, serviceCharge;
   char trans;
   getBalance(&balance);
   while ((trans = getTrans()) != 'q') {
      if (trans == 'c')
         updateCheque(&balance, &serviceCharge, &cheques, &bounced);
      else if (trans == 'd')
         updateDeposit(&balance, &serviceCharge, &deposits);
      else if (trans == 'w')
         updateWithdrawal(&balance, &serviceCharge, &withdrawals);
   }
   updateServiceCharge(balance, serviceCharge, cheques, bounced, deposits, withdrawals);
}

void getBalance(double *balance)
{
	printf("Welcome to the Cheque Book Balancer\n");
	printf("\n");
	printf("Please enter the current balance: ");
	scanf("%lf", &balance);
	return balance;
}

void getTrans(char *trans)
{
	printf("*******************************************************\n");
	if (balance < 1000)
	printf("Balance forward                                 %.2lf\n", balance);
	else
	printf("Balance forward                                  %.2lf\n", balance);
	printf("*******************************************************\n");
	
	printf("Transaction Menu\n================\n");
	printf("c - Cheque\nd - Deposit\nw - Withdrawal\nq - Quit\n");
	printf("================\n);
	
	printf("Enter selection(c, d, w, or q): ");
	scanf("%a", &trans)
	while (trans != 'c' && trans != 'd' && trans != 'w' && trans != 'q')
	{
		printf("Invalid selection, enter 'c', 'd', 'w', or 'q': ");
		scanf("%a", &trans)
	}
	return trans; 
		
}



void updateCheque( double *balance, double *serviceCharge, int *cheques, int *bounced)
{
	double* cheque;
	int charge, bounce;
	printf("Enter amount of cheque: ");
	scanf("%lf", &cheque);
	
	printf("Transaction       Debit       Credit            Balance\n*******************************************************\n");
	printf("Cheque           %.2lf                          %.2lf\n", cheque, balance);
	printf("*******************************************************");
	if (cheque > balance){
		printf("Cheque has bounced, transaction cancelled");
		bounced = bounced + 1;
		return bounced;
	}
	
	balance = balance - cheque;
	cheques = cheques + 1;
	return balance;
	return cheques;
	
}
there it is, it gives me those errors..

sorry about the previous post.

thanks alot
  #7  
Old 21-Oct-2005, 06:05
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Need help understand function prototypes!


Hi BlStretch,

Here are some tips:

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

Please put semicolon at the end of each statement.
(your program misses some. Can you find where?)

You can easily find errors by looking at the code itself.
for example look at the line:
CPP / C++ / C Code:
printf("================\n);
Did you find the error?
You missed out a double quote.

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

When you use a function, make sure that you use the function in the way it is defined.
for example consider this line: You used no parameters in getTrans() function.
CPP / C++ / C Code:
   while ((trans = getTrans()) != 'q') {
But look at the definition. You gave a parameter char *trans which is unnecessary. So modify your getTrans like this:
CPP / C++ / C Code:
char getTrans( );   //no parameters passed. and return type is char

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

Next,
You can return a value only if you specify a return type. So if you want to return trans, define the function like this:
CPP / C++ / C Code:
char getTrans();  //The function return a character

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

If you want to use the getBalance statement like this,
CPP / C++ / C Code:
   getBalance(&balance);

You should define the getBalance function like this:
CPP / C++ / C Code:
void getBalance(double* balance); 

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

In your function trans, you used the balance variable.
Like this:
CPP / C++ / C Code:
if (balance < 1000)
You cannot use any variable declared in the main function in any other function unless it is passed as an parameter.
So if you still want to use balance variable, you can modify the function like this:
CPP / C++ / C Code:
char getTrans(double balance);
so that you can use balance in the function.

And dont pass every parameter as reference.
If you want only to use the variable balance and you dont need to vary the variable in a function, use the simple pass by value as in the previous getTrans function definition.

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

Dont use the parameters of the updateCheque function to define the other functions updateDeposit and updateWithdrawal.
Declare the function the way you want the function to use.
If you want to use updateDeposit like this:
CPP / C++ / C Code:
updateDeposit(&balance, &serviceCharge, &deposits);
then define the function like this:
CPP / C++ / C Code:
void updateDeposit(double *balance, double *serviceCharge, int *deposits);

Similarly you modify the other function updateWithdrawal according to the way you want to use them in the main function.

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

You can return only one value from any function using the return statement.
How would you return many values?
That is why we use pass by reference.
So if you modify the variables passed by reference, they are modified in the main function itself.

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

Rememer this again:
Quote:
If you do want a function to modify its argument you can obtain the desired effect using pointer arguments instead:

CPP / C++ / C Code:
void foo(int *j) {
  *j = 0;               //note how the variable is used here.
}

int main(void) {
  int k=10;
  foo(&k);
  /*  k now equals 0  */
}
This is also known as `pass by reference'

Note how the values passed are used inside the program.
Can you find the errors in your functions based on this?

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

I hope you understand what i have typed here.
If you have any doubts or clarifications, please post again.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
Last edited by Paramesh : 21-Oct-2005 at 07:35.
  #8  
Old 21-Oct-2005, 08:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
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

Re: Need help understand function prototypes!


In addition to what Paramesh mentioned, let's look at your getTrans() function in detail. The prototype:
CPP / C++ / C Code:
void getTrans(char *trans);
it's use in main():
CPP / C++ / C Code:
while ((trans = getTrans()) != 'q') {
And the function:
CPP / C++ / C Code:
void getTrans(char *trans)
{
	printf("*******************************************************\n");
	if (balance < 1000)
	printf("Balance forward                                 %.2lf\n", balance);
	else
	printf("Balance forward                                  %.2lf\n", balance);
	printf("*******************************************************\n");
	
	printf("Transaction Menu\n================\n");
	printf("c - Cheque\nd - Deposit\nw - Withdrawal\nq - Quit\n");
	printf("================\n);
	
	printf("Enter selection(c, d, w, or q): ");
	scanf("%a", &trans)
	while (trans != 'c' && trans != 'd' && trans != 'w' && trans != 'q')
	{
		printf("Invalid selection, enter 'c', 'd', 'w', or 'q': ");
		scanf("%a", &trans)
	}
	return trans; 
		
}

The prototype states it's a void function with a parameter that is passed back. This means the function does not return a value that can immediately be used as you did in main(). The function must be defined as
CPP / C++ / C Code:
char gettrans()
instead. This is a better definition anyway. It's better to return the value than pass a pointer.

In the function, as Paramesh mentioned you cannot use balance because it's not in the function. This is a bad place to display it anyway because it's not necessary in this function. Move the print lines into main(). Also, if you want a specific format for the number, specify it. Instead of having two printf's that differ by a space, define the printf statement as:
CPP / C++ / C Code:
printf("Balance forward                 %10.2lf\n", balance);
Now no matter how small or large the value is it will line up.

The menu is defined as
CPP / C++ / C Code:
printf("c - Cheque\nd - Deposit\nw - Withdrawal\nq - Quit\n");
This is a personal preference, but I find these type of displays easier to read and line up when defined like:
CPP / C++ / C Code:
printf("c - Cheque\n");
printf("d - Deposit\n");
printf("w - Withdrawal\n");
printf("q - Quit\n");
This makes it easier to
1) read
2) adjust for display
3) add new commands
4) remove old commands


You defined the variable trans as a pointer but use it as a character. Since it's a pointer, you need to reference it as *trans.


CPP / C++ / C Code:
scanf("%a", &trans)
1) what does %a mean? Did you mean %c perhaps?
2) trans is a pointer, not a character
3) you have no ';'
4) read this. 'Nuff said.


CPP / C++ / C Code:
return trans; 
You defined the function as a void. You cannot return a value. Another reason to change the function definition as described above.


So, rewrite your function as:
The prototype:
CPP / C++ / C Code:
char getTrans();
it's use in main():
CPP / C++ / C Code:
printf("Balance forward                 %10.2lf\n", balance);
while ((trans = getTrans()) != 'q') {
    ...
    printf("Balance forward                 %10.2lf\n", balance);
}
And the function:
CPP / C++ / C Code:
char getTrans()
{
    char trans, x;

    printf("Transaction Menu\n");
    printf("================\n");
    printf("c - Cheque\n");
    printf("d - Deposit\n");
    printf("w - Withdrawal\n");
    printf("q - Quit\n");
    printf("================\n);
    
    printf("Enter selection(c, d, w, or q): ");
    trans = getchar(); 
    while (x != '\n') x = getchar();   // clear the input buffer
    while (trans != 'c' && trans != 'd' && trans != 'w' && trans != 'q')
    {
        printf("Invalid selection, enter 'c', 'd', 'w', or 'q': ");
        trans = getchar(); 
        while (x != '\n') x = getchar(); 
    }
    return trans; 	
}
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #9  
Old 21-Oct-2005, 14:12
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Re: Need help understand function prototypes!


thanks alot guys, i appreciate the time you spent helping me with the coding...

i tried to wrap my head around the whole prototype function thing really quickly, and i think it didnt work very well. as well, i am terrible at not placing my ; after statements, its just one of those things i forget often, im trying to find a program to work with my crimson editor that will underline bad coding so i can correct it better. Also thanks alot for explaining functions as i am trying to stay ahead of my class by reading a little ahead in the book and trying to simplfy my code by using these new ways of programming.

thanks for yout time,
brendan
  #10  
Old 21-Oct-2005, 22:39
Blstretch Blstretch is offline
New Member
 
Join Date: Oct 2005
Posts: 21
Blstretch is on a distinguished road

Re: Need help understand function prototypes!


I know replying to myself in my own trhead mite not be correct, but i just need to ask, is that
CPP / C++ / C Code:
 getchar() 
function in c or c++? cause its confusing me a lilttle bit..

i read the part 5 and 6 of the tutorials you made Walt, and they make sense, i just dont quite get the functioning of a while statement without braces.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

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

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:33.


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