not all paths return a vale

This is a discussion on not all paths return a vale within the C++ Programming forums, part of the General Programming Boards category; hey all, my compiler says that not all these paths return a value, but to my eye they do. can ...

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    not all paths return a vale

    hey all, my compiler says that not all these paths return a value, but to my eye they do. can someone help spot my error? thanks in advance

    Code:
    ]
    char Game::mf_game_displayOptions(int options)
    {
    	char choice='h';//local only
    	if (options==1)
    	{	bool choiceCheck=false;
    		do
    		{
    		
    		cout<<endl<<endl<<endl<<"count under con"<<"<B>unt, <L>eft grounder, <R>ight grounder, " 
    			<<"<S>acrafice fly, <H>ome run"<<endl;
    		cin>>choice;
    		choice=tolower(choice);
    		if (choice=='b' || choice=='l' || choice=='r'
    			|| choice=='s' || choice=='h')
    		{	
    			choiceCheck=true;
    			return choice;
    		}
    		else
    		{	
    			clrscr();
    			cout<<"you've entered an invalid option, please change it.";
    			choiceCheck=false;
    		}
    		}while(choiceCheck==false);
    	}
    	else
    	{return (choice);}
    
    }

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    well technecilly your right but compilers can't read intentions, in essence to some compilers it would look like.

    }while(choiceCheck==false);

    if choiceCheck happens to go true before
    choice equals one of these,

    if (choice=='b' || choice=='l' || choice=='r'
    || choice=='s' || choice=='h')

    there is no return. it doesn't always see that that is "technically" not possible.

    so you have three options, get rid of the last else since all it does is return choice anyway, add a return at the end of the function, or ignore it.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Indeed, the last line of a function must álways return the right type, even when that's redundant.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Your inner if statement might be easier to read as a case statement.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  2. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21