Thread: Array with for loop

  1. #16
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Thank you everyone for their help i have got it done and now it is running correctly.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 5
    
    
    int main(void)
    {
        //Start of variables
        char name[SIZE][50]; //how many names comes first then max reserve characters
        float hourlyrate[SIZE]={0};
        float hoursworked[SIZE]={0};
        float grosspay[SIZE]={0};
        float basepay[SIZE]={0};
        float overtimepay[SIZE]={0};
        float taxespaid[SIZE]={0};
        float netpay[SIZE]={0};
        int i, j;
        //End of variables
        
        //Initialize variables
        j=0;
    
    
        for (i = 0; i < SIZE; i++){
        /*START OF RUN*/
            //Start of user input
            printf("Employee %d name:", i+1);
            scanf("%s", name[i]);
            printf("Enter hourly rate:");
            scanf("%f", &hourlyrate[i]);
            printf("Enter hours worked:");
            scanf("%f", &hoursworked[i]);
            printf("\n");
            if (hoursworked[i] <= -1){
                break;
            }
            if (hourlyrate[i] <= -1){
                break;
            }
            //End of user input
        }/* end of user for */
    
    
        printf("PAYMENT DATA \n \n");
    
    
        //Printing Out Results
    
    
        for(i = 0; i < SIZE; i++){
            //Start of equations
            if (hoursworked[i] <= -1){
                break;
            }
            if (hourlyrate[i] <= -1){
                break;
            }
            else if (hoursworked[i]>40){
            grosspay[i]=40*hourlyrate[i]+(hoursworked[i]-40)*(1.5*hourlyrate[i]);
            basepay[i]=40*hourlyrate[i];
            overtimepay[i]=(hoursworked[i]-40)*(1.5*hourlyrate[i]);
            }
            else if (hoursworked[i]<=40){
            grosspay[i]=hoursworked[i]*hourlyrate[i];
            basepay[i]=hoursworked[i]*hourlyrate[i];
            overtimepay[i]=0;
            }
            taxespaid[i]=grosspay[i]*0.2;
            netpay[i]=grosspay[i]-taxespaid[i];
            //End of equations
    
    
            //Start of calculated data
            /*use for-loop for char array*/
            printf("Pay to: ");
            for(j=0; name[i][j] != '\0'; j++){
                printf("%c", name[i][j]);
            }
            printf("\n");
            fflush(stdin);
            printf("Hourly rate:     $%.2f \n", hourlyrate[i]);
            printf("Hours worked:    $%.2f \n", hoursworked[i]);
            printf("Gross pay:       $%.2f \n", grosspay[i]);
            printf("Base pay:        $%.2f \n", basepay[i]);
            printf("Overtime pay:    $%.2f \n", overtimepay[i]);
            printf("Taxes paid:      $%.2f \n", taxespaid[i]);
            printf("Net paid:        $%.2f \n", netpay[i]);
            printf("----------------------------------\n");
            //End of calculated data
        }/* end of OUTPUT for */
        /*END OF RUN*/
        
        system("pause");
        return 0;
    }
    I don't think there is a need for flush though.
    Last edited by InfinityHacker; 07-14-2012 at 02:17 AM.

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by InfinityHacker View Post
    Thank you everyone for their help i have got it done and now it is running correctly.
    That's good to hear.

    Code:
    /*use for-loop for char array*/
    printf("Pay to: ");
    for(j=0; name[i][j] != '\0'; j++){
        printf("%c", name[i][j])
    }
    printf("\n");
    Usually you would print a string (a char array delimited by the null character '\0') using the %s format specifier (as you did with scanf)
    Code:
    printf("Pay to: %s\n", name[i]);
    I don't think there is a need for flush though.
    There's never a need for fflush(stdin) because its behavior is undefined.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with While Loop and array
    By Thinlizzy76 in forum C Programming
    Replies: 4
    Last Post: 11-14-2011, 02:27 PM
  2. While loop with array
    By Lego_TeCh in forum C Programming
    Replies: 3
    Last Post: 08-15-2009, 09:42 AM
  3. array and loop help
    By hockey1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 09:47 PM
  4. Help Array and Loop
    By Mike1982 in forum C Programming
    Replies: 5
    Last Post: 10-17-2007, 02:41 PM
  5. Need help on for loop and array
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2003, 10:17 AM

Tags for this Thread