Thread: Beginner that needs help.

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    7

    Beginner that needs help.

    I have to write a program that calculates 5 employees' work hours and pay and prints it all in a list using a for loop. I also have to store user input into an array. And if -1 is entered by the user anywhere then the loop should be terminated.

    My output needs to look something like this:
    Enter name: Glenn
    Enter hourly rate: 2.00
    Enter hours worked: 50

    Enter name: -1

    Pay to: Glenn
    Hours worked: 50.0
    Hourly rate: $ 2.00
    Gross pay: $110.00
    Base pay: $ 80.00
    Overtime pay: $ 30.00
    Taxes paid: $ 22.00
    Net pay: $ 88.00

    So far I have done this but it is not working so I am not sure what I need to do. Could I get some advice please?
    (ps. I am a total beginner at c and I am still not a hundred percent sure how to do array so sorry. )
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define SIZE 5
    int main()
    {
        //variables
        double rate[5], hours[5];
        double overtimerate = 1.5;
        char name[5][20];
        double grosspay[5], overtimepay[5], basepay[5], taxpay[5], netpay[5];
    
    
        int loop, i = 0;
    
    
        //loop 5 times
        for (loop = 0; loop < 5; loop++)
        {
            //use if to break out of loop after input if -1 is entered
            printf("Enter your name:\n");
            scanf_s("%s", &name[loop], 20);
            if (strcmp(name, "-1") == 0)
                break;
            printf("Enter your hourly rate:\n");
            scanf_s("%f", &rate[loop]);
            if (rate[loop] == -1)
                break;
            printf("Enter number of hours worked:\n");
            scanf_s("%f", &hours[loop]);
            if (hours[loop] == -1)
                break;
        
        
    
    
        //output
        //math calculations to calculate pay and taxes
        //differentiate between overtime and non overtime pay
        for (loop = 0; loop < 5; loop++) {
            if (hours[loop] > 40) {
                basepay[loop] = rate[loop] * 40;
                overtimepay[loop] = (hours[loop] - 40)*(overtimerate*rate[loop]);
                grosspay[loop] = ((hours[loop] - 40)*(rate[loop] * overtimerate)) + (40 * rate[loop]);
                taxpay[loop] = .2*((hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop]));
                netpay[loop] = (hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop]) - (.2*((hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop])));
            }
            else {
                basepay[loop] = rate[loop] * hours[loop];
                taxpay[loop] = .2*(rate[loop] * hours[loop]);
                netpay[loop] = (rate[loop] * hours[loop]) - (.2*(rate[loop] * hours[loop]));
            }
        }
        for (i = 0; name[loop][i] != '\0'; i++) {
            printf("Employee: %s\n", name[loop][i]);
        }
        printf("Hourly rate: %.2f\n", rate[loop]);
        printf("Hours worked: %.2f\n", hours[loop]);
        printf("Base pay: %.2f\n", basepay[loop]);
        printf("Overtime pay: %.2f\n", overtimepay[loop]);
        printf("Your paycheck is: %.2f\n", grosspay[loop]);
        printf("Taxes paid:%.2f\n", taxpay[loop]);
        printf("Money taken home: %.2f\n", netpay[loop]);
    
    
    }
        system("pause");
    
    
        return 0;
    }

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h> // include for defintion of strcmp(), etc..
     
    #define SIZE 5 // why define SIZE here, then use "5" everywhere you should put "SIZE"???
    int main(void /* in C put void as parameter when function expects no parameters */)
    {
        //variables
         // should define constant for MAX_EMPLOYEE_NAME_LEN or some such
        float rate[5], hours[5]; // changed type "double" to "float", due to a bug when reading via "%f" printf format. If want to use double, use different format specificer!
        float overtimerate = 1.5; // should define a constant for this, as it is NOT a variable that can change!
        char name[5][20] = { 0 } ;
        float grosspay[5], overtimepay[5], basepay[5], taxpay[5], netpay[5];
    
        int num_employees = 0;
     
        // take employee data input
        for (int loop = 0; loop < 5; loop++) // use loop-local variable in "for" loop, also dont name your variable "loop"
        {
            //use if to break out of loop after input if -1 is entered
            printf("Enter your name:\n");
            scanf("%20s", &name[loop]);
            if (strcmp(name[loop], "-1") == 0) // name is the array of name strings, name[loop] is the current name string
                break;
            // removed the check for "-1" being entered for pay rate and # of hours worked, since it doesnt make
            // any sense to not enter these once the user is already entering an employee's info!
            // In other words, it should be a mandatory input. If not, you can re-write this such that -1 entered for
            // any value will abort the current employee data entry and continue to the next, or end input entirely...
            printf("Enter your hourly rate:\n");
            scanf("%f", &rate[loop]);
            printf("Enter number of hours worked:\n");
            scanf("%f", &hours[loop]);
            ++num_employees;
        } // yeah, you missed that bracket!
    
        //math calculations to calculate pay and taxes
        //differentiate between overtime and non overtime pay
        
        // I am sure all this math can be simplified a great deal.. I will leave that to you.
        
        for (int loop = 0; loop < num_employees; loop++) { // only calculate for employees actually entered by user
            if (hours[loop] > 40 /* again, define a constant MAX_NORMAL_PAY_HOURS or some such */) {
                basepay[loop] = rate[loop] * 40;
                overtimepay[loop] = (hours[loop] - 40)*(overtimerate*rate[loop]);
                grosspay[loop] = ((hours[loop] - 40)*(rate[loop] * overtimerate)) + (40 * rate[loop]);
                taxpay[loop] = .2*((hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop]));
                netpay[loop] = (hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop]) - (.2*((hours[loop] - 40)*(rate[loop] * overtimerate) + (40 * rate[loop])));
            }
            else {
                basepay[loop] = rate[loop] * hours[loop];
                taxpay[loop] = .2*(rate[loop] * hours[loop]); // again, define 0.20 as a TAX_RATE constant or some such..
                netpay[loop] = (rate[loop] * hours[loop]) - (.2*(rate[loop] * hours[loop]));
                overtimepay[loop] = 0.0; // dont forget to initialize this, since it is printed later!
                // whoops, forget to set grosspay? Also, whats the difference b/w base pay and gross pay here?
                // whoops, forget to set anything else? How about calculate employee's non-overtime pay in all cases, then adjust for overtime in case hours > 40?
            }
        }
    
        //output
        for (int loop = 0; loop < num_employees; loop++) { // only do output for employees actually entered by user
          printf("Employee: %s\n", name[loop]);
          printf("Hourly rate: %.2f\n", rate[loop]);
          printf("Hours worked: %.2f\n", hours[loop]);
          printf("Base pay: %.2f\n", basepay[loop]);
          printf("Overtime pay: %.2f\n", overtimepay[loop]);
          printf("Your paycheck is: %.2f\n", grosspay[loop]);
          printf("Taxes paid:%.2f\n", taxpay[loop]);
          printf("Money taken home: %.2f\n", netpay[loop]);
        } // whoops, had this bracket in the wrong spot again -- always printed last employee's data!
        
        //system("pause"); // never use this!
     
     
        return 0;
    }
    I have taken some time to go over the code and fix it to some extent, and I added comments for explanation. I did find quite a few bugs in here, not all of which are fixed, but as it was, the code did not even compile and there were some gross logical errors (which is OK and expected for a beginner), but also I found some more subtle bugs (such as the wrong usage of printf formatting specifiers). I hope this will put you on the right track..
    Last edited by MacNilly; 07-16-2016 at 05:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. beginner help
    By mouse666666 in forum C++ Programming
    Replies: 5
    Last Post: 09-27-2010, 05:07 AM
  3. Another beginner 'n when to use while vs if
    By lastrial in forum C Programming
    Replies: 4
    Last Post: 05-14-2007, 12:43 AM
  4. Help for beginner please!
    By sflyers in forum C++ Programming
    Replies: 12
    Last Post: 01-10-2004, 09:10 PM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

Tags for this Thread