# Thread: For Loop - Need desparate help!

1. ## For Loop - Need desparate help!

I have been struggling to get this to work and need help deperately - can someone take a look and help me?

#include <stdio.h>

main()
{
int num_employees, /* Number of employees */
response, /* Response of 1 Yes for 0 for No */
upper_limit; /* tests # of employees entered */
double wage_rate, /* Hourly Pay Rate */
hours_worked, /* Hours worked */
weekly_pay, /* Weekly pay for employee */
total_weekly_pay; /* Total weekly pay for employees */

/* Ask for an employee */

printf("\nDo you want to process an employee? ");
printf("\nEnter 1 for Yes or 0 for No: ");
scanf("\n%d", &response);

printf("\nPlease enter wage rate for current employee: ");
scanf("%lf", &wage_rate);
printf("\nPlease enter number of hours worked: ");
scanf("%lf", &hours_worked);

/* For Loop, Calculate and Display */

for (num_employees = 1; num_employees <= upper_limit; ++num_employees)
{
weekly_pay = wage_rate * hours_worked;
printf("\nWeekly Pay for this Employee: %.2f\n",weekly_pay);

total_weekly_pay += weekly_pay;
}

/* Ask for another employee */

printf("\nDo you want to process another employee? ");
printf("\nEnter 1 for Yes or 0 for No: ");
scanf("\n%d", &response);

printf("\nThe Total Weekly Pay for %d employees is %.2f\n\n",
num_employees, total_weekly_pay);
}

The problem I am having is that it totally skips the loop.

Thanks!

2. It may be skipping the loop becuase upper_limit has never been initialized, but then again, it may not be.

A few other things I noticed:

Code:
``` printf("\nDo you want to process an employee? ");
printf("\nEnter 1 for Yes or 0 for No: ");
scanf("\n%d", &response);```
you never check to see if they even want to process any employees. A simple if will fix that (for both tests).

Other than that, the code seems fine to me, of course it's passed my bed time and I don't have my glasses on.

Another thing, should use int main(), instead of just plain old main(). The very last statment should be return0

3. for (num_employees = 1; num_employees <= upper_limit; ++num_employees)
Your problem is probably the fact that you never give "upper_limit" any value. What's "upper_limit" supposed to be?

[edit: ya beat me to it!]

Quzah.

4. I am having similar issues with for loops.
Wish I could help.

5. printf("\nDo you want to process another employee? ");
printf("\nEnter 1 for Yes or 0 for No: ");
scanf("\n%d", &response);

what happens here to response if the user enters 0 or 1 ...don't see any statements referring to response?

6. Code:
```#include "stdio.h"

#define YES    1
#define NO     0  // not used

int main()
{
int num_employees = 0, response;
double wage_rate, hours_worked, weekly_pay = 0, total_weekly_pay = 0;

while(true){
// Process an employee?
printf("\nDo you want to process an employee?[1/0] ");
scanf("%d", &response);

if(response == YES){
// wage rate?
printf("\nWage rate for current employee: \$");
scanf("%lf", &wage_rate);

// hours worked?
printf("Number of hours worked: ");
scanf("%lf", &hours_worked);

// calculate/display
weekly_pay = wage_rate * hours_worked;
printf("Weekly pay for current employee: \$%.2f\n",weekly_pay);

total_weekly_pay += weekly_pay; num_employees++;
}
else
break;
}

// display total pay for all employees
printf("\nThe Total Weekly Pay for %d employees is \$%.2f\n\n",
num_employees, total_weekly_pay);

return 0;
}```
try it.

7. I get the following error during compile:

d:\code\funcode\fcode.c(11) : error C2065: 'true' : undeclared identifier

8. true is not a C keyword - it's C++

Substitute the value 1 for true

So
while ( 1 )

9. ## Specifically: For Loop

knave or anyone else that wants to help:

your result works but I need to stick with the for loop because that is what they are requesting. Its actually a 3 part question. My While loop worked, the do-While is almost there but the for loop is driving me mad...

can you give me input on how I set it up with the for loop? I have now inicialized the upper_limit to 20 but now my output loops 21 times and goes straight to the "weekly total for 21 employees is: \$XXXX.XX.

Thanks...

10. Yup. Easy fix:

for( x = 0; x < 20; x++ )

Or...

for( x = 1; x < 21; x++ )

Your problem is that you're doing "<=" which means it also executes when it is 20 or 21 respectively.

Quick example:

for( x = 0; x <=1; x++) printf("I print twice!");

It prints once when x is zero, and once when x is one, because in both cases, x is LESS THAN OR EQUAL TO, not just LESS THAN.

Quzah.