Thread: Illegal Operation

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    39

    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.
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    That would be a good idea (to post the code), but you aren't trying to divide by 0 are you?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    -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.
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Post code.

  6. #6
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    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.
    hasafraggin shizigishin oppashigger...

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    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;
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  8. #8
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Post the section of code where this happens

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Is armorEP or weaponEP equal to 0?

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    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;        
        }
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  12. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    > A: I AM NOT dividing. I am making the computer pick a number between 0 and the value of "weaponEP" and "armorEP".

    your doing this how?

    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.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  13. #13
    Registered User
    Join Date
    Jun 2002
    Posts
    39
    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.
    Compiler: Dev-C++ 4.9.8.0

    -Bert

  14. #14
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    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.
    Last edited by foniks munkee; 07-21-2002 at 05:48 PM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    "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.
    Last edited by Gabu; 07-21-2002 at 10:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  2. i get illegal operation.
    By entry_point in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2007, 09:41 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. illegal operation error
    By xlordt in forum C Programming
    Replies: 2
    Last Post: 09-01-2003, 07:53 AM
  5. Illegal operation
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 04:33 PM