CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2006
    Posts
    25

    If statement question

    I feel extremely stupid for asking this question but its driving me bonkers. It just wont work!

    Say you have a variable A. Its 2000.

    A = 2000

    You have a variable B. Now, B is gonna be something, just dont know what yet. But there are certain things you want to do for each.

    If A is bigger than twice the size of B, do this.
    If A is bigger than Three times the size of B, do this.
    If A is bigger than four times the size of B, do this.

    You get the drift, right?

    Now I cant just do A = (2*B) cause I if A is 2000 and b is 900, well its not equal 1800, but it is bigger than 900 * 2.

    This should be super simple, and I feel stupid cause its not working right... I have (basically)


    if(A > (5 * B))

    else if(A > (4 * B))

    else if(A > (4 * B))

    else if(A > (2 * B))

    else


    Cept it doesnt work. Say A is 2000. Even if the number is 1000, it still uses the first one (If A is 5 times bigger than B). Which its not. >_> Its only 2 times bigger

    Any ideas? =D And try not to laugh at me too hard.

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: If statement question

    You probably have a syntax problem in your code. Post the real code here so that we can help you fix it. Remember that when you have two or more statements in an if body, you need braces ("{" and "}").
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Nov 2006
    Posts
    25

    Re: If statement question

    This is for a small RPG Ive been working on for fun. Here is the code that it is being used for

    int MBattle()
    {
    if (mpowerLevel > powerLevel)
    {
    if (mpowerLevel > (10 * powerLevel))
    {
    mhit = ((rand() % 500) + 1);
    }

    else if (mpowerLevel > (9 * powerLevel))
    {
    mhit = ((rand() % 550) + 1);
    }

    else if (mpowerLevel > (8 * powerLevel))
    {
    mhit = ((rand() % 400) + 1);
    }

    else if (mpowerLevel > (7 * powerLevel))
    {
    mhit = ((rand() % 350) + 1);
    }

    else if (mpowerLevel > (6 * powerLevel))
    {
    mhit = ((rand() % 300) + 1);
    }

    else if (mpowerLevel > (5 * powerLevel))
    {
    mhit = ((rand() % 250) + 1);
    }

    else if (mpowerLevel > (4 * powerLevel))
    {
    mhit = ((rand() % 200) + 1);
    }

    else if (mpowerLevel > (3 * powerLevel))
    {
    mhit = ((rand() % 150) + 1);
    }

    else if (mpowerLevel > (2 * powerLevel))
    {
    mhit = ((rand() % 100) + 1);
    }

    else if (mpowerLevel > (1.9 * powerLevel))
    {
    mhit = ((rand() % 90) + 1);
    }

    else if (mpowerLevel > (1.8 * powerLevel))
    {
    mhit = ((rand() % 80) + 1);
    }

    else if (mpowerLevel > (1.7 * powerLevel))
    {
    mhit = ((rand() % 70) + 1);
    }

    else if (mpowerLevel > (1.6 * powerLevel))
    {
    mhit = ((rand() % 60) + 1);
    }

    else if (mpowerLevel > (1.5 * powerLevel))
    {
    mhit = ((rand() % 50) + 1);
    }

    else if (mpowerLevel > (1.4 * powerLevel))
    {
    mhit = ((rand() % 40) + 1);
    }

    else if (mpowerLevel > (1.3 * powerLevel))
    {
    mhit = ((rand() % 35) + 1);
    }

    else if (mpowerLevel > (1.2 + powerLevel))
    {
    mhit = ((rand() % 30) + 1);
    }

    else if (mpowerLevel > (1.1 * powerLevel))
    {
    mhit = ((rand() % 25) + 1);
    }

    else
    {
    mhit = ((rand() % 20) + 1);
    }
    }




    No matter what numbers are involved, it ALWAYS chooses the first if. >_>

    Edit: Sorry it doesnt seem to like indenting code on here. its indented on the posting screen, but not when I post it

    Edit: I'd put the rest of the program but there's a whole lot of it.
    Last edited by Campuschris; November 14th, 2006 at 06:09 PM.

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: If statement question

    In the code that you posted, you forgot a closing brace. Not sure whether this has anything to do with your error. But apart from that the code is fine. In fact, if you try out the following compilable program, you'll see that it does enter the correct condition:
    Code:
    void MBattle(int mpowerLevel, int powerLevel, int &mhit)
    {
    	if (mpowerLevel > powerLevel)
    	{
    		if (mpowerLevel > (10 * powerLevel))
    		{
    			mhit = ((rand() %  500) + 1);
    		}
    
    		else if (mpowerLevel > (9 * powerLevel))
    		{
    			mhit = ((rand() % 550) + 1);
    		}
    
    		else if (mpowerLevel > (8 * powerLevel))
    		{
    			mhit = ((rand() % 400) + 1);
    		}
    
    		else if (mpowerLevel > (7 * powerLevel))
    		{
    			mhit = ((rand() % 350) + 1);
    		}
    
    		else if (mpowerLevel > (6 * powerLevel))
    		{
    			mhit = ((rand() % 300) + 1);
    		}
    
    		else if (mpowerLevel > (5 * powerLevel))
    		{
    			mhit = ((rand() % 250) + 1);
    		}
    
    		else if (mpowerLevel > (4 * powerLevel))
    		{
    			mhit = ((rand() % 200) + 1);
    		}
    
    		else if (mpowerLevel > (3 * powerLevel))
    		{
    			mhit = ((rand() % 150) + 1);
    		}
    
    		else if (mpowerLevel > (2 * powerLevel))
    		{
    			mhit = ((rand() % 100) + 1);
    		}
    
    		else if (mpowerLevel > (1.9 * powerLevel))
    		{
    			mhit = ((rand() % 90) + 1);
    		}
    
    		else if (mpowerLevel > (1.8 * powerLevel))
    		{
    			mhit = ((rand() % 80) + 1);
    		}
    
    		else if (mpowerLevel > (1.7 * powerLevel))
    		{
    			mhit = ((rand() % 70) + 1);
    		}
    
    		else if (mpowerLevel > (1.6 * powerLevel))
    		{
    			mhit = ((rand() % 60) + 1);
    		}
    
    		else if (mpowerLevel > (1.5 * powerLevel))
    		{
    			mhit = ((rand() % 50) + 1);
    		}
    
    		else if (mpowerLevel > (1.4 * powerLevel))
    		{
    			mhit = ((rand() % 40) + 1);
    		}
    			
    		else if (mpowerLevel > (1.3 * powerLevel))
    		{
    			mhit = ((rand() % 35) + 1);
    		}
    
    		else if (mpowerLevel > (1.2 + powerLevel))
    		{
    			mhit = ((rand() % 30) + 1);
    		}
    
    		else if (mpowerLevel > (1.1 * powerLevel))
    		{
    			mhit = ((rand() % 25) + 1);
    		}
    
    		else
    		{
    			mhit = ((rand() % 20) + 1);
    		}
    	}
    }
    
    int main()
    {
    	int hit;
    	MBattle(2000, 800, hit);
    	return 0;
    }
    So you have to make sure that you are actually passing the correct values and then that you are actually getting inside the correct condition. So place a breakpoint at if (mpowerLevel > powerLevel), look at the values for mpowerLevel and powerLevel and then check whether the correct condition gets executed.

    P.S. When you post source code, please use the [code] and [/code] tags. It makes it so much more readable.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Nov 2006
    Posts
    25

    Re: If statement question

    Thats a good idea! Thanks a bunch, I'll give that a shot!

    Sorry about the code part, I wasnt sure how. This is my first time posting here.

  6. #6
    Join Date
    Nov 2006
    Posts
    25

    Re: If statement question

    You hit the nail on the head! It was sending 0 as one of the ones. YAAY Thank you!

  7. #7
    Join Date
    Nov 2006
    Posts
    25

    Re: If statement question

    One more question on this and I promise I'll leave you alone! lol

    here is my full battle code

    Code:
    int MBattle()
    {
    
    cout<<"Current mpowerLevel is "<<mpowerLevel<<endl;
    cout<<"Current powerlevel is "<<powerLevel<<endl;
    
    if (mpowerLevel > powerLevel)
    {
    	if (mpowerLevel > (10 * powerLevel))
    	{
    		mhit = ((rand() %  500) + 1);
    	}
    
    	else if (mpowerLevel > (9 * powerLevel))
    	{
    		mhit = ((rand() % 550) + 1);
    	}
    
    	else if (mpowerLevel > (8 * powerLevel))
    	{
    		mhit = ((rand() % 400) + 1);
    	}
    
    	else if (mpowerLevel > (7 * powerLevel))
    	{
    		mhit = ((rand() % 350) + 1);
    	}
    
    	else if (mpowerLevel > (6 * powerLevel))
    	{
    		mhit = ((rand() % 300) + 1);
    	}
    
    	else if (mpowerLevel > (5 * powerLevel))
    	{
    		mhit = ((rand() % 250) + 1);
    	}
    
    	else if (mpowerLevel > (4 * powerLevel))
    	{
    		mhit = ((rand() % 200) + 1);
    	}
    
    	else if (mpowerLevel > (3 * powerLevel))
    	{
    		mhit = ((rand() % 150) + 1);
    	}
    
    	else if (mpowerLevel > (2 * powerLevel))
    	{
    		mhit = ((rand() % 100) + 1);
    	}
    
    	else if (mpowerLevel > (1.9 * powerLevel))
    	{
    		mhit = ((rand() % 90) + 1);
    	}
    
    	else if (mpowerLevel > (1.8 * powerLevel))
    	{
    		mhit = ((rand() % 80) + 1);
    	}
    
    	else if (mpowerLevel > (1.7 * powerLevel))
    	{
    		mhit = ((rand() % 70) + 1);
    	}
    
    	else if (mpowerLevel > (1.6 * powerLevel))
    	{
    		mhit = ((rand() % 60) + 1);
    	}
    
    	else if (mpowerLevel > (1.5 * powerLevel))
    	{
    		mhit = ((rand() % 50) + 1);
    	}
    
    	else if (mpowerLevel > (1.4 * powerLevel))
    	{
    		mhit = ((rand() % 40) + 1);
    	}
    		
    	else if (mpowerLevel > (1.3 * powerLevel))
    	{
    		mhit = ((rand() % 35) + 1);
    	}
    
    	else if (mpowerLevel > (1.2 + powerLevel))
    	{
    		mhit = ((rand() % 30) + 1);
    	}
    
    	else if (mpowerLevel > (1.1 * powerLevel))
    	{
    		mhit = ((rand() % 25) + 1);
    	}
    
    	else if (mpowerLevel > powerLevel)
    	{
    		mhit = ((rand() % 20) + 1);
    	}
    	
    	else if (mpowerLevel == powerLevel)
    	{
    		mhit = ((rand() % 20) + 1);
    	}
    }
    
    
    if (mpowerLevel > powerLevel)
    {
    	if (powerLevel > (10 * mpowerLevel))
    	{
    		mhit = 0;
    	}
    
    	else if (powerLevel > (9 * mpowerLevel))
    	{
    		mhit = 0;
    	}
    
    	else if (powerLevel > (8 * mpowerLevel))
    	{
    		mhit = ((rand() % 1) + 1);
    	}
    
    	else if (powerLevel > (7 * mpowerLevel))
    	{
    		mhit = ((rand() % 2) + 1);
    	}
    
    	else if (powerLevel > (6 * mpowerLevel))
    	{
    		mhit = ((rand() % 3) + 1);
    	}
    
    	else if (powerLevel > (5 * mpowerLevel))
    	{
    		mhit = ((rand() % 4) + 1);
    	}
    
    	else if (powerLevel > (4 * mpowerLevel))
    	{
    		mhit = ((rand() % 5) + 1);
    	}
    
    	else if (powerLevel > (3 * mpowerLevel))
    	{
    		mhit = ((rand() % 6) + 1);
    	}
    
    	else if (powerLevel > (2 * mpowerLevel))
    	{
    		mhit = ((rand() % 10) + 1);
    	}
    
    	else if (powerLevel > (1.9 * mpowerLevel))
    	{
    		mhit = ((rand() % 11) + 1);
    	}
    
    	else if (powerLevel > (1.8 * mpowerLevel))
    	{
    		mhit = ((rand() % 12) + 1);
    	}
    
    	else if (powerLevel > (1.7 * mpowerLevel))
    	{
    		mhit = ((rand() % 13) + 1);
    	}
    
    	else if (powerLevel > (1.6 * mpowerLevel))
    	{
    		mhit = ((rand() % 14) + 1);
    	}
    
    	else if (powerLevel > (1.5 * mpowerLevel))
    	{
    		mhit = ((rand() % 15) + 1);
    	}
    
    	else if (powerLevel > (1.4 * mpowerLevel))
    	{
    		mhit = ((rand() % 16) + 1);
    	}
    		
    	else if (powerLevel > (1.3 * mpowerLevel))
    	{
    		mhit = ((rand() % 17) + 1);
    	}
    
    	else if (powerLevel > (1.2 + mpowerLevel))
    	{
    		mhit = ((rand() % 18) + 1);
    	}
    
    	else if (powerLevel > (1.1 * mpowerLevel))
    	{
    		mhit = ((rand() % 19) + 1);
    	}
    }
    
    return 0;
    
    }

    Thats the function MBattle, which determines how hard the mob hits the player. There is a problem, if the player has a higher powerlevel than the mob it does NO damage at all. 0. Also, if the player and mob are the exact same powerlevel, they do no damage at all.

    I put in breakpoints and checked, it is sending the proper values for mpowerLevel and powerLevel. Any ideas?

  8. #8
    Join Date
    May 2005
    Posts
    399

    Re: If statement question

    There is a typo...

    The second if statement should read

    if (powerLevel > mpowerLevel)

  9. #9
    Join Date
    Nov 2006
    Posts
    25

    Re: If statement question

    AHAAA!

    Thank you so much. Im horrible about that stuff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured