Working with the for loop

A

AR

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);



}
}
 
O

Old Wolf

AR said:
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.
 
B

Barry Schwarz

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>>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top