My final data does not display

This is a discussion on My final data does not display within the C Programming forums, part of the General Programming Boards category; My final data does not display. I must being passing the pointers incorrectly to the subsequent functions. Can anyone help? ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Question My final data does not display

    My final data does not display. I must being passing the pointers incorrectly to the subsequent functions. Can anyone help? I didn't want to list all my code but felt it was necessary for clarity.





    code:--------------------------------------------------------------------------------
    #include <stdio.h>
    #include <strings.h>
    #include <stdlib.h>

    #define total_employees 5
    #define over_time_rate 1.5
    #define forty_hour_week 40

    struct s_employee
    {
    char name[20] ; /* user name */
    int clock_number ; /* clock # */
    float hourly_rate ; /* hourly rate of pay */
    float hours_worked ; /* hours worked */
    float overtime_hours ; /* OT hours worked */
    float gross_pay ; /* calc gross pay amount */
    float forty_hour_pay ; /* calc gross pay amount for 40 hours */
    float overtime_pay ; /* calc gross pay amount for time > 40 hours */

    struct s_employee *next_emp;
    };

    struct s_employee *emps;
    struct s_employee *emps2;
    struct s_employee *emps3;

    /************************************************** **********************/
    /* begin - Function: f_explain_messsage */
    /************************************************** **********************/
    /* */
    /* Purpose: explain what we will be processing */
    /* */
    /* Parameters: none */
    /* */
    /* Returns: Nothing, since we're only displaying an informational */
    /* message */
    /* */
    /************************************************** **********************/

    void f_explain_message (void)
    {

    printf ("\nWe will determine gross pay based on hours ");
    printf ("worked times rate of pay. Overtime will be calculated.");
    printf ("\nWe will process a total of five employees.\n\n") ;
    fflush(stdout);


    }

    /************************************************** **********************/
    /* end - Function: f_explain_message */
    /************************************************** **********************/




    /************************************************** **********************/
    /* begin - Function: f_input1 */
    /************************************************** **********************/
    /* */
    /* Purpose: Obtains input from user */
    /* */
    /* Parameters: emp1 - ptr to linked list */
    /* */
    /* Returns: Nothing */
    /* */
    /************************************************** **********************/

    struct s_employee *f_input1 (void)

    {

    /* local variable declaration */
    int f_counter = 0 ;
    int f_clock_number = 0 ;
    char f_first_name [20] = "" ;
    char f_last_name [20] = "" ;
    float f_hourly_rate = 0 ;


    /* misc pointers */
    struct s_employee *current_pointer, *initial_pointer;


    /* null pointers */
    initial_pointer = NULL;
    current_pointer = NULL;


    /* populate struct */
    while ( f_counter < total_employees )


    {

    /* allocate memory */
    struct s_employee *emp = malloc( sizeof(*emp) );


    /* Prompt for user to input their first name / last name */
    /* concat first/last name and move to array struct, copy string */
    printf ("Please enter the first name: ");
    scanf ("%s", f_first_name);

    printf ("Please enter the last name: ");
    scanf ("%s", f_last_name);

    strcat (f_first_name, " ");
    strcat (f_first_name, f_last_name);
    strcpy(emp->name, f_first_name);


    /* Prompt for user to input the clock # */
    printf ("Please enter the clock number: ") ;
    scanf ("%i", &emp->clock_number);


    /* Prompt for user to input the hourly rate of pay */
    printf ("Please enter the hourly rate: ") ;
    scanf ("%f", &emp->hourly_rate);
    printf("\n");
    emp->next_emp = NULL;

    if ( initial_pointer == NULL )
    {
    /* list empty, this is where it starts */
    initial_pointer = emp;
    }
    else
    /* non-empty list, append to the end of the list */
    {
    current_pointer->next_emp = emp;
    }

    /* process next_emp */
    current_pointer = emp;
    ++f_counter;

    }

    /* return pointer to the start of the list */
    return initial_pointer;


    }

    /************************************************** **********************/
    /* end - Function: f_input1 */
    /************************************************** **********************/

    / **************************************************
    **********************/
    /* begin - Function: f_input2 */
    / **************************************************
    **********************/
    /* */
    /* Purpose: Obtains hours worked from user */
    /* */
    /* Parameters: emps - ptr to linked list */
    /* */
    /* Returns: original pointer */
    /* */
    / **************************************************
    **********************/




    struct s_employee *f_input2 (struct s_employee *emps)


    {

    /* misc pointers */
    struct s_employee *tmp = emps;



    while ( tmp != NULL )

    {

    printf("Please enter the total hours worked by %s: ", tmp->name );
    scanf("%f", &tmp->hours_worked);
    printf("\n");
    tmp = tmp->next_emp;

    }

    /* return pointer to the start of the list */
    return emps;


    }



    / **************************************************
    **********************/
    /* end - Function: f_input2 */
    / **************************************************
    **********************/


    / **************************************************
    **********************/
    /* begin - Function: f_gross_pay_calc */
    / **************************************************
    **********************/
    /* */
    /* Purpose: calculate gross pay */
    /* */
    /* Parameters: emps2 - ptr to linked list */
    /* */
    /* Returns: initial pointer */
    /* */
    / **************************************************
    **********************/

    struct s_employee *f_gross_pay_calc (struct s_employee *emps2)

    {

    struct s_employee *tmp = emps2;


    while ( tmp != NULL )

    {

    / **************************************************
    **********************/
    /* If hours worked <= 40 */
    /* Calculate gross pay by multiplying hours worked by the hourly rate */
    /* overtime hours is set to 0 */
    / **************************************************
    **********************/
    if (tmp->hours_worked <= forty_hour_week)

    {


    tmp->gross_pay = (tmp->hours_worked * tmp->hourly_rate),

    (tmp->overtime_hours = 0) ;

    }

    else {
    / **************************************************
    **********************/
    /* else If hours worked > 40 */
    /* calculate pay for 40 hours worked */
    /* calculate overtime hours = hours worked minus 40 */
    /* calculate overtime pay by multiplying hours hourly rate times 1.5 */
    /* and that result times overtime hours */
    /* and finally, calculate gross pay by adding 40 hour pay + overtime pay*/
    / **************************************************
    **********************/

    tmp->forty_hour_pay = (tmp->hourly_rate * forty_hour_week) ;

    tmp->overtime_hours = (tmp->hours_worked - forty_hour_week) ;

    tmp->overtime_pay =
    (tmp->overtime_hours * (tmp->hourly_rate * over_time_rate)) ;

    tmp->gross_pay =
    (tmp->forty_hour_pay + tmp->overtime_pay) ;

    }

    tmp = tmp->next_emp;

    }

    /* return pointer to the start of the list */
    return emps2;

    }

    / **************************************************
    **********************/
    /* end - Function: f_gross_pay_calc */
    / **************************************************
    **********************/




    / **************************************************
    **********************/
    /* begin - Function: f_print_headings */
    / **************************************************
    **********************/
    /* */
    /* Purpose: print rpt headings */
    /* */
    /* Parameters: none */
    /* */
    /* Returns: Nothing, since we're only displaying headers */
    /* */
    / **************************************************
    **********************/

    void f_print_headings (void)

    {

    printf ("\nName Clock Number Hours Worked OT Hours Hourly Rate Gross Pay \n") ;
    printf ("-------------- ------------ ------------ -------- ----------- --------- \n") ;


    }

    / **************************************************
    **********************/
    /* end - Function: f_print_headings */
    / **************************************************
    **********************/




    / **************************************************
    **********************/
    /* begin - Function: f_print_detail */
    / **************************************************
    **********************/
    /* */
    /* Purpose: prints all detail info to screen */
    /* */
    /* Parameters: emps3 - ptr to linked list */
    /* */
    /* Returns: Nothing */
    /* */
    / **************************************************
    **********************/

    void f_print_detail (struct s_employee *emps3)

    {

    struct s_employee *tmp = emps3;


    while ( tmp != NULL )


    {

    printf ("%-13s"" %06i %5.1f %4.1f $%6.2f $%7.2f\n",
    tmp->name,
    tmp->clock_number, tmp->hours_worked,
    tmp->overtime_hours, tmp->hourly_rate,
    tmp->gross_pay) ;


    tmp = tmp->next_emp;

    }

    printf ("\n\n");


    }


    / **************************************************
    **********************/
    /* end - Function: f_print_detail */
    / **************************************************
    **********************/


    main()
    {

    /* create a linked list */
    struct s_employee emp1, emp2, emp3, emp4, emp5;

    emp1.next_emp = &emp2;
    emp2.next_emp = &emp3;
    emp3.next_emp = &emp4;
    emp4.next_emp = &emp5;
    emp5.next_emp = ( struct s_employee *) 0;




    /********************** begin performed functions ***********************/

    /* Explain what we will be processing */
    f_explain_message ();


    /* perform get misc function */
    emps = f_input1();


    /* perform get hours function */
    f_input2(emps);


    /* perform grosspay calc function */
    f_gross_pay_calc (emps2);


    /* perform print headings function */
    f_print_headings ();


    /* perform Print employee information to the screen function */
    f_print_detail (emps3);


    /********************** end performed functions *************************/




    }

    [/code]

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    you must start code segments with [code] and end them with [/code]

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    Lots of BUGS in your code.
    Code:
    #include <stdio.h> 
    #include <strings.h>
    #include <stdlib.h>
    
    #define total_employees 2
    #define over_time_rate 1.5
    #define forty_hour_week 40
    
    struct s_employee
    {
        char name[20] ; /* user name */ 
        int clock_number ; /* clock # */
        float hourly_rate ; /* hourly rate of pay */
        float hours_worked ; /* hours worked */ 
        float overtime_hours ; /* OT hours worked */
        float gross_pay ; /* calc gross pay amount */
        float forty_hour_pay ; /* calc gross pay amount for 40 hours */
        float overtime_pay ; /* calc gross pay amount for time > 40 hours */
    
        struct s_employee *next_emp; 
    };
    //changed
    struct s_employee *emps; 
    struct s_employee *emps2;                      // why ?
    struct s_employee *emps3; 
    
    
    void f_explain_message (void)
    {
    
        printf ("\nWe will determine gross pay based on hours ");
        printf ("worked times rate of pay. Overtime will be calculated.");
        printf ("\nWe will process a total of five employees.\n\n") ; 
        fflush(stdout); 
    }
    
    
    struct s_employee *f_input1 (void)
    {
        
    /* local variable declaration */ 
        int f_counter = 0 ; 
        int f_clock_number = 0 ; 
        char f_first_name [20] = "" ;
        char f_last_name [20] = "" ;
        float f_hourly_rate = 0 ; 
     
    
    /* misc pointers */
    struct s_employee *current_pointer, *initial_pointer;   
     
     
    /* null pointers */
        initial_pointer = NULL;
        current_pointer = NULL;
     
     
    /* populate struct */
        while ( f_counter < total_employees ) 
        { 
    
    /* allocate memory */
            struct s_employee *emp = malloc( sizeof(struct s_employee) );   // CHANGED to THIS STATEMENT
     
     
    /* Prompt for user to input their first name / last name */
    /* concat first/last name and move to array struct, copy string */
            printf ("Please enter the first name: ");
            scanf ("%s", f_first_name);
            
            printf ("Please enter the last name: "); 
            scanf ("%s", f_last_name);
    
            strcat (f_first_name, " ");
            strcat (f_first_name, f_last_name); 
            strcpy(emp->name, f_first_name); 
     
    
    /* Prompt for user to input the clock # */
            printf ("Please enter the clock number: ") ; 
            scanf ("%i", &emp->clock_number);
     
    
    /* Prompt for user to input the hourly rate of pay */
            printf ("Please enter the hourly rate: ") ; 
            scanf ("%f", &emp->hourly_rate);
            printf("\n");
            emp->next_emp = NULL;
            
            if ( initial_pointer == NULL )
            {
    /* list empty, this is where it starts */
                initial_pointer = emp;
            } 
            else 
    /* non-empty list, append to the end of the list */
            {
                current_pointer->next_emp = emp;
            } 
    
    /* process next_emp */
            current_pointer = emp;
            ++f_counter;
    
        } 
    
    /* return pointer to the start of the list */
        return initial_pointer;
    
    
    } 
    
    struct s_employee *f_input2 (struct s_employee *emps) 
    {
    
    /* misc pointers */
        struct s_employee *tmp = emps;
        
    
    
        while ( tmp != NULL )
    
        {
    
            printf("Please enter the total hours worked by %s: ", tmp->name );
            scanf("%f", &tmp->hours_worked);
            printf("\n");
            tmp = tmp->next_emp;
            
        }
    
    /* return pointer to the start of the list */
        return emps;              // not necessary WHY ?
    
    
    }
    
    
    
    struct s_employee *f_gross_pay_calc (struct s_employee *emps2)
    
    {
    
        struct s_employee *tmp = emps2;
    
    
        while ( tmp != NULL ) 
    
        { 
    
            if (tmp->hours_worked <= forty_hour_week)
    
            {
    
    
                tmp->gross_pay = (tmp->hours_worked * tmp->hourly_rate), 
    
                    (tmp->overtime_hours = 0) ; 
                
            } 
    
            else {
                
                tmp->forty_hour_pay = (tmp->hourly_rate * forty_hour_week) ; 
    
                tmp->overtime_hours = (tmp->hours_worked - forty_hour_week) ; 
    
                tmp->overtime_pay =
                    (tmp->overtime_hours * (tmp->hourly_rate * over_time_rate)) ; 
                
                tmp->gross_pay = 
                    (tmp->forty_hour_pay + tmp->overtime_pay) ; 
                
            }
            
            tmp = tmp->next_emp;
            
        } 
        
    /* return pointer to the start of the list */
        return emps2;
    
    } 
    
    void f_print_headings (void)
    
    {
    
        printf ("\nName Clock Number Hours Worked OT Hours Hourly Rate Gross Pay \n") ; 
        printf ("-------------- ------------ ------------ -------- ----------- --------- \n") ; 
     
     
    } 
    
    
    
    
    void f_print_detail (struct s_employee *emps3)
    
    {
    
        struct s_employee *tmp = emps3;
    
    
        while ( tmp != NULL ) 
    
    
        { 
    
            printf ("%-13s"" %06i %5.1f %4.1f $%6.2f $%7.2f\n",
                    tmp->name, 
                    tmp->clock_number, tmp->hours_worked, 
                    tmp->overtime_hours, tmp->hourly_rate, 
                    tmp->gross_pay) ;
    
    
            tmp = tmp->next_emp;
    
        }
    
        printf ("\n\n"); 
    
    
    }
    
    
    
    main()
    {
    
    /* create a linked list */ 
    /*    struct s_employee emp1, emp2, emp3, emp4, emp5;
    
        emp1.next_emp = &emp2; 
        emp2.next_emp = &emp3; 
        emp3.next_emp = &emp4; 
        emp4.next_emp = &emp5; 
        emp5.next_emp = ( struct s_employee *) 0; 
    
    */
    
    
    /********************** begin performed functions ***********************/
    
    /* Explain what we will be processing */
        f_explain_message (); 
    
    
    /* perform get misc function */
        emps = f_input1();
    
    
    /* perform get hours function */ 
        f_input2(emps);
    
    
    /* perform grosspay calc function */
         f_gross_pay_calc (emps2) ;   // why EMPS2
    
    
    /* perform print headings function */ 
        f_print_headings ();
    
    
    /* perform Print employee information to the screen function */ 
        f_print_detail (emps);  // WHY EMPS3
    
    /********************** end performed functions *************************/
     
    }
    1. Use all caps for Macro definitions. It is easier to read.
    Please Explain what you want to accomplish with these fragments of code,
    1.
    Code:
        emp1.next_emp = &emp2; 
        emp2.next_emp = &emp3; 
        emp3.next_emp = &emp4; 
        emp4.next_emp = &emp5; 
        emp5.next_emp = ( struct s_employee *) 0;
    Why do you want to create a linked list like this, when you are already making one dynamically ?
    2.
    Code:
           f_print_detail (emps);  // WHY EMPS3
    The biggest BUG is this one. The head of your linked list is "emps". But you are passing somethig else , i.e. emps3

    3.
    Code:
    struct s_employee *emps; 
    struct s_employee *emps2;                      // why ?
    struct s_employee *emps3;
    what is the purpose of these variables ?

    4.
    Code:
    struct s_employee *emp = malloc( sizeof(*emp) );
    struct s_employee *emp = malloc( sizeof(struct s_employee) );
    Malloc wants the number of bytes that need to be allocated. The sizeof operator will accept a data type and return the number of bytes required for that data type.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Thumbs up

    RoshanX,

    Thank you so much for enlightening me. It works like a charm now.

    Forever greatful,

    P1C1078

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 11:47 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM

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