Here is an excerpt from my code along with output. I've included print statements for debug purposes.
I've printed out all the values the code's if
statement should check; they are all correct. The code works correctly for most test cases where checkWeights
is added up (for example, with .2, .2, .2, .2). However, in the case of .1, .3, .3, .1, the code prints the Your weight distribution is invalid!
message, and it should not. What is going wrong here?
Code:
checkWeights = checkWeights + weights[i];
fprintf(stdout, "%lf\n", checkWeights);
fprintf(stdout, "%lf\n", totalWeight);
fprintf(stdout, "%d\n", i);
fprintf(stdout, "%d\n", categories - 1);
if((i == (categories - 1)) && (checkWeights != totalWeight))
{
fprintf(stdout, "\nYour weight distribution is invalid! Try again.\n");
dataProvided = 0;
break;
}
}
Output:
How many categories are you currently graded on? 4
These 4 categories account for how much of your grade? (1, .85, .1 etc.) .8
Weight for category 1 (1, .3, .22 etc.): .3
Please enter your score in category 1 (1, .23, .32 etc.): .321
0.300000
0.800000
0
3
Weight for category 2 (1, .3, .22 etc.): .1
Please enter your score in category 2 (1, .23, .32 etc.): .323
0.400000
0.800000
1
3
Weight for category 3 (1, .3, .22 etc.): .3
Please enter your score in category 3 (1, .23, .32 etc.): .232
0.700000
0.800000
2
3
Weight for category 4 (1, .3, .22 etc.): .1
Please enter your score in category 4 (1, .23, .32 etc.): .4534
0.800000
0.800000
3
3
Your weight distribution is invalid! Try again.