1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Calling Functions in C++

Discussion in 'Software Development' started by hyler, Sep 26, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    In BASIC, I remember a way in programs with multiple functions to be able to start one function from a command line within another, I believe it was called the "call" command. Is there anything like this in C++? I have a program that could use it. IE:

    #include "stdafx.h"
    #include <iostream.h>

    int main()
    {
    WHAT COMMAND WOULD GO HERE TO START THE COMMANDS WITHIN beginprog?
    }

    bool beginprog()
    {
    int tries =1; //define integer variable
    while (tries<4) { //start while loop

    cout<<"Do you want to procede (y or n) ?\n"; //write question
    char answer=0; //define character variavle
    cin>>answer; //asks user to input answer

    switch (answer)
    {
    case 'y':
    return true;
    case 'n':
    return false;
    default:
    cout<<"Sorry, I don't understand that.\n";
    tries=tries+1;
    }
    }
    cout<<"I'll take that for a no.\n";
    return false;
    }
     
  2. Guy

    Guy

    Joined:
    Feb 19, 1999
    Messages:
    260
    Code:
    int main()
    {
       bool status = beginprog();
       return (int)status;
    }
    or simply...
    Code:
    int main()
    {
       beginprog();
       return 0;
    }
     
  3. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    Code:
    OK, I've organized it with functions but there are quite a few things wrong with it and I can't seem to catch them.
    
    #include <iostream.h>
    
    	int main()
    	{
    
    	cout<<"*************************************************************************";	
    	cout<<"*******     **     ** ******     ** *** ** ****     **     **     **     *******";
    	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** *** *******";
    	cout<<"**   ** ******     ** ****** ****** *** ** ****     **** **** *** **     **   **";
    	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** ** ********";
    	cout<<"*******     ** *** **     **     **     **   ** *** **** ****     ** *** *******";
    	cout<<"**************************************************************************";	
    	cout<<"**********Enter 1 to Add, 2 to Subtract, 3 to Multiply, or 4 to Divide**********";
    	cout<<endl;
    start:
    	   		cout<<"Choose the number of your operation then press Enter: ";
    			int choice;
    			cin>>choice;
    			if (choice == 1)
    				bool status = addition();
    					return (int)status;
    						goto start;
    			if (choice == 2)
    				bool status = subtraction();
    					return (int)status;	
    						goto start;
    			if (choice == 3)
    				bool status = multiplication();
    					return (int)status;	
    						goto start;
    			if (choice == 4)
    				bool status = division();
    					 return (int)status;	
    						goto start;
    			
    			if (choice >4)
    				goto start;
    	}
    			bool adding()
    		{
    			cout<<"Addition-";
    				cout<<"Enter first number: ";
    			int first;
    				cin>>first;
    					cout<<"Enter second number: ";
    			int second;
    				cin>>second;
    			cout<<"Answer: ";
    				cout<<first + second;
    					cout<< endl;
    		}
    		bool subtraction()
    		{
    			cout<<"Subtraction-";
    				cout<<"Enter first number: ";
    			int first1;
    				cin>>first1;
    					cout<<"Enter second number: ";
    			int second1;
    				cin>>second1;
    					cout<<"Answer: ";
    						cout<<first1 - second1 ;
    							cout<< endl;
    			}
    			bool multiplication()
    			{
    			cout<<"Multiplication-";
    				cout<<"Enter first number: ";
    			int first2;
    				cin>>first2;
    					cout<<"Enter second number: ";
    			int second2;
    				cin>>second2;
    					cout<<"Answer: ";
    						cout<<first2 * second2 ;
    							cout<< endl;
    		}
    		bool division()
    		{
    			cout<<"Division-";
    				cout<<"Enter first number: ";
    			int first3;
    				cin>>first3;
    					cout<<"Enter second number: ";
    			int second3;
    				cin>>second3;
    					cout<<"Answer: ";
    						cout<<first3 / second3;
    							cout<< endl;		 
    		}			
    
    
    Errors:
    --------------------Configuration: CALC - Win32 Debug--------------------
    Compiling...
    calc.cpp
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(20) : error C2065: 'addition' : undeclared identifier
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(20) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(21) : error C2065: 'status' : undeclared identifier
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(24) : error C2065: 'subtraction' : undeclared identifier
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(24) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(28) : error C2065: 'multiplication' : undeclared identifier
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(28) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(32) : error C2065: 'division' : undeclared identifier
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(32) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(53) : error C2373: 'subtraction' : redefinition; different type modifiers
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(66) : error C2373: 'multiplication' : redefinition; different type modifiers
    H:\COMP PROGRAMMING\C++ CHAPTERS\CALC\calc.cpp(79) : error C2373: 'division' : redefinition; different type modifiers
    Error executing cl.exe.
    
    CALC.exe - 8 error(s), 4 warning(s)
     
  4. Guy

    Guy

    Joined:
    Feb 19, 1999
    Messages:
    260
    1. if you want to write your functions after main() you have to prototype them so the main function knows they exist:
    Code:
    bool addition();
    
    int main() {
    ....
    }
    
    bool addition() {
    ...
    }
    2. You use adding() for your function, but addition() within main

    3. You must have everything that you want to happen after a successful if to be in braces:
    Code:
    if(choice == 1)
    {
       bool status = divide();
       return (int)status;
    }
    4. You do not need the goto start; statements after the returns since the program will never get to them.

    5. You are not returning a bool value from your functions


    Hope this helps.
     
  5. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    C++ Is annoying. All I had to do to get rid of over half of my errors and warnings was to take the functions other than main and put them on top of the code.
     
  6. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    All right, now that it works, it's just a matter of getting it to work the way I want it to. Here is my newly revised code, now how would I go about returning to the Int Main function once one of my Bool functions is done? It just shuts down after they finish.

    Code:
    #include <iostream.h>
    
    			bool addition()
    		{
    			cout<<"Addition-";
    				cout<<"Enter first number: ";
    			int	first;
    				cin>>first;
    					cout<<"Enter second number: ";
    			int second;
    				cin>>second;
    			cout<<"Answer: ";
    				cout<<first + second;
    					cout<< endl;
    		return 0;
    		}
    		bool subtraction()
    		{
    			cout<<"Subtraction-";
    				cout<<"Enter first number: ";
    			int first1;
    				cin>>first1;
    					cout<<"Enter second number: ";
    			int second1;
    				cin>>second1;
    					cout<<"Answer: ";
    						cout<<first1 - second1 ;
    							cout<< endl;
    		return 0;	
    		}
    			bool multiplication()
    			{
    			cout<<"Multiplication-";
    				cout<<"Enter first number: ";
    			int first2;
    				cin>>first2;
    					cout<<"Enter second number: ";
    			int second2;
    				cin>>second2;
    					cout<<"Answer: ";
    						cout<<first2 * second2 ;
    							cout<< endl;
    		return 0;
    			}
    		bool division()
    		{
    			cout<<"Division-";
    				cout<<"Enter first number: ";
    			int first3;
    				cin>>first3;
    					cout<<"Enter second number: ";
    			int second3;
    				cin>>second3;
    					cout<<"Answer: ";
    						cout<<first3 / second3;
    							cout<< endl;		 
    		return 0;
    		
    		}			
    	int main()
    	{
    
    	cout<<"********************************************************************************";	
    	cout<<"*******     **     ** ******     ** *** ** ****     **     **     **     *******";
    	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** *** *******";
    	cout<<"**   ** ******     ** ****** ****** *** ** ****     **** **** *** **     **   **";
    	cout<<"******* ****** *** ** ****** ****** *** ** **** *** **** **** *** ** ** ********";
    	cout<<"*******     ** *** **     **     **     **   ** *** **** ****     ** *** *******";
    	cout<<"********************************************************************************";	
    	cout<<"**********Enter 1 to Add, 2 to Subtract, 3 to Multiply, or 4 to Divide**********";
    	cout<<endl;
    start:
    	   		cout<<"Choose the number of your operation then press Enter: ";
    			int choice;
    			cin>>choice;
    			if (choice == 1)
    			{
    				bool status = addition();
    					return (int)status;
    						goto start;
    						}
    			if (choice == 2)
    			{
    				bool status = subtraction();
    					return (int)status;	
    						goto start;
    			}
    			if (choice == 3)
    			{
    				bool status = multiplication();
    					return (int)status;	
    						goto start;
    			}
    			if (choice == 4)
    			{
    				bool status = division();
    					 return (int)status;	
    						goto start;
    			}
    			if (choice >4)
    			{
    				goto start;
    			}		
    	}
    
     
  7. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    bump
    bump bump bump bump
    bump bump
     
  8. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    *ACK* You have goto's in your program...!!!
     
  9. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    The reason it ends is because of this:

    PHP:
    return (int)status;
    Using a return in your main ends the program.

    If you want it to continue, you probably want to output status to the screen (cout) and then goto start so another calculation can be performed...
     
  10. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    No I don't, they were just in the first version of my code as testing purposes. If you look at the code I have recently posted, there is not one GOTO in the program. No if only I could write a GOTO type command that would let you go from one function to another.
     
  11. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    You do have goto's...

    Code:
    if (choice == 4) {
      bool status = division();
      return (int)status;	
      goto start;
    }
    
     
  12. hyler

    hyler Thread Starter

    Joined:
    Jul 30, 2001
    Messages:
    126
    Sorry, my mistake. I had posted that before I replaced the GOTO commands with loops. I could have swore I posted it after. Oh well.
     
  13. Gibble

    Gibble

    Joined:
    Oct 9, 2001
    Messages:
    27,087
    Then post the new stuff.

    But either way, a return statement in your main function ends the program :)
     
  14. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Similar Threads - Calling Functions
  1. dabeartn
    Replies:
    6
    Views:
    562
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/167598

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice