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