Having problem with illegal function definitions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirayamato1992
    New Member
    • Jun 2010
    • 3

    Having problem with illegal function definitions

    i wrote some codes for my assignments.
    then it came out several problems involving illegal local function definition.

    Code:
    #include <iostream>
    using namespace std;
    
    void system_module();
    void card_module();
    int transaction_module();
    void traffic_control(int);
    void withdraw_module();
    void deposit_module();
    void transfer_module();
    void inquiry_module();
    void receipt_module();
    void repeat_module();
    
    int saving = 10000;
    int current = 10000;
    
    int main()
    {
    	int trans;						/*trans stands for transaction type*/
    	
    	trans = transaction_module();
    	traffic_control(trans);
    	return 0;
    }
    
    void system_module()
    {
    	char login;
    
    	while (login != 'S' && login != 'X')
    	{
    		cout << "Please type S to start or X to Exit : ";
    		cin >> login;	
    	}
    
    	if (login == 'S')
    	{	
    		cout<<"Welcome to Southern Bank SDN. BHD.  How may I help you today ?\n";
    	}
    	else if (login == 'X')
    	{
    		cout << "System is shutting down. \n";
    	}
    }
    
    void card_module()
    {
    	char card; 
    	const int pw=920117;
    	int x = 3, pin;
    start:
    	while (card != 'I' && card != 'C')
    	{
    		cout << "Press I to insert card or C to cancel : ";
    		cin >> card;
    	}
    	
    	if (card == 'I')
    	{
    		cout << "Please insert PIN number : ";
    		cin >> pin;
    
    		if (pin != pw && card != 'C')
    		{
    			while (x > 0 )
    			{
    				cout << "Wrong PIN number entered. Please try again. " << "[" << x << " more chance(s)]\n\a";
    				x = x - 1;
    				cout << "Please reinsert PIN number : ";
    				cin >> pin;
    			}
    		
    				cout << "Your card has been barred from further use. Please ask the bank for assistance.\n";
    		}
    		else if (pin == pw)
    		{
    			cout << "PIN number aunthenticated. \n";
    		}
    	}
    	else if (card == 'C')
    	{
    		cout << "Session canceled. Please take your card. Have a nice day ! \n";
    		goto start; /*To be continued*/
    	}
    }
    
    int transaction_module()
    {
    	int type;
    
    	cout << "1. Cash Withdrawal \t\n";
    	cout << "2. Cash Deposit \t\n";
    	cout << "3. Money Transfer \t\n";
    	cout << "4. Balance Inquiry \t\n";
    	cout << "5. Abort \t\n";
    	cout << " Please select transaction : ";
    	cin >> type;
    
    	return type;
    }
    
    void traffic_control(int trans)
    {
    	switch (trans)
    	{
    	case 1:{goto withdraw;break;}
    	case 2:{goto deposits;break;}
    	case 3:{goto transfer;break;}
    	case 4:{goto inquiry;break;}
    	}
    
    
    		void withdraw_module();
    		{
    			int acc, draw;
    		withdraw:
    			while (acc != 1 && acc != 2 && acc != 3)
    			{	
    				cout << "1. Saving Account \t\n";
    				cout << "2. Current Account \t\n";
    				cout << "3. Cancel \t\n";
    				cout << "Select your account to withdraw money from : ";
    				cin >> acc;
    			}
    
    			{
    				while (acc == 3)
    				{
    					cout << " Thank you for using our services. \n";break;			/*need some modification*/
    				}
    
    				if (acc == 1)
    				{
    					while (saving >= 0)
    					{
    		saving:
    						cout << " Saving account currently has RM " << saving << endl;
    						cout << " Enter the amount you want to withdraw : RM ";
    						cin >> draw;
    				
    						if ( saving >= draw)		
    						{
    							saving = saving - draw;break;
    						}
    
    						else if (saving < draw)
    						{
    							cout << "Insufficient fund. Please reenter the amount. \n";
    							goto saving;
    						}
    					}
    				}	
    
    				else if (acc == 2)
    				{
    					while (current > 0)
    					{
    		current:
    						cout << "Current account currently has RN " << current << endl;
    						cout << "Enter the amount you want to withdraw : RM";
    						cin >> draw;
    
    						if ( current > draw)		
    						{
    							current = current - draw;break;
    						}
    
    						else
    						{
    							cout << "Insufficient fund. Please reenter the amount. \n";
    							goto current;
    						}
    					}
    				}
    				goto receipt;
    			}
    		}
    
    		void deposit_module()
    		{
    			int acc, deposit;
    		deposits:
    			while (acc != 1 && acc != 2 && acc != 3)
    			{	
    				cout << "1. Saving Account \t\n";
    				cout << "2. Current Account \t\n";
    				cout << "3. Cancel \t\n";
    				cout << "Select your account to deposit money to : ";
    				cin >> acc;
    			}
    
    			{
    				while (acc == 3)
    				{
    					cout << " Thank you for using our services. \n";break;			
    				}
    
    				if ( acc == 1)
    				{
    					cout << " Saving account currently has RM " << saving << endl;
    					cout << " Enter the amount you want to deposit : RM ";
    					cin >> deposit;
    					saving = saving + deposit;
    				}	
    
    				else if (acc == 2)
    				{
    					cout << "Current account currently has RN " << current << endl;
    					cout << "Enter the amount you want to deposit : RM";
    					cin >> deposit;
    					current = current + deposit;
    				}
    				goto receipt;
    			}
    		}
    
    		void transfer_module()
    		{
    			int acc, acc2, amount;
    			acc = 0;
    			acc2 = 0;
    			amount = 0;
    		transfer:
    			{
    				while (acc != 1 && acc != 2 && acc != 3)
    				{	
    					cout << "1. Saving Account \t\n";
    					cout << "2. Current Account \t\n";
    					cout << "3. Cancel \t\n";
    					cout << "Select your account to transfer money from : ";
    					cin >> acc;
    			
    					switch (acc)
    					{
    					case 1 : {saving;break;}
    					case 2 : {current;break;}
    					case 3 : {cout << " Thank you for using our services. \n";break;}
    					}
    				
    						cout << "Enter the amount you want to transfer : RM";
    						cin >> amount;
    						acc = acc - amount;break;
    				}
    					
    				while (acc2 != 1 && acc2 != 2 && acc2 != 3)
    				{
    					cout << "1. Saving Account \t\n";
    					cout << "2. Current Account \t\n";
    					cout << "3. Cancel \t\n";
    					cout << "Select your account to transfer money to : ";
    					cin >> acc2;
    			
    					switch (acc2)
    					{
    					case 1 : {saving;break;}
    					case 2 : {current;break;}
    					case 3 : {cout << " Thank you for using our services. \n";goto start;break;}
    					}
    					acc = acc2 + amount;break;
    				}
    			}
    			goto receipt;
    		}
    
    		void inquiry_module()
    		{
    			int acc, acc2, amount;
    			acc = 0;
    			acc2 = 0;
    			amount = 0;
    		inquiry:
    			{
    				while (acc != 1 && acc != 2 && acc != 3)
    				{	
    					cout << "1. Saving Account \t\n";
    					cout << "2. Current Account \t\n";
    					cout << "3. Cancel \t\n";
    					cout << "Select your account for inquiry : ";
    					cin >> acc;
    			
    					switch (acc)
    					{
    					case 1 : {saving;break;}
    					case 2 : {current;break;}
    					case 3 : {cout << " Thank you for using our services. \n";break;}
    					}
    					
    					if (acc == 1)
    					{
    						cout << "Current balance of this account : RM "<<saving<<endl;break;
    					}
    						
    					else if (acc == 2)
    					{
    						cout << "Current balance of this account : RM "<<current<<endl;break;
    					}
    
    					else if (acc == 3)
    					{
    						cout << " Transaction canceled. Thank you for using us. \n";break;
    						goto start;
    					}
    				}
    			}
    		}
    }
    
    void receipt_module()
    {
    	char receipt;
    
    	cout << " Do you want to print receipt ? Y or N : ";
    	cin >> receipt;
    
    	if (receipt == 'Y')
    	{
    		cout << " Receipt printed. Please collect it. \n";
    	}
    
    	else if (receipt == 'N')
    	{
    		cout << "Thank you. Come back soon. \n";
    	}
    }
    
    void repeat_module()
    {
    	char con;
    
    	cout << "Do you still want to continue your session ? Y or N : ";
    	cin >> con;
    
    	if (con == 'Y')
    	{
    		goto start;		/
    	}
    
    	else if (con == 'N')
    	{
    		cout << "Thank you for banking with us. \n";
    	}
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can't define functions inside other functions, you have put several functions inside traffic_control which need to be moved.

    Comment

    • kirayamato1992
      New Member
      • Jun 2010
      • 3

      #3
      thx. I've tried out.
      but ended up with even more errors.
      stating undefined labels , like inquiry, deposit etc
      mind explaining for me ?
      i'm still new to all this

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        To explain it I would need to see the code that is causing the errors and the errors (copy and pasted) as well.

        Based on what you have written in post #3 I can say that you have an error in your code somewhere.

        BTW did you remove the ; from the end of line 114?

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          A little observation about the code
          in void withdraw_module () lots of goto statement present. which is real scary. This is always possible to write a code without using goto statement. remove all of your goto statements.
          about
          Code:
          goto receipt;
          in your entire code there is no label name
          receipt
          fix them. i think there is more goto statement without label

          Best Regards
          Johny

          Comment

          • kirayamato1992
            New Member
            • Jun 2010
            • 3

            #6
            Originally posted by johny10151981
            A little observation about the code
            in void withdraw_module () lots of goto statement present. which is real scary. This is always possible to write a code without using goto statement. remove all of your goto statements.
            about
            Code:
            goto receipt;
            in your entire code there is no label name
            receipt
            fix them. i think there is more goto statement without label

            Best Regards
            Johny
            ok. thx for pointing that out. i'll try to make some modification about it

            Comment

            Working...