# Thread: Need assistance with loop and else if

1. ## Need assistance with loop and else if

Hi All,

I'm trying to figure out how I can test a condition (negative or positive number) and then have it go back into the for loop. What I'm trying to accomplish is if the user enters a negative number the user will get an error and then should be re-prompted for a positive number like this:

First, enter the number of salaries to process: 4

Now enter the 4 salaries to be averaged.

Enter salary #1: 10000
Enter salary #2: 8000
Enter salary #3: -20000
*** Invalid entry. Salary must be positive. ***
Enter salary #3: 25000
Enter salary #4: -3333
*** Invalid entry. Salary must be positive. ***
Enter salary #4: 52000

The Average of the 4 salaries entered is: \$ 23750.00

The Total of the 4 salaries entered is: \$ 95000.0

Any help would be very appreciated:

Code:
```#include <stdio.h>

int main(void)
{

int     i, salary;
int     numb_salaries = 0;
int     sal_total = 0;
float   total = 0;

printf("Welcome to the  Employee Calculator Program. \n\n");

printf("This program calculates the average and total of as ");
printf("many employee salaries as you wish to enter. \n\n");

printf("First, enter the number of salaries to process: ");
scanf ("%i", &numb_salaries);

printf("\nNow enter the salaries to be averaged. \n\n");

/*  Enter for loop  */

for (i = 1; i <=numb_salaries; ++i)
{

printf("Enter salary #%i: ",i);
scanf ("%i", &salary);
sal_total = sal_total + salary;

if ( salary < 0 )
salary = -salary;

printf("*** Invalid entry. Salary must be positive. *** ");

}

printf ("Thanks for using the program.\n\n");

getchar(); /* Pause output */

return (0);
}```

2. Make a flowchart from your code, then analyze it and you shall easily find your error.
And you didn't mention what you had problems with.
And just a FYI,
sal_total = sal_total + salary;
can be written as
sal_total += salary;

3. Ok, so within the loop you have:

Code:
```printf("Enter salary #&#37;i: ",i);
scanf ("%i", &salary);
sal_total = sal_total + salary;

if ( salary < 0 )
salary = -salary;

printf("*** Invalid entry. Salary must be positive. *** ");```
So you are saying:

Code:
```1. "Enter Salary #(loop iteration)"