Array with for loop

This is a discussion on Array with for loop within the C Programming forums, part of the General Programming Boards category; I have to do an assignment for class along these lines: Use a for loop to process the data for ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    7

    Question Array with for loop

    I have to do an assignment for class along these lines:

    Use a for loop to process the data for 5 employees. Use arrays to store the user input. In the for loop, if any of the entered fields are –1, break out of the loop.

    The program logic will first load all of the data, until the user enters the max number of records, or they input -1 for one of the fields. After the data is loaded, it will then be processed and output generated.

    I'm confuse on how to do the array which has to look similar to this:

    Enter name: Glenn
    Enter hourly rate: 2.00
    Enter hours worked: 50

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

    The teacher wants us to update our previous assignment. I'm not sure how to integrate. Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h> /* using this for getch */
    
    
    int main(void)
    {
        //Start of integers
        /* Ppl names */
        char name[20];
        /* Hourly rate */
        float hrr;
        /* Hours worked */
        float hrw;
        /* Amount paid */
        float ap;
        /* Taxes paid */
        float tp;
        /* Loop int */
        int looper;
        //End of integers
        
        for (looper = 0; looper < 5; looper++){
        /*START OF RUN*/
            //Start of user input
            printf("Employee name:");
            scanf("%s", &name);
            fflush(stdin);
            printf("Enter hourly rate:");
            scanf("%f", &hrr);
            printf("Enter hours worked:");
            scanf("%f", &hrw);
            printf("\n");
            //End of user input
        
            //Start of equations
            if (hrw>40){
            ap=40*hrr+(hrw-40)*(1.5*hrr);
            }
            else{
            ap=hrw*hrr;
            }
            tp=ap*0.2;
            //End of equations
    
    
            //Start of calculated data
            printf("Pay to: %s \n", name);
            printf("Hourly rate: %f \n", hrr);
            printf("Hours worked: %f \n", hrw);
            printf("Amount paid: %f \n", ap);
            printf("Taxes paid: %f \n", tp);
            //End of calculated data
        /*END OF RUN*/
    
    
        printf("----------------------------------\n");
        
        }
        
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
        //Start of integers
        /* Ppl names */
        char name[20];
        /* Hourly rate */
        float hrr;
        /* Hours worked */
        float hrw;
    Instead of using these three variables for all employees, you should exchange them for arrays and then adapt the rest of your code accordingly.

    BTW: your comment is misleading because none of them is an integer.

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    Just turn each variable for user input into an array of 5 (the "name" variable, since it contains strings, will have to be a multi-dimensional array).

    I'd also advise that you do all the user input in one "for()" loop, and the calculating/printing in another, so that you meet the requirements of your assignment ("After the data is loaded, it will then be processed and output generated.").

    You should also make your variables have more meaningful names, so the program is easier to read and you wouldn't need comments above them describing their function.

    Did you teacher instruct you to use "fflush(stdin)"? Because using "fflush()" to clear the input stream is undefined. See Why fflush(stdin) is wrong.

    Also, "conio.h" is not a standard 'C' library. You only seem to be using it for a "getch()" at the end of the problem. Better to use the standard "getchar()."

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Agreed with Matticus, but
    Just turn each variable for user input into an array of 5
    I would use a struct here, it's better coding practice and it causes less confusion, especially when you're already using arrays.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    I would use a struct here, it's better coding practice and it causes less confusion, especially when you're already using arrays.
    I agree with this, but I sense that the OP has not yet covered structures (though I could be wrong). But certainly good advice.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    I am a beginner so please explain thoroughly so I can gain your knowledge.

    Quote Originally Posted by Matticus View Post
    Just turn each variable for user input into an array of 5 (the "name" variable, since it contains strings, will have to be a multi-dimensional array).
    I have not been able to find a tutorial on this that explains it.
    I believe I get where memcpy is going though.

    Quote Originally Posted by Matticus View Post
    I'd also advise that you do all the user input in one "for()" loop, and the calculating/printing in another, so that you meet the requirements of your assignment ("After the data is loaded, it will then be processed and output generated.").
    There are two different equations for which i need. One has to be where the hours worked are under 40 and another which is over 40 which is differed to as overtime.

    Quote Originally Posted by Matticus View Post
    You should also make your variables have more meaningful names, so the program is easier to read and you wouldn't need comments above them describing their function.
    Then what name should I put for the variables.

    Quote Originally Posted by Matticus View Post
    Did you teacher instruct you to use "fflush(stdin)"? Because using "fflush()" to clear the input stream is undefined. See Why fflush(stdin) is wrong.
    Yes he instructed us to use fflush. I don't know how to flush the input buffer at all (its a bit confusing an explanation would be great).

    Quote Originally Posted by Matticus View Post
    Also, "conio.h" is not a standard 'C' library. You only seem to be using it for a "getch()" at the end of the problem. Better to use the standard "getchar()."
    I tried getchar and it won't work.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    I have not been able to find a tutorial on this that explains it. I believe I get where memcpy is going though.
    So you find structures to be an easier concept than arrays?

    There are two different equations for which i need. One has to be where the hours worked are under 40 and another which is over 40 which is differed to as overtime.
    Did you try separate variables?

    Code:
        float hoursUnder40;
        float hoursOver40;
        // what about 40 exactly?
    Then what name should I put for the variables.
    Maybe something like ...

    Code:
        float rateHourly;     /* Hourly rate */
        float hoursWorked;    /* Hours worked */
        float amountPaid;     /* Amount paid */
        float taxesPaid;      /* Taxes paid */
        int progLoop;         /* Loop int */
    Yes he instructed us to use fflush. I don't know how to flush the input buffer at all (its a bit confusing an explanation would be great).
    Did you read the link?

    I tried getchar and it won't work.
    I'd like to see that code; post it and maybe we can find out why this is.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Quote Originally Posted by Matticus View Post
    So you find structures to be an easier concept than arrays?



    Did you try separate variables?

    Code:
        float hoursUnder40;
        float hoursOver40;
        // what about 40 exactly?


    Maybe something like ...

    Code:
        float rateHourly;     /* Hourly rate */
        float hoursWorked;    /* Hours worked */
        float amountPaid;     /* Amount paid */
        float taxesPaid;      /* Taxes paid */
        int progLoop;         /* Loop int */


    Did you read the link?



    I'd like to see that code; post it and maybe we can find out why this is.
    Structures seem easier to work with than arrays.

    40 is the max number of hours one works before over time kicks in which is 1.2 times the regular pay. Everyones hourly pay is independent from each other.

    Will use those variables.

    I'm attaching my previous assignment with this I did not use for loop (told not to) so it looks a bit messy.

    Thanks for the help so far.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    Structures seem easier to work with than arrays.
    Then what will happen when you encounter an array within a structure? Or an array of structures?

    40 is the max number of hours one works before over time kicks in which is 1.2 times the regular pay. Everyones hourly pay is independent from each other.
    This was a technicality nit-pick on my part. Have one variable for fourty, and one for over fourty.

    I'm attaching my previous assignment with this I did not use for loop (told not to) so it looks a bit messy.
    Sorry, I don't read attachments (though others might). You might want to post the code for a better response. But I wish you well, and please ask questions here as you need to.

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    oh ill put my code here.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h> /* using this for getch */
    
    
    int main(void)
    {
        //Start of integers
        /* Ppl names */
        char name[20];
        /* Hourly rate */
        float hrr;
        /* Hours worked */
        float hrw;
        /* Amount paid */
        float ap;
        /* Taxes paid */
        float tp;
        /* Loop int */
        int looper;
        //End of integers
        
        for (looper = 0; looper < 5; looper++){
        /*START OF RUN*/
            //Start of user input
            printf("Employee name:");
            scanf("%s", &name);
            fflush(stdin);
            printf("Enter hourly rate:");
            scanf("%f", &hrr);
            printf("Enter hours worked:");
            scanf("%f", &hrw);
            printf("\n");
            //End of user input
        
            //Start of equations
            if (hrw>40){
            ap=40*hrr+(hrw-40)*(1.5*hrr);
            }
            else{
            ap=hrw*hrr;
            }
            tp=ap*0.2;
            //End of equations
    
    
            //Start of calculated data
            printf("Pay to: %s \n", name);
            printf("Hourly rate: %f \n", hrr);
            printf("Hours worked: %f \n", hrw);
            printf("Amount paid: %f \n", ap);
            printf("Taxes paid: %f \n", tp);
            //End of calculated data
        /*END OF RUN*/
    
    
        printf("----------------------------------\n");
        
        }
        
        getch();
        return 0;
    }
    can you also explain how to flush correctly?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    2,650
    oh ill put my code here.
    Um, this is what you already posted. I thought you had a previous assignment where you did not use a "for" loop.

    can you also explain how to flush correctly?
    If you're using "scanf()" then there are a few ways to avoid missing a "getchar()" without resorting to undefined behavior. But I myself would have to see the guilty code to offer suggestions.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,125
    This:
    Code:
    scanf("%s", &name);
    should be:
    Code:
    scanf("%s", name);
    since name will be converted to a pointer to its first element.

    As stated earlier, remove the fflush(stdin).

    Quote Originally Posted by InfinityHacker
    I tried getchar and it won't work.
    Quote Originally Posted by InfinityHacker
    can you also explain how to flush correctly?
    You don't need to "flush" the input buffer at all because you are only using scanf such that whitespace will be skipped anyway. The problem comes when you use scanf then use say, fgets, in which case you would need to cause the extra whitespace to be ignored.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    oh yea sorry
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        //Start of variables
        /* Ppl names */
        char name1[20];
        char name2[20];
        char name3[20];
        char name4[20];
        char name5[20];
        /* Hourly rate */
        float hrr1;
        float hrr2;
        float hrr3;
        float hrr4;
        float hrr5;
        /* Hours worked */
        float hrw1;
        float hrw2;
        float hrw3;
        float hrw4;
        float hrw5;
        /* Amount paid */
        float ap1;
        float ap2;
        float ap3;
        float ap4;
        float ap5;
        /* Taxes paid */
        float tp1;
        float tp2;
        float tp3;
        float tp4;
        float tp5;
        //End of variables
        
    /*START OF FIRST RUN*/
        //Start of user input
        printf("Employee name:");
        scanf("%s", &name1);
        fflush(stdin);
        printf("Enter hourly rate:");
        scanf("%f", &hrr1);
        printf("Enter hours worked:");
        scanf("%f", &hrw1);
        printf("\n");
        //End of user input
        
        //Start of equations
        if (hrw1>40){
        ap1=40*hrr1+(hrw1-40)*(1.5*hrr1);
        }
        else{
        ap1=hrw1*hrr1;
        }
        tp1=ap1*0.2;
        //End of equations
    
    
        //Start of calculated data
        printf("Pay to: %s \n", name1);
        printf("Hourly rate: %f \n", hrr1);
        printf("Hours worked: %f \n", hrw1);
        printf("Amount paid: %f \n", ap1);
        printf("Taxes paid: %f \n", tp1);
        //End of calculated data
    /*END OF FIRST RUN*/
    
    
        printf("----------------------------------\n");
    
    
    /*START OF SECOND RUN*/
        //Start of user input
        printf("Employee name:");
        scanf("%s", &name2);
        fflush(stdin);
        printf("Enter hourly rate:");
        scanf("%f", &hrr2);
        printf("Enter hours worked:");
        scanf("%f", &hrw2);
        printf("\n");
        //End of user input
        
        //Start of equations
        if (hrw2>40){
        ap2=40*hrr2+(hrw2-40)*(1.5*hrr2);
        }
        else{
        ap2=hrw2*hrr2;
        }
        tp2=ap2*0.2;
        //End of equations
    
    
        //Start of calculated data
        printf("Pay to: %s \n", name2);
        printf("Hourly rate: %f \n", hrr2);
        printf("Hours worked: %f \n", hrw2);
        printf("Amount paid: %f \n", ap2);
        printf("Taxes paid: %f \n", tp2);
        //End of calculated data
    /*END OF SECOND RUN*/
    
    
        printf("----------------------------------\n");
        
    /*START OF THIRD RUN*/
        //Start of user input
        printf("Employee name:");
        scanf("%s", &name3);
        fflush(stdin);
        printf("Enter hourly rate:");
        scanf("%f", &hrr3);
        printf("Enter hours worked:");
        scanf("%f", &hrw3);
        printf("\n");
        //End of user input
        
        //Start of equations
        if (hrw3>40){
        ap3=40*hrr3+(hrw3-40)*(1.5*hrr3);
        }
        else{
        ap3=hrw3*hrr3;
        }
        tp3=ap3*0.2;
        //End of equations
    
    
        //Start of calculated data
        printf("Pay to: %s \n", name3);
        printf("Hourly rate: %f \n", hrr3);
        printf("Hours worked: %f \n", hrw3);
        printf("Amount paid: %f \n", ap3);
        printf("Taxes paid: %f \n", tp3);
        //End of calculated data
    /*END OF THIRD RUN*/
    
    
        printf("----------------------------------\n");
        
    /*START OF FOURTH RUN*/
        //Start of user input
        printf("Employee name:");
        scanf("%s", &name4);
        fflush(stdin);
        printf("Enter hourly rate:");
        scanf("%f", &hrr4);
        printf("Enter hours worked:");
        scanf("%f", &hrw4);
        printf("\n");
        //End of user input
        
        //Start of equations
        if (hrw4>40){
        ap4=40*hrr4+(hrw4-40)*(1.5*hrr4);
        }
        else{
        ap4=hrw4*hrr4;
        }
        tp4=ap4*0.2;
        //End of equations
    
    
        //Start of calculated data
        printf("Pay to: %s \n", name4);
        printf("Hourly rate: %f \n", hrr4);
        printf("Hours worked: %f \n", hrw4);
        printf("Amount paid: %f \n", ap4);
        printf("Taxes paid: %f \n", tp4);
        //End of calculated data
    /*END OF FOURTH RUN*/
    
    
        printf("----------------------------------\n");
        
    /*START OF FIFTH RUN*/
        //Start of user input
        printf("Employee name:");
        scanf("%s", &name5);
        fflush(stdin);
        printf("Enter hourly rate:");
        scanf("%f", &hrr5);
        printf("Enter hours worked:");
        scanf("%f", &hrw5);
        printf("\n");
        //End of user input
        
        //Start of equations
        if (hrw5>40){
        ap5=40*hrr5+(hrw5-40)*(1.5*hrr5);
        }
        else{
        ap5=hrw5*hrr5;
        }
        tp5=ap5*0.2;
        //End of equations
    
    
        //Start of calculated data
        printf("Pay to: %s \n", name5);
        printf("Hourly rate: %f \n", hrr5);
        printf("Hours worked: %f \n", hrw5);
        printf("Amount paid: %f \n", ap5);
        printf("Taxes paid: %f \n", tp5);
        //End of calculated data
    /*END OF FIFTH RUN*/
    
    
        printf("----------------------------------\n");
        printf("\n");
        
        system("pause");
    
    
    }/* closing brace */
    I need help with fgets. its confusing.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,125
    Please read my post #12: we appear to have posted at almost the same time.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    I read that but i need a thorough explaination please and thanks.

Page 1 of 2 12 LastLast
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, 10:42 AM
  3. array and loop help
    By hockey1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 10:47 PM
  4. Help Array and Loop
    By Mike1982 in forum C Programming
    Replies: 5
    Last Post: 10-17-2007, 03:41 PM
  5. Need help on for loop and array
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2003, 11:17 AM

Tags for this Thread


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