Reputation: 8280
I have a run time issue when i debugged my script.
It says :
Unhandled exception at 0x002a4f8e in test2.exe: 0xC0000094: Integer division by zero.
Although the line in question is a rand number generation.
if((rand() % miss) < 3){ //line 40
int phit = (rand()% hit);
} else {
bool pmiss = true;
}
Debugger stops at line 40 when it goes wrong (which i labelled above).
My full script is here: http://www.paste.to/MzQ1ODM1
How should I have written the if statement if this causes such an error, i keep thinking like a PHP programmer which is v.frustrating to adapt!
Upvotes: 1
Views: 3720
Reputation: 706
line 29 and 32 should be
miss = 5
miss = 2
you are declaring a new local variable miss inside the if scope which means you are not changing the one declared at line 6
Upvotes: 1
Reputation: 18349
You are re-declaring miss
at line 30. That variable will be local to the if statement. Therefore, the global miss
will still have a value of zero. Take out all those int
, and also the bool
inside the if
statements! Like this:
if(user_weapon == 'k'){
miss = 5;
hit = 5;
} else if(user_weapon == 'b'){
miss = 2;
hit = 3;
} else {
invalid = true;
}
Upvotes: 5
Reputation:
The % (modulus) is the remnant of the division. So you divide a by b in a%b and get the remnant from it. If b is 0 (like in this case), you get an error.
Division by 0 is illegal and causes a cosmic disturbance (or code crash).
try setting the variable to another value.
Upvotes: 0