Hi All,

I'm trying to figure out how I canFirst, 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
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:

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

Recommended Answers

All 6 Replies

Inside the for loop substitute the if for a do/while.

...
do {
        printf("Enter salary #%i: ",i);
        fflush(stdout);

        if (!(scanf("%d", &i) == 1)) {
            fprintf(stderr, "Error, you didn't enter a valid input");
            break;
        }
    }while( i < 0);
...

Thank you for taking the time of reading and using code tags. If you add a c to , next time it makes it specific to the C language.[code=c], next time it makes it specific to the C language.

Use continue instead so that you can enter the salary again...

i = 1;
	while(i <= numb_salaries){

		printf("Enter salary #%i: ",i);
		scanf ("%i", &salary);
		if(salary < 0){
			printf("\n Please enter a valid salary...\n Press enter to re-try...");
			fflush(NULL);
			getchar();
			continue;
		}
		sal_total = sal_total + salary;
		i++;
	}

Hey man...thanks so much, that worked like a charm

Please mark the thread as Solved...
Thank You...

Use continue instead so that you can enter the salary again...

i = 1;
	while(i <= numb_salaries){

		printf("Enter salary #%i: ",i);
		scanf ("%i", &salary);
		if(salary < 0){
			printf("\n Please enter a valid salary...\n Press enter to re-try...");
			fflush(NULL);
			getchar();
			continue;
		}
		sal_total = sal_total + salary;
		i++;
	}

Although the continue works in this situation, there are many times it won't work, so Aia's solution is better.

And what does fflush(NULL) do?

And what does fflush(NULL) do?

Flushes everything (like a damn good toilet :) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.