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 "}").
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.
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.
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. :)
Re: If statement question
You hit the nail on the head! It was sending 0 as one of the ones. :) YAAY Thank you!
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?
Re: If statement question
There is a typo...
The second if statement should read
if (powerLevel > mpowerLevel)
Re: If statement question
AHAAA!
Thank you so much. :) Im horrible about that stuff