Thread: If-else if statement testing float values

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    5

    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!!
    Last edited by micker2g; 12-09-2012 at 11:37 PM.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    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.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What are the netpay values being printed in your first two printf statements?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    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!

    [edit] 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..).
    Last edited by micker2g; 12-10-2012 at 11:44 AM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    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.
    Last edited by micker2g; 12-10-2012 at 06:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  2. testing for int or float
    By rodrigorules in forum C++ Programming
    Replies: 17
    Last Post: 11-23-2007, 12:16 PM
  3. Testing a stream object in a while statement
    By sudar in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2007, 08:50 PM
  4. testing if a number is a float
    By UnknownImage in forum C Programming
    Replies: 11
    Last Post: 02-13-2003, 11:10 AM
  5. Testing for ASCII values?
    By csmatheng in forum C Programming
    Replies: 1
    Last Post: 02-19-2002, 02:22 PM