Function doesnt call

This is a discussion on Function doesnt call within the C++ Programming forums, part of the General Programming Boards category; This is driving me mad. I have been trying several things to sort this out, but my "gameOver()" function is ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,480

    Function doesnt call

    This is driving me mad. I have been trying several things to sort this out, but my "gameOver()" function is never read, dispite doing everything right that I can see.

    The whole code is over 650 lines so Il only post the parts relevant.

    I have also commented on the code what is going wrong.

    Here is the player struct:

    Code:
    typedef struct
    {
    	std::string m_PlayerName;
    	unsigned long m_PlayerScore;
    	unsigned short m_Lives;  // This is assigned a value of 3 in a function
    	double m_Gold;
    } Player;
    This is the second room of the game, where the problem is, but I have grealtly reduced it to show the problem

    Code:
    // function with details of room two
    int roomTwo ( Room *rm, Player *client )
    {
    	std::cout << "Lives: " << client->m_Lives << std::endl;
    
    	rm->m_DoorIslocked = true;
    
    	std::cout << rm->mp_RoomDiscrip[ 1 ] << " ";
    
    	const char *pQuestions[ 5 ] =
    	{
    		"\nHow many legs on a spider?\n\n",
    		"\nHow many colours in the rainbow?\n\n",
    		"\nWhat is the opposite to black?\n\n",
    		"\n5 * 50 = ?\n\n",
    		"\nCrystal is a?\n\n"
    	};
    
    	bool answer = false, haveKey = false, isAlive = true;
    
    	int choice = rand() % 4 + 0;
    
    	while ( rm->m_DoorIslocked == true )
    	{
    		if ( rm->m_DoorIslocked == false )
    		{
    			break;
    		}
    
    		else if ( client->m_Lives <= 0 )
    		{
    			isAlive = false; // Player is defeated token
    
    			break; // get out of the loop
    		}
    
    		while ( answer == false )
    		{
    			std::cout << "Lives: " << client->m_Lives << "\n\n";
    
    			int theAnswer = 0;
    			std::string theReply = "";
    
    			std::cout << pQuestions[ choice ] << " ";
    			std::cout << "> ";
    
    			switch ( choice ) 
    			{
    			case 0: // for minimal code only showing one
    				std::cin >> theAnswer;
    
    				if ( theAnswer == 8 )
    				{
    					answer = true;
    					haveKey = true;
    
    					if ( haveKey == true )
    					{
    						rm->m_DoorIslocked = false;
    					}
    				}
    
    				else
    				{
    					if ( client->m_Lives <= 0 )
    					{
    						answer = true;
    						haveKey = true;
    
    						if ( haveKey == true )
    						{
    							rm->m_DoorIslocked = false;
    						}
    					}
    
    					client->m_Lives--; // lose a life
    				}
    				break;
    
    						default:
    				break;
    			}
    		}
    	}
    
    	if ( isAlive == false )  // This is the function that is not being called
    	{                              // dispite the isAlive being equal to false
    		gameOver();
    	}
    
    	roomThree ( rm, client ); // instead, it skips to here and the new room
    
    	return 0;
    }
    I am sure it is somthing very small I am doing wrong, as if the answer is entered correctly, the program works fine and calls the third room no problems. It just after 3 incorrect answers, the game over function is not called dispite the loop breaking as it should do.

    Any help greatly appriciated.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    At a glance, you have quite alot of redundant code that could be removed to simplify your problem. For example, consider:
    Code:
    while ( rm->m_DoorIslocked == true )
    {
    	if ( rm->m_DoorIslocked == false )
    	{
    		break;
    	}
    
    	else if ( client->m_Lives <= 0 )
    	{
    		isAlive = false; // Player is defeated token
    
    		break; // get out of the loop
    	}
    You could discard the isAlive variable and just write:
    Code:
    while ( rm->m_DoorIslocked && client->m_Lives > 0 )
    {
    I suspect that this is the very problem as well. At some point, rm->m_DoorIslocked becomes true, but isAlive remains true. You need to check the status of client->m_Lives in the inner loop, or something like that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,480
    Thanks for the advice laserlight, Il see what I can do, il change the code as you suggested too, I think the readabity could be better I agree.
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
      if ( client->m_Lives <= 0 )
    					{
    						answer = true;
    						haveKey = true;
    
    						if ( haveKey == true )
    						{
    							rm->m_DoorIslocked = false;
    						}
    					}
    m_DoorIslocked is set to false, causing the outer loop to terminate before isAlive can be set to false.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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