# Thread: If-else if statement testing float values

1. ## If-else if statement testing float values

Yes this is a homework but I have been searching for the last two days and cannot find a solution. I am trying to test these float values for exact numbers in this case since I know the data being entered. I can't get the if else if statement to produce the intended results.

Code:
```void printCheckHeader(STR30Address info, EmployeeRecord employeeInfo[],int number,FILE * reportFile)
{
int checkNo = 100; // disregard
char Dollar[46];
char text1[46] = "Six Hundred Six and 36/100 Dollars           ";
char text2[46] = "Two Hundred Twenty-Three and 46/100 Dollars  ";
char text3[46] = "Three Hundred Twenty and 04/100 Dollars      ";
char text4[46] = "Five Hundred Seventy-Three and 83/100 Dollars";
char text5[46] = "Two Hundred Fourty-One and 93/100 Dollars    ";
printf("%f\n",employeeInfo[0].netpay); // these are printing the correct values as a test
printf("%f\n",employeeInfo[1].netpay); //printing correctly
for(int j = 0; j < number; j++)
{
if((employeeInfo[j].netpay) > 600.000000)
{
for(int l = 0; l < 47; l++)
Dollar[l] = text1[l];
}
else if((employeeInfo[j].netpay) < 225.000000)
{
for(int l = 0; l < 47; l++)
Dollar[l] = text2[l];
}
else if(((employeeInfo[j].netpay) > 315.000000) && ((employeeInfo[j].netpay) < 350.000000))
{
for(int l = 0; l < 47; l++)
Dollar[l] = text3[l];
}
else if(((employeeInfo[j].netpay) > 500.000000) && ((employeeInfo[j].netpay) <600.000000))
{
for(int l = 0; l < 47; l++)
Dollar[l] = text4[l];
}
else
{
for(int l = 0; l < 47; l++)
Dollar[l] = text5[l];
}```
Its going straight to the last else statement. I should also note that the employeeInfo[].netpay is a float and the for statement in the last else statement is transferring the array correctly. Just not the right one I'm supposed to be getting.

I was hoping someone could give me some pointers as to what im doing wrong. Thank you!!

2. Hey!

No matter how long I program I keep running into problems like this. I occasionally space out while coding or code after I get home from the bar (bad idea). My best method for solving this issue is I usually comment out all the other statements and just test them one by one. When I find the one that doesn't work like its supposed too I "zoom" in to the logic and work it out. Sooner or later you stumble upon the problem and the plus is when you solve it yourself you get better at debugging!

Another thing I occasionally do is place line outputs inside the if statements to say something like "condition 1" so I can watch the logic run. Or breakpoints in your code can help was well.

3. > for(int l = 0; l < 47; l++)
> Dollar[l] = text1[l];
Your arrays are only 46 chars long (subscripts 0 to 45 inclusive).
So all of these are buffer overruns.

Try using just
strcpy( Dollar, text1 );

4. What are the netpay values being printed in your first two printf statements?

5. Its printing
606.361511
223.456497
Which, of course, is why I'm testing ranges and not the exact values.

@Lesshardotfind I do the same thing! I'll spend way too much time on one
piece of code and it drives me nuts until i finally debug it.

@salem I'll try this but I don't think its the copying of the array to "Dollar" it seems as though the Conditions are at fault. I could be wrong though. Ill try this and report back!

 Salem: I changed the statements with the strcpy(Dollar, text1); and its still only executing the last "else" statements. (and yes I used the text1, text2, etc..).

6. Have you checked the value of 'number'? Which by the way is a poor name.

Can you make a minimal complete test program that preproduces the problem for us?

7. Solved! iMalc, I made a small test program and in doing so I found I was just overdoing it with the extra zeros at the end and some parenthesis. Here are the changes I made in case anyone else has a similar problem:
Code:
``` for(int j = 0; j < numEmployees; j++)        {
if ((employeeInfo[j].netpay) > 600)
{
strcpy( Dollar, text1 );
}
else if ((employeeInfo[j].netpay) < 225)
{
strcpy( Dollar, text2 );
}
else if (((employeeInfo[j].netpay) > 315) && ((employeeInfo[j].netpay) < 350))
{
strcpy( Dollar, text3 );
}
else if (((employeeInfo[j].netpay) > 500) && ((employeeInfo[j].netpay) < 600))
{
strcpy( Dollar, text4 );
}
else
{
strcpy( Dollar, text5 );
}
} //close for loop```
All i did was take off the zeros and put the struct variable in parenthesis. Which is still puzzling me because Im sure I tested it like this before. Oh well its working now. Thank you everyone for your advice.