| re: Working with the for loop
AR wrote:
[color=blue]
> 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++)[/color]
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.
[color=blue]
> 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);[/color]
*** 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 ?
[color=blue]
>
> printf("\nThank you. Process another employee?");
> scanf("%s", &again);
>
> printf("End of processing\n\n\n");
>
> }while(again != 'Y' && again!= 'y');[/color]
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');
[color=blue]
> strcpy(FULLNAME, Fname);
> strcat(FULLNAME, " ");
> strcat(FULLNAME, Lname);[/color]
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. |