clogic-error

C Code Logic Error (calculating an hourly wage)


This is the description of what hourly should do: Hourly workers are paid by the hour and allowed to work 20 of hours a week, where they'll receive 100% of their paycheck, but can also work up to 20 additional hours of overtime per month where they'll be paid 125% of their wage (so 20 normal hours, 20 overtime hours)

below is my C code:

#include <stdio.h>

int main(void) {
    printf("Hello! Please input your worker ID (A = Admin., S = Staff, H = Hourly, J = Adj. Faculty, R = Reg. Faculty, and T = Teaching Assistant) or Q to quit. \n");
    char type;
    scanf("%c", &type);

    int salary = 0;
    int overtimeHours = 0;
    int hoursWorked = 0;
    float hourly = 0.0;
    float hAverage = 0;
    int hCounter = 0;
    float hTotal = 0.0;
    while (type != 'q' && type != 'Q'){

        switch(type) {
        case 'h':
        case 'H':
            //40 hours a WEEK
            overtimeHours = 0;
            hoursWorked = 0;
            hourly = 0.0;
            salary = 0;
            puts("Please enter your hourly wage and hours worked.");
            scanf(" %f %d", &hourly, &hoursWorked);
                
            hoursWorked = hoursWorked / 4.0;

            if (hoursWorked > 40) {
                hoursWorked = 20;
                overtimeHours = 20;

                //for the week
                hTotal += 4 *((hoursWorked * hourly)+ (overtimeHours * hourly * 1.25)); //WORKS
                hAverage +=  4 * (40 * hourly);
            }
            else if (hoursWorked > 20){
                overtimeHours = hoursWorked - 20;
                hoursWorked = 20;

                hTotal += 4 *((hoursWorked * hourly)+ (overtimeHours * hourly * 1.25));// WORKS
                hAverage +=  4 * ((hoursWorked) * hourly);
            }
            else {
                hTotal += 4 *((hoursWorked * hourly)+ (overtimeHours * hourly * 1.25)); //WORKS
                hAverage +=  4 * ((hoursWorked) * hourly);
            }

            hCounter++;
                
            break;
        } //ends switch

        puts("Please enter another worker ID, Q for quit, or ? for directions");
        scanf(" %c", &type);

    } // end while
    printf("Hourly     %.2f          %.2f       \n", (hAverage/hCounter), hTotal/hCounter);
    return 0;

}//end main

This is only a small portion of the full program. When I only enter one input for hourly it works but with multiple it calculates the wrong input.


Solution

  • In reviewing your code and noting the good comments made, it appears that there are blocks of code that are basically repeating the desired end result of accumulating total averages. And that can set one up for possible error scenarios. Also, the "while" loop seemed to be a bit complicated as well.

    With that in mind following is a refactored version of the program.

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void)
    {
        char type;
    
        int salary;
        int overtimeHours;
        int hoursWorked;
        int hCounter = 0;
    
        double hourly;
        double hAverage = 0.0;
        double hTotal = 0.0;
    
        printf("Hello! Please input your worker ID (A = Admin., S = Staff, H = Hourly, J = Adj. Faculty, R = Reg. Faculty, and T = Teaching Assistant) or Q to quit: ");
    
        while (1)                           /* Simplify the while loop          */
        {
            if (scanf(" %c", &type) != 1)   /* Just in case no entry is made    */
                type = 'q';
    
            if (tolower(type) == 'q')
                break;
    
            switch(tolower(type))
            {
            case 'h':
                //40 hours a WEEK - I guess that there are four weeks in a period
                overtimeHours = 0;
                hoursWorked = 0;
                hourly = 0.0;
                salary = 0;
    
                puts("Please enter your hourly wage and hours worked: ");
    
                if (scanf(" %lf %d", &hourly, &hoursWorked) != 2)
                    break;
    
                if (hoursWorked > 100)
                {
                    hoursWorked   = 20;
                    overtimeHours = 20;
                }
                else if (hoursWorked > 80)
                {
                    overtimeHours = hoursWorked - 80;
                    hoursWorked = 20;
                }
                else
                {
                    overtimeHours = 0;
                    hoursWorked /= 4;
                }
    
                hTotal   += 4 * (hoursWorked * hourly) + overtimeHours * hourly * 1.25;     /* Just need one set of accumulation formulas once hours and overtime are deduced */
                hAverage += 4 * (hoursWorked * hourly);
    
                hCounter++;
    
                break;
    
            default:        /* To accomodate other future scenarios */
                break;
            } //ends switch
    
            puts("Please enter another worker ID, Q for quit, or ? for directions: ");
    
        } // end while
    
        if (hCounter > 0)
            printf("Hourly     %.2f          %.2f       \n", (hAverage/hCounter), hTotal/hCounter);
        else
            printf("No time and wage data was entered\n");  /* Fallback in case no data was entered */
    
        return 0;
    }//end main
    

    Following are some key points.

    Since the revisions were prevalent throughout the refactored code, it seemed necessary to include the whole refactored program in this answer.

    With that, a simple test of the refactored code was done with the entry of one hourly employee with no overtime, and one employee with ten hours of overtime.

    craig@Vera:~/C_Programs/Console/Wages/bin/Release$ ./Wages 
    Hello! Please input your worker ID (A = Admin., S = Staff, H = Hourly, J = Adj. Faculty, R = Reg. Faculty, and T = Teaching Assistant) or Q to quit: h
    Please enter your hourly wage and hours worked: 
    10.00 80
    Please enter another worker ID, Q for quit, or ? for directions: 
    h
    Please enter your hourly wage and hours worked: 
    20.00 90
    Please enter another worker ID, Q for quit, or ? for directions: 
    q
    Hourly     1200.00          1325.00   
    

    Hand checking the values on a calculator seemed to verify that the proper averages were being calculated.

    As always, review and test this for yourself. Also, you might want to refer to some additional tutorial literature especially as it pertains to utilizing "while" loops and how to break out of such loops.