I need help with this Boolean statement. Why do I always come out with 0?

if (fga || fgm == 0){
field_goal_percentage = 0;}
else {
field_goal_percentage == fgm / fga; }

if (tpm || tpa == 0) {
three_point_percentage = 0;}
else {
three_point_percentage = tpm/tpa;}

if (ftm || fta == 0) {
free_throw_percentage = 0;}
else {
free_throw_percentage = ftm/fta;}

total_rebounds = off + def;

points = (2*fgm) + tpm + ftm;

if (min || points == 0) {
points_per_48 = 0;}
else {
points_per_48 = points/ (min/48);}
maybe you mean to check if
1
2
3
4
5
6
7
8
if(tpm ==  0 || tpa == 0){
...
}
(...)
if(ftm == 0 || fta == 0){
(...)
}
(...)

and so forth... because if tpm hasn't been initialised to 0 (<type> tpm = 0) your condition will always be true because tpm != 0 (so it's true). Same thing with fga, ftm and min.

Hope this helps!

AeonFlux1212

[edit]What I mean is that if you write
1
2
3
4
if(ftm || fta == 0)
{
(...)
}

the program will check if ftm is true or false, which ends up meaning for integer or floating point that if ftm is NOT equal to zero then the condition si true and your program doesn't even check if fta == 0 or not, because it's an OR in the condition statement.

Hope this clears things out!

AeonFlux1212[/edit]
Last edited on
Well according to maths you need to change all if statements cos i suppose from the divide of the variables they are all int or double(float)
Here is some of my assumptions for it:
since you divide fgm/fga assumed from the code its a basketball points program where
fgm=field goals made and fga=field goals attempts all you need to do is just check if fga!=0 something like this

//use this code from here
if (fga != 0 ){
field_goal_percentage = fgm/fga;}

if (tpa != 0 ) {
three_point_percentage = tpm/tpa;}

if (fta != 0 ) {
free_throw_percentage = ftm/fta;}

total_rebounds = off + def;

points = (2*fgm) + tpm + ftm;

if (min != 0) {
points_per_48 = points/ (min/48);}
// to here

Try this code replacing with yours i think it will be doing the exact maths you need !
Last edited on
Topic archived. No new replies allowed.