By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,502 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,502 IT Pros & Developers. It's quick & easy.

Working with the for loop

P: n/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);

}
}

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Nov 15 '05 #2

P: n/a
On 13 Nov 2005 18:00:46 -0800, "AR" <ba******@aol.com> 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>>
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.