Thread: Call by Reference/Address Errors - Please Help

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Unhappy Call by Reference/Address Errors - Please Help

    Hello all,

    I'm new to the board and I'm very desperate for some help with a program I'm writing for class but I can't get rid of the errors in the call by reference functions. I've posted the code below and would be grateful for any help.

    Thanks,
    Vireyda

    Code:
    #include<stdio.h> //include standard input output functions
    
    float Cheque(float balance);
    float Deposit(float balance);
    float Print(float balance);
    
    void main()
    {
    
    float balance=0; 
    char response;
    
    printf("Welcome to the Personal Banker.\n");
    
    		printf("Please select a transaction:\n");
    		printf("Cheque	- C or c\nDeposit	- D or d\nPrint	-P or p\nQuit	-Q or q");
    		scanf("%c", response);
    		switch(response)
    		{case'c':case'C': 
    			Cheque(&balance); 
    			break;
    		case 'd': case'D':
    			Deposit(&balance);
    			break;
    		case 'p': case'P':
    			Print(&balance);
    			break;
    		case 'q': case'Q':
    			break;
    		default:
    			printf("That is an invalid selection.  Please try again. ");
    		}
    	
    }
    
    void Cheque (float &balance)
    {
    	float amount=0;
    
    	printf("Please enter the amount of the cheque. $");
    	scanf ("%f",amount);
    	
    	if (amount>balance)
    		printf ("There are insufficient funds to process this cheque.");
    	else
    		balance=-amount;
    }
    
    
    void Deposit (float &balance)
    {
    	float amount=0;
    
    	printf ("Please enter the amount of deposit. $");
    	scanf ("%f",amount);
    	balance=+amount;
    }
    
    void Print (float balance)
    {
    	printf ("The balance of your account is $%.2f.", balance);
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    In C++ you use & in the function argument list to specify a reference parameter, but you don't use it in the function call. Make your functions match and you'll have fewer errors, but be aware that your scanf calls are incorrect as well. You do need to add the & there. Main returns int, and lastly, =- and =+ will not work as you expect. You want += and -=:
    Code:
    #include<stdio.h> //include standard input output functions
    
    void Cheque(float& balance);
    void Deposit(float& balance);
    void Print(float balance);
    
    int main()
    {
    float balance=0; 
    char response;
    
    printf("Welcome to the Personal Banker.\n");
    
    		printf("Please select a transaction:\n");
    		printf("Cheque	- C or c\nDeposit	- D or d\nPrint	-P or p\nQuit	-Q or q");
    		scanf("%c", &response);
    		switch(response)
    		{case'c':case'C': 
    			Cheque(balance); 
    			break;
    		case 'd': case'D':
    			Deposit(balance);
    			break;
    		case 'p': case'P':
    			Print(balance);
    			break;
    		case 'q': case'Q':
    			break;
    		default:
    			printf("That is an invalid selection.  Please try again. ");
    		}
    	
    }
    
    void Cheque (float &balance)
    {
    	float amount=0;
    
    	printf("Please enter the amount of the cheque. $");
    	scanf ("%f",&amount);
    	
    	if (amount>balance)
    		printf ("There are insufficient funds to process this cheque.");
    	else
    		balance-=amount;
    }
    
    
    void Deposit (float &balance)
    {
    	float amount=0;
    
    	printf ("Please enter the amount of deposit. $");
    	scanf ("%f",&amount);
    	balance+=amount;
    }
    
    void Print (float balance)
    {
    	printf ("The balance of your account is $%.2f.", balance);
    }
    My best code is written with the delete key.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I think you'll need to make a change to your function prototypes as well.

    which...Prelude already said...doh

  4. #4
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    I've made all the corrections you suggested, but I'm still getting errors (added below) that I don't understand at all and have no idea how to fix. Do you know what this might mean and how to correct it?

    Thanks again,
    Vireyda
    --------------------Configuration: BankAccountcpp - Win32 Debug--------------------
    Compiling...
    BankAccountcpp.cpp
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(47) : error C2556: 'void __cdecl Cheque(float &)' : overloaded function differs only by return type from 'float __cdecl Cheque(float &)'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(13) : see declaration of 'Cheque'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(47) : error C2371: 'Cheque' : redefinition; different basic types
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(13) : see declaration of 'Cheque'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(61) : error C2556: 'void __cdecl Deposit(float &)' : overloaded function differs only by return type from 'float __cdecl Deposit(float &)'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(14) : see declaration of 'Deposit'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(61) : error C2371: 'Deposit' : redefinition; different basic types
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(14) : see declaration of 'Deposit'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(70) : error C2556: 'void __cdecl Print(float)' : overloaded function differs only by return type from 'float __cdecl Print(float)'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(15) : see declaration of 'Print'
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(70) : error C2371: 'Print' : redefinition; different basic types
    D:\School Stuff\Cosc 102h\Assignment 3\BankAccountcpp.cpp(15) : see declaration of 'Print'
    Error executing cl.exe.

    BankAccountcpp.exe - 6 error(s), 0 warning(s)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've made all the corrections you suggested
    No you didn't. I suggested making your function declarations and definitions match. Your errors clearly show that you neglected to make the necessary changes.
    My best code is written with the delete key.

  6. #6
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    I just want to clarify that the function declarations are the prototypes and the definitions are the functions that are called and contain code within them? And the calls are within the function main? I want to make sure that I'm matching up the right things.

    Edit: I'm also only getting the errors when I try to build the program, not when I Compile - I get no errors when I compile

    Thanks,
    Vireyda
    Last edited by Vireyda; 03-14-2004 at 06:20 PM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I just want to clarify that the function declarations are the prototypes and the definitions are the functions that are called and contain code within them?
    This is a subtle subject. For now, that explanation will do.
    My best code is written with the delete key.

  8. #8
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    Okay, I figured out where I was missing changes. Thanks again for all the help. You've just ended two days of frustration.

    Vireyda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  2. No call to execv
    By protocol78 in forum C Programming
    Replies: 4
    Last Post: 05-08-2007, 11:43 AM
  3. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  4. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  5. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 07:45 PM