Switch statement

This is a discussion on Switch statement within the C++ Programming forums, part of the General Programming Boards category; I need a little help here. the code im posting was a nested if /ifelse.I thought i would change it ...

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Switch statement

    I need a little help here. the code im posting was a nested if /ifelse.I thought i would change it to a switch statement, this is part of the code i use to make a dll for a game i play on certain servers. What is does is reads what weapon i have(and the weapons are all defined i.e deagle is 26. this function was previously a bool.) and sets weather or not i can shoot you thru a wall. hehe.

    Since i changed it to a switch it compiles and the games loads but its not setting the penetration level anymore. I was hoping that some of you might be able to tell me what im doing wrong.
    Here's the code.
    Code:
    int CorrectGun(int); // in the header.
    
    then in the cpp.
    int penetrate;
    int CorrectGun( int weaponid)
    {
    	switch (weaponid)
    	{
    	case 26:
            penetrate = WALL_PEN1;
            break;
    	case 28:
    		penetrate = WALL_PEN1;
    		break;
    	case 22:
    		penetrate = WALL_PEN1;
    		break;
    	case 27:
    		penetrate = WALL_PEN1;
    		break;
    	case 8:
    		penetrate = WALL_PEN1;
    		break;
    	case 3:
    		penetrate = WALL_PEN2;
    		break;
    	case 18:
    		penetrate = WALL_PEN2;
    		break;
    	case 13:
    		penetrate = WALL_PEN1;
    		break;
    	case 24:
    		penetrate = WALL_PEN1;
    		break;
    	case 20:
    		penetrate = WALL_PEN1;
    		break;
    	default:
    		penetrate = WALL_PEN0;
    		break;	
        }
    	return penetrate;
    }
    
    //===================================================================================
    int CurPenetration(void)
    {
    	int weaponid = penetrate;
    	if (CorrectGun(weaponid))
    		return penetrate;
    	return WALL_PEN0;
    }
    Let me know if you need additional info.
    thks.
    big146

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int weaponid = penetrate;

    Is this right? You are setting weaponid equal to the current value of penetrate (either WALL_PEN0, WALL_PEN1, WALL_PEN2, or maybe 0). So it can be one of four values, but you have many more cases in your switch).

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Yes...depending on the weapon that im currently holding it will read weather or not it is powerful enough to shoot through the wall. if wall pen 1 it will shoot thru one wall ...if wall pen 2 it will shoot through two walls. the reason there is more switch statemnets then wall_pen is that there are diff weapons that i can be holding. i.e pistol. machine gun.. rifle ect.
    big146

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    But why are you setting weaponid = penetrate? They are two different things. weaponid is the type of weapon (pistol. machine gun.. rifle), and penetrate is how powerful it is (can it shoot though a wall).

  5. #5
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Here's the orignal source

    here is what im trying to convert to a switch...im thinking that maybe a switch stament would be faster.
    Code:
    //===================================================================================
    int penetrate;
    bool CorrectGun()
    {
    	if(currentWeaponID==WEAPON_DEAGLE, currentWeaponID==WEAPON_SIG, currentWeaponID==WEAPON_COLT, currentWeaponID==WEAPON_PARA, currentWeaponID==WEAPON_AK, currentWeaponID==WEAPON_AUG, currentWeaponID==WEAPON_SG550, currentWeaponID==WEAPON_G3SG1)
        {
            penetrate = WALL_PEN1;
            return true;
        }
    	else if(currentWeaponID==WEAPON_SCOUT, currentWeaponID==WEAPON_AWP)
        {
            penetrate = WALL_PEN2;
            return true;
        }
        else
        {
            penetrate = WALL_PEN0;
            return true;
        }
    }
    
    //===================================================================================
    int CurPenetration(void)
    {
    	if (CorrectGun())
    		return penetrate;
    	return WALL_PEN0;
    }
    big146

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could try something like:
    Code:
    int penetrate;
    bool CorrectGun()
    {
    	switch (currentWeaponID)
    	{
    		case WEAPON_DEAGLE:
    		case WEAPON_SIG:
    		case WEAPON_COLT:
    		case WEAPON_PARA:
    		case WEAPON_AK:
    		case WEAPON_AUG:
    		case WEAPON_SG550:
    		case WEAPON_G3SG1:
    			penetrate = WALL_PEN1;
    			return true;
    		case WEAPON_SCOUT:
    		case WEAPON_AWP:
    			penetrate = WALL_PEN2;
    			return true;
    		default:
    			penetrate = WALL_PEN0;
    			return true;
    	}
    }
    And you are right. A switch statement will definitely be faster.

  7. #7
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    thx swoopy that did the trick.. I tested and it works great! I did not realize that u could use a switch statement without the break; in it( -> noob).
    big146

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >im thinking that maybe a switch stament would be faster.
    Any difference would be negligable, even in a real-time game. If you want to improve the performance, look to your algorithms, not your control statements.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 04:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 03:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 06:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 03:47 PM

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