/* This program lets the user add employee records to * the end of the file "employee.dat". Entry stops when * an empty employee name is entered. * * Author: Evan Weaver Last Modified: 25-Nov-1996 */ #include #include int enter_employee(char name[], char job[], double *psal); main() { FILE *fp; char name[36], job[41]; double salary; fp = fopen("employee.dat", "a"); if (fp == NULL) printf("Cannot open the employee.dat file\n"); else { while (enter_employee(name, job, &salary)) fprintf(fp, "%s;%s;%.2lf\n", name, job, salary); fclose(fp); } } /* end of main program */ /* Lets the user enter data for an employee. If no name * is entered, no more data is asked for and a false value * is returned. Otherwise, a true value is returned after * placing the user's input into the variables pointed * to by the parameters. */ int enter_employee(char name[], char title[], double *psal) { int ok = 0; /* false */ printf("Enter employee name (or nothing to stop): "); gets(name); if (strcmp(name, "")) { printf("Enter %s's job title: ", name); gets(title); printf("Enter %s's salary: ", name); scanf("%lf", psal); /* no & since psal is a pointer! */ while (getchar() != '\n') ; /* discard junk entered after the number */ ok = 1; /* true */ } return ok; } /* end of enter_employee() function */