The short answer is don't mix fgets with scanf.
Code:
char buff[BUFSIZ];
printf("Do you wish to enter data for a new worker ?\n");
printf("0 to exit; 1 to continue.\n");
fgets(buff, BUSIZ, stdin);
if ( buff[0] == '0')
break;
printf("Enter a surname for the %d° worker: ", i + 1);
fgets(ARRAY_WORKERS[i].surname, 40, stdin);
printf("Enter the number of years worked: ");
fgets(buff, BUSIZ, stdin);
sscanf(buff,"%d", &ARRAY_WORKERS[i].years_worked);
fgets(buff, BUSIZ, stdin);
printf("Enter the monthly salary (ex. 1350.55): ");
sscanf(buff,"%f", &ARRAY_WORKERS[i].salary);
Ideally, do this
Code:
if ( fgets(buff, BUSIZ, stdin) != NULL ) {
if ( sscanf(buff,"%d", &ARRAY_WORKERS[i].years_worked) == 1 ) {
The respective else clauses being
- error in conversion
- EOF or error in reading from stdin