bool help

I'm trying to make a function to check if a Random number is less than or equal too another number (called hit_chance)

1
2
3
4
5
6
7
8
9
10
11
12
 20 bool Weapon::did_you_hit(void)
 21 {
 22 
 23     if(int Random <=hit_chance)
 24 {
 25     hit_chance==true;
 26 }
 27 
 28 else
 29 {
 30 return false;
 31 }


I'm not sure why this code isn't working?
Variable Random is not declared, did you mean this?
1
2
3
4
5
6
7
8
9
10

bool Weapon::did_you_hit(int Random, int hit_chance) {
  
     if(Random <= hit_chance) {  //braces not needed but I put them there in case I add more code later
          return true;
     }
     else {
           return false;
     }
}


This should be declared a const function because you are not changing the value of the arguments.

You could investigate the use of the ternary ?: operator as a short cut for if then else.

HTH

Edit:

Is there any way you can avoid posting code with line numbers? I had to manually delete each one.

Edit2:

You shoiuld have kept this in the other thread as they are so closely related. helios might spend time answering the same question in the other thread.
Last edited on
Topic archived. No new replies allowed.