Thread: Error in return variable.

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    Error in return variable.

    Hey all. I am new to C and C++

    I am doing a test program and it does not return the variable called input in the input2 function. any help would be great.





    Code:
    #include <iostream>
    using namespace std;
    int input2(int input);
    
    int main (int argc, char * const argv[]) {
        // insert code here...
    //int input=0;
    	int input;
    	//input=0;
    	input2(input);
    	
        
    	
    	cout<<input;
    	switch(input)
    	{
    		case 1:
    			printf("New Game");
    			break;
    		case 2:
    			printf("Load Game");
    			break;
    		case 3:
    			printf("high scores");
    			break;
    		case 4:
    			printf("Quiting!");
    			break;
    		default:
    			printf("Invalid Choice!");
    			break;
    			
    	}
    	getchar();
    
        return 0;
    }
    
    int input2(int input){
    	//int input;
    	//int test;
    	
    	std::cout << "Hello, World!\n";
    
    	printf("\n\n1. Play Game\n");
    	printf("2. Load\n");
    	printf("3. High Scores\n");
    	printf("4. Quit\n\n");
    	printf("Selection:  ");
    	cin>>input;
    	cin.ignore();
    	
    	cout<<"\n"<<input<<"\n";
    	//test==input;
    	return input;
    	
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to store the return variable somewhere, for example:
    Code:
    int input = 9;
    int root = 0;
    
    root = get_square_root(input);
    
    // now root is 3.
    Your current code uses an input parameter, but that doesn't change the variable in main. (There is a slightly more advanced technique called pass-by-reference that would do that, but that's not what you want here.) You don't need a parameter, you just need to use the return value.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    Thanks.. That worked wonderfully.

    It was so obvious. I don't know how I missed that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM