1. ## Illegal Operation

I am making a RPG and when I entered battle and attacked something pop-ed up saying that "this program preformed and illegal operation". When I clicked "details" this showed up
GAME caused a divide error in module GAME.EXE at 0167:00401a40.
Registers:
EAX=00004823 CS=0167 EIP=00401a40 EFLGS=00010202
EBX=00000002 SS=016f ESP=0074fdb0 EBP=0074fdd8
ECX=00004823 DS=016f ESI=816c3a2c FS=6327
EDX=00000000 ES=016f EDI=00000000 GS=0000
Bytes at CS:EIP:
f7 3d 9c 80 41 00 89 15 94 80 41 00 e8 9b f8 00
Stack dump:
00540000 0074fdd4 0040ce4c 0040ce9e 00418144 206c3a2c 0074fdf8 004012b5 0041703c 00000029 0074fdf8 004012fd 00417000 00417004 0074fe00 ffffffff
How do I fix this? I can post the source code if ya want.

2. That would be a good idea (to post the code), but you aren't trying to divide by 0 are you?

3. Oooh ooh! Let's place bets! I'm betting he's using an uninitialized pointer! You've got divide by zero covered, any one else care to jump in here? (Yeah, I know, it's a divide error, but I'll take pointers just to make this fun...)

Quzah.

4. -I don't know what a "divide error" is so I can someone explain them to me?

-I'm not using pointers by the way.

5. Post code.

6. you can't divide by zero in algebra, right? so you can't divide by zero in your programming calculations either. check for divisions [or mod] by zero in your program.

7. I didn't have any division in my code but I think I know what the problem may be. I used the randmise thing. Here's a section of that code.
Code:
```        yourAttack = rand() % 15;
armorE = rand() % armorEP;
weaponE = rand() % weaponEP;```

8. Post the section of code where this happens

9. Is armorEP or weaponEP equal to 0?

10. >armorE = rand() % armorEP;
>weaponE = rand() % weaponEP;

I'm guessing that at some point, armorEP or weaponEP reaches 0... then you get a divide by zero error, since (I think) % divides it, then (I know) gives you the remainder. Try testing to see if it is 0, and doing the "rand() % ***EP" only if it is greater than 0.

11. A: I AM NOT dividing. I am making the computer pick a number between 0 and the value of "weaponEP" and "armorEP".

B: The "illegal operation" still happens when "weaponEP" and "armorEP" is not zero.

C: Here's the section of code where it happens.
Code:
```while (yourHP > 0 && enemyHP > 0)
{

//Your turn to attack or defend
cout << endl;
cout << "       *" << endl;
cout << " [-]  /" << endl;
cout << ">=#=<%" << endl;
cout << "//\\\\" << endl;
cout << "^  ^" << endl;

cout << "Enter 1 to Attack \n Enter 2 to Defend \n\n";
cin >> yourCommand;

//Processes command
//Command to attack
{
if (yourPower < 15)
{
cout << "You do not have enough Power Points to Attack \n";
cout << "Your HP: " << yourHP << "\n";
cout << "Your PP: " << yourPower << "\n";
cout << "Enemy HP: " << enemyHP << "\n";
cout << "Enemy PP: " <<enemyPower << "\n---\n";
}

yourAttack = rand() % 15;
armorE = rand() % armorEP;
weaponE = rand() % weaponEP;

enemyHP = enemyHP - (yourAttack + weaponE);
yourPower = yourPower - 5;
yourPower = yourPower + 3;

cout << "You have done " << yourAttack << " HP of damage to your opponent and you lost 5 Power Points attacking. But you also gained 3 Power Points during this turn. \n";
cout << "Your HP: " << yourHP << "\n";
cout << "Your PP: " << yourPower << "\n";
cout << "Enemy HP: " << enemyHP << "\n";
cout << "Enemy PP: " <<enemyPower << "\n---\n";
}

//Command to defend
if (yourCommand == 2)
{
if (yourPower < 5)
{
cout << "You do not have enough Power Points to Defend \n";
cout << "Your HP: " << yourHP << "\n";
cout << "Your PP: " << yourPower << "\n";
cout << "Enemy HP: " << enemyHP << "\n";
cout << "Enemy PP: " <<enemyPower << "\n---\n";
}

yourHP = yourHP + 5;
yourPower = yourPower + 5;

cout << "By defending you have gained 5 Power Points. And you have also gained 5 HP. \n";
cout << "Your HP: " << yourHP << "\n";
cout << "Your PP: " << yourPower << "\n";
cout << "Enemy HP: " << enemyHP << "\n";
cout << "Enemy PP: " <<enemyPower << "\n---\n";
}

//Enemy's turn to attack or defend
// Command: If hits are more than 20 then attack
if (enemyHP > 20)
{
enemyCommand = 1;
}

// Command: If hits are less than 20 then defend
if (enemyHP < 20)
{
enemyCommand = 2;
}

//Processes command
if (enemyCommand == 1)
{
enemyAttack = rand() % 15;

yourHP = yourHP - (enemyAttack - armorE);
enemyPower = enemyPower - 5;
enemyPower = enemyPower + 3;

cout << "Your opponent has taken " << enemyAttack << " HP from you. \n";
cout << "Your HP: " << yourHP << "\n";
cout << "Your PP: " << yourPower << "\n";
cout << "Enemy HP: " << enemyHP << "\n";
cout << "Enemy PP: " <<enemyPower << "\n\n";
}

if (enemyCommand == 2)
{
enemyHP = enemyHP + 5;
enemyPower = enemyPower + 5;

cout << "Since you opponent has decided to defend he gains 5 Power Points and 5 HP. \n";
cout << "Your HP: " << yourHP << "\n";
cout << "Your PP: " << yourPower << "\n";
cout << "Enemy HP: " << enemyHP << "\n";
cout << "Enemy PP: " <<enemyPower << "\n\n";
}

//End 'while' command
}

if (yourHP < 1)
{
cout << "You lost! \n";
yourMoney = yourMoney - 50;
enemyHP = 100;
enemyPower = 200;
}

if (enemyHP < 1)
{
cout << "You won! \n";
yourMoney = yourMoney + 50;
enemyHP = 100;
enemyPower = 200;
}```

12. > A: I AM NOT dividing. I am making the computer pick a number between 0 and the value of "weaponEP" and "armorEP".

armorE = rand() % armorEP;

english translation:

armorE equals the remainder of rand() devided by armorEP;

thusly limiting the maximum value of armorE to be armorEP-1.

% returns the remainder of a division(ie. rand() / armorEP).

its easy to get confused, just make sure weaponEP and armorEP are never 0.

13. But I have to make it "0" at some point. So I guess I should find another way for it to pick a random number or something.

14. No, just check the value of the variable before applying the % operator - if it is equal to 0 then make it equal to 1 for purpose of the division. Then you can reset it to 0. Easy

Before you do any division (and that includes using the modulus operator), you should always check that the right operand is not equal to zero.

15. "I AM NOT dividing."

To get the remainder, first you must divide. Modulus does that; therefore you ARE dividing.

Plus, even if it didn't, what's the remainder when dividing by zero? It'd still be an error.