Like Tree1Likes

Help with symbolic constants

This is a discussion on Help with symbolic constants within the C Programming forums, part of the General Programming Boards category; Below is my code for a beginning type project. I am asking for help with defining constants. What other constants ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    22

    Help with symbolic constants

    Below is my code for a beginning type project. I am asking for help with defining constants. What other constants can I include or what variables can I change to constants. I have muddled over this for a while and would like some input from my fellow c programmers. Also, this is a project that was to be written within the confines of the first few chapters of a text book. Therefor, there is no advanced code included in my code. So please do not post any other ways in which I could have written it more efficiently unless it directly relates to defining constants.

    Thanks for your Help!!!

    Code:
    #include <stdio.h>
    
    #define STD_HOURS 40.0    /*standard hours per work week */
    
    
    int main(void)
    {
        long clock_num;                /* employee clock number */
        int counter;                    /* counter to tell for loop where to begin and end */
        float gross;                    /* weekly gross pay (wage * hours) */
        float hours;                    /* number of hours worked per week */
        int num_of_employees;    /* query user for number of employees */
        float overtime = 0.0;        /* overtime hours worked initialized to 0.0 */
        float overtime_pay;            /* overtime pay is paid as time and a half (overtime * ( wage + (wage/2))) */
        float wage;                    /* hourly wage */
    
        /* Prompt explaining program and asking for user input */
        printf("This is a program to calculate weekly gross pay.\n");
        printf("You will now be prompted to enter employee data.\n\n");
        printf("Please enter the number of employees to calculate weekly gross pay for: \n");
        scanf("%i", &num_of_employees);
    
    
        /* for loop to cycle through the number of employees and gather employee information */
        for (counter = 1; counter <= num_of_employees; counter++)
        {
            printf("\n\nEnter clock number for employee: ");
            scanf("%li", &clock_num);
            printf("Enter hourly wage for employee: ");
            scanf("%f", &wage);
            printf("Enter the number of hours the employee worked: ");
            scanf("%f", &hours);
    
            if (hours > STD_HOURS)
                {
                    overtime = hours - STD_HOURS;
                    overtime_pay = overtime * (wage + (wage / 2));
                    gross = wage * STD_HOURS + overtime_pay;        /*calculate gross pay to include overtime */
                }
            else
            {
                gross = wage * hours;         /* calculate gross pay if condition is false */
            }
    
    
            /* Create table to display employee information */
            printf("\n------------------------------------------------\n");
            printf("Clock#      Wage     Hours     OT      Gross\n");
            printf("------------------------------------------------\n\n");
    
            /* Print employee information into created table */
            printf("%06li     %5.2f    %5.1f   %5.1f    %7.2f\n", clock_num, wage, hours, overtime,  gross);
        }
    
    getchar();        /* Get character before exit */
    return(0);        /* success */
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    Looks alright; every other value in the program seems to change depending on other data, so I don't see any clear need for more symbolic constants.

    So please do not post any other ways in which I could have written it more efficiently unless it directly relates to defining constants
    Are you sure? Can we tell you about the bugs we see?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How about TimeAndHalfPerWk for overtime between 40 and 60 hours, and DoubleTimePerWk for hours over 60 in a week.

    Also, maybe TimeAndHalfPerDay for hours > 8 in any day and < 12 hours, and DoubleTimePerDay for hours > 12 in any day or any designated holiday.

    I see them as defines at the top of the program, for easy changes if the assignment or work contract changes.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    As part of my code requirements I am suppose to define at least one more constant but for the life of me I cannot figure out where in my code I would. Go ahead I guess, Pick my code apart but please keep it simple.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    This is more of an observation than "picking apart" (I'm eating pizza and don't want to lose focus on that while digging through the code).

    The way that this is coded, you should re-initialize the variables each time the loop executes (or within the body of the "else" statement).

    If the first employee works over STD_HOURS, then a value for "overtime" is calculated. This value is printed out to the screen.

    If the second employee works under STD_HOURS, then the value for "overtime" is not calculated - and is never updated. When it prints the information for the second employee, it prints the wrong value under the "overtime" column.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    C programmers prefer to write the loop like this
    for (counter = 0; counter < num_of_employees; counter++)
    I suggest you do too, unless there's a reason to do it the other way.

    Also, %d is usually used instead of %i.

    For another constant, why not:
    Code:
    #define OVERTIME_RATE  1.5
    
    // and change this line to:
    overtime_pay = overtime * wage * OVERTIME_RATE;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    You could define a maximum number of employees, and check to make sure this value is not exceed when taking input.

    [edit] oogabooga's idea is much better than this one!

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Quote Originally Posted by oogabooga View Post
    C programmers prefer to write the loop like this
    for (counter = 0; counter < num_of_employees; counter++)
    I suggest you do too, unless there's a reason to do it the other way.

    Also, %d is usually used instead of %i.

    For another constant, why not:
    Code:
    #define OVERTIME_RATE  1.5
    
    // and change this line to:
    overtime_pay = overtime * wage * OVERTIME_RATE;
    Fantastic idea- I didn't even think of this as I wanted to make it harder for myself. Also, I realize %i is base 8 but I didn't really think it mattered too much in this instance.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Quote Originally Posted by Matticus View Post
    This is more of an observation than "picking apart" (I'm eating pizza and don't want to lose focus on that while digging through the code).

    The way that this is coded, you should re-initialize the variables each time the loop executes (or within the body of the "else" statement).

    If the first employee works over STD_HOURS, then a value for "overtime" is calculated. This value is printed out to the screen.

    If the second employee works under STD_HOURS, then the value for "overtime" is not calculated - and is never updated. When it prints the information for the second employee, it prints the wrong value under the "overtime" column.
    I guess I don't understand how to do what you are saying. Each time I compile and run the code I get my desired result. Please Expound

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    Run the program and enter "2" for number of employees.
    Give the first employee 50 hours. You will see 10 in the "overtime" column.
    Give the second employee 20 hours. You will still see 10 in the "overtime" column.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Quote Originally Posted by Matticus View Post
    Run the program and enter "2" for number of employees.
    Give the first employee 50 hours. You will see 10 in the "overtime" column.
    Give the second employee 20 hours. You will still see 10 in the "overtime" column.

    ooooo... you are right. How would I go about re initializing overtime? Please forgive me as this is for my first ever programming class and I have only been programming a few weeks.

    Would I just reinitialize it in the beginning of the for loop?
    Last edited by rpmischris; 09-17-2012 at 07:08 PM.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    Code:
    // If this is true, you update three variables
    if (hours > STD_HOURS)
    {
        overtime = hours - STD_HOURS;
        overtime_pay = overtime * (wage + (wage / 2));
        gross = wage * STD_HOURS + overtime_pay;        /*calculate gross pay to include overtime */
    }
    
    // Otherwise ... you only update one variable here
    else
    {
        gross = wage * hours;         /* calculate gross pay if condition is false */
    }
    It might be a good idea to update the same variables for each possible outcome.
    I hope that was clear!

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    Would I just reinitialize it in the beginning of the for loop?
    That is another solution, sure!

    [edit]

    My solution might make the code easier to follow:

    - If one outcome, update the three variables accordingly
    - If another outcome, update the three variables accordingly
    Last edited by Matticus; 09-17-2012 at 07:12 PM.

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Thanks for pointing this out Matticus!!!

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Quote Originally Posted by Matticus View Post
    Code:
    // If this is true, you update three variables
    if (hours > STD_HOURS)
    {
        overtime = hours - STD_HOURS;
        overtime_pay = overtime * (wage + (wage / 2));
        gross = wage * STD_HOURS + overtime_pay;        /*calculate gross pay to include overtime */
    }
    
    // Otherwise ... you only update one variable here
    else
    {
        gross = wage * hours;         /* calculate gross pay if condition is false */
    }
    It might be a good idea to update the same variables for each possible outcome.
    I hope that was clear!
    I guess I am not quite following what you are saying. Anyway to make it more simpler? lol

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Symbolic vs Descriptive
    By audinue in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 12-10-2008, 04:26 PM
  2. is there a weird rule about symbolic constants?
    By i_can_do_this in forum C Programming
    Replies: 5
    Last Post: 07-10-2006, 07:14 AM
  3. Symbolic debugging
    By lyx in forum Windows Programming
    Replies: 1
    Last Post: 12-03-2003, 02:16 PM
  4. Help with Constants and enum constants. Newbie learning C++.
    By UnregJDiPerla in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:29 PM
  5. Symbolic Constants??
    By ACAC in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2001, 06:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21