Thread: Why am I getting warnings that function "doesn't always return a value" ?

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    2

    Question Why am I getting warnings that function "doesn't always return a value" ?

    I have been working on this program that is a timed mathematical quiz and have everything completed. The problem is I keep getting warnings when I compile that says 1: 'sign' : not all control paths return a value and 2: 'equation' : not all control paths return a value. Can anyone help me see what the problem is?

    Code:
     
    #include<iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include<cstdlib>
    
    #include<ctime>
    
    int num();                                // function prototype 
    char sign();                              // function prototype
    int equation( int a, char c, int b );     // function prototype
    
    int main()
    {
    	srand( time( 0 ) );   // This time function returns the current 
    	                      // "calendar time" in seconds.  This value is
    	                      // converted to an unsigned integer and used as 
    	                      // the seed to the random number generator.
    
    	int numcorrect = 0;  
    	int stop = time( 0 ) + 30;
    	int x, y, answer, correctanswer;
    	char z;
    
    	while ( time( 0 ) < stop )
    	{	
    		x = num(), y = num();    // Assignment of x and y to the num function
    		z = sign();
    
    		cout << "What is " << x << " " << z << " " 
    			 << y << endl;       // Prompt to ask the arithmetic question
    		cin >> answer;           // Input answer
    
    		correctanswer = equation( x, z, y ); // Assignment of correctanswer
    		                                     // to the equation function
    
    		if ( correctanswer == answer )       // tests user's answer
    		{
    			cout << "Very good!\n\n"; 
    			++numcorrect;
    		}
    		else
    		{
    			cout << "Incorrect.\n"; 
    			cout << "The correct answer is " << correctanswer << "\n\n";
    		}
    
    	}                                        // end while loop
    
    	cout << "\nYou answered " << numcorrect 
    		 << " questions correctly.\n\n";     // Prompts # of correct answers
    	
    	return 0;
    }
    
    // Function implementation
    int num() 
    {
    	return 1 + ( rand() % 12 );    // returns random integer between 1 and 12
    }
    
    // Function implementation
    char sign()
    {
    	int k = 1 + rand() % 5;   // assigns random integers between 1 and 5 to k
    
    	if ( k == 1 )             // tests k and returns the + sign
    		return '+';
    
    	if ( k == 2 )             // tests k and returns the - sign
    		return '-';
    
    	if ( k == 3 )             // tests k and returns the * sign
    		return '*';
    
    	if ( k == 4 )             // tests k and returns the / sign
    		return '/';
    
    	if ( k == 5 )             // tests k and returns the % sign
    		return '%';
    }                             // ends the sign function implementation block
    
    // Function implementation
    // a, c, and b below are parameters to 
    // the equation function implementation
    int equation ( int a, char c, int b )
    {
    	if ( c == '+' )           // tests c to return the appropriate equation
    		return a + b;
    
    	if ( c == '-' )
    		return a - b;
    
    	if ( c == '*' )
    		return a * b;
    
    	if ( c == '/' )
    		return a / b;
    
    	if ( c == '%' )
    		return a % b;
    }                            // ends equation function implementation block

    Thank you!
    teeCee

  2. #2
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    There are no else's in those functions, so if something doesn't match any of the if statements, it falls off the end of the function without returning anything.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    2
    Thanks alot!!

    Wouldn't the else statements really be unnecessary though since the way it's set up, there are no possibilities for a false?

    I wanted to use the case structure instead, but I just wasn't able to get it to work.

    Thanks again,
    teeCee

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    If you really want to shut the compiler up, just stick a return 0; at the end of both of the functions. In some instances this warning is useful, others, it's just annoying.

  5. #5
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    In your situation, its unnecessary. That's why its a warning and not an error. The compiler doesn't exactly know what is going to happen, but it is possible for your function to not return anything, as it sees it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. 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