Working with the for loop

Discussion in 'C Programming' started by AR, Nov 14, 2005.

  1. AR

    AR Guest

    Hi
    I am trying to figure out how to set up the conditions of my for loop
    in a nested loop program. I am missing some key element in either or
    both the syntax of the for loop and/or the placement of the for loop
    in my program. I could use some help.

    thanks

    PS I tried to only include the part of the program I needed help
    completing.




    void GetInfo(void)
    {


    {

    printf("\n\nMountain Pacific Corporation\n");
    printf("Department Salary Program\n\n");
    printf("Please enter the name of the Department: ");
    scanf("%s", department);

    for(count_EMP = 1; count_EMP > 1; count_EMP++)


    do {

    printf("\nEnter employee # %d: ", count_EMP);
    scanf("%s %s", &Fname, &Lname);

    printf("\nPlease enter the hourly wage for the employee: ");
    scanf("%f", &wage);

    printf("\nPlease enter the number of hours worked this week:
    ");
    scanf("%f", &hours);

    printf("\nThank you. Process another employee?");
    scanf("%s", &again);

    printf("End of processing\n\n\n");

    }while(again != 'Y' && again!= 'y');




    if (RegHr > 40)
    {
    OTHr = hours - 40;
    OTHrPay = OT * OTHr * wage;
    RegHrPay = 40.0 * wage;
    }

    else
    {
    RegHrPay = hours * wage;
    OTHrPay = 0.0;

    }

    GrossPay = RegHrPay + OTHrPay;


    strcpy(FULLNAME, Fname);
    strcat(FULLNAME, " ");
    strcat(FULLNAME, Lname);



    printf("\n\nMountain Pacific Corporation\n");
    printf("Department Salary Program\n\n");


    printf("Employee Reg Hrs "
    "Overtime Hrs Gross\n");

    printf("-----------------------------------------"
    "-----------------------------\n\n");


    printf("%-12s%9.1f%2c($%.2f)%12.1f%2c($%.2f)%c%15.2f\n\n",
    FULLNAME,
    hours, ' ', wage, OTHr, ' ', OTHrPay, ' ', GrossPay);



    }
    }
     
    AR, Nov 14, 2005
    #1
    1. Advertising

  2. AR

    Old Wolf Guest

    AR wrote:

    > Hi
    > I am trying to figure out how to set up the conditions of my for loop
    > in a nested loop program.
    > for(count_EMP = 1; count_EMP > 1; count_EMP++)


    Your loop body will never execute. It sets count_EMP to 1,
    and then checks and sees that it isn't >1, so the loop ends.
    Recall that for(A;B;C) means:
    A; while (B) { ...stuff...; C; }
    ie. it checks the loop condition before it does any of the
    stuff in the loop.

    I can't suggest a fix because I don't know when you wanted
    your loop to end.

    > do {
    >
    > printf("\nEnter employee # %d: ", count_EMP);
    > scanf("%s %s", &Fname, &Lname);
    >
    > printf("\nPlease enter the hourly wage for the employee: ");
    > scanf("%f", &wage);
    >
    > printf("\nPlease enter the number of hours worked this week:
    > ");
    > scanf("%f", &hours);


    *** CHECK THE RESULTS OF SCANF ***
    That goes for everybody in this group.
    If the user types in something that wasn't a valid float, then
    your scanf will fail and your program might write infinite amounts
    of crap to the screen until you press break, or if you're lucky,
    just go into another round of the loop.
    Don't you care ?

    >
    > printf("\nThank you. Process another employee?");
    > scanf("%s", &again);
    >
    > printf("End of processing\n\n\n");
    >
    > }while(again != 'Y' && again!= 'y');


    This will keep executing the do...while loop, with count_EMP = 1,
    until the person doesn't enter "y", at which point it will go back to
    the for-loop, set count_EMP to 2, and then enter the do...while loop
    again. There is no way to get out of the for loop. I doubt this is what
    you intended. How about replacing both loops with:

    count_EMP = 0;

    do {
    ++count_EMP;

    .... your code goes here ...

    } while (again != 'Y' && again != 'y');


    > strcpy(FULLNAME, Fname);
    > strcat(FULLNAME, " ");
    > strcat(FULLNAME, Lname);


    Please avoid using these functions if you can help it:

    snprintf(FULLNAME, sizeof FULLNAME, "%s %s", Fname, Lname);

    I'm assuming FULLNAME is an array of chars. This way you will
    get no buffer overflows.
     
    Old Wolf, Nov 14, 2005
    #2
    1. Advertising

  3. On 13 Nov 2005 18:00:46 -0800, "AR" <> wrote:

    snip

    How many different names are you using from this one email address and
    why are you posting the same code under different subjects?


    <<Remove the del for email>>
     
    Barry Schwarz, Nov 15, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,385
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,240
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    697
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    415
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page