#include #include #define INSERT_LAB 1 #define SAVE 2 #define DONE 3 #define MAX_NAME_LENGTH 80 #define MAX_LABS 15 typedef struct{ char name[MAX_NAME_LENGTH]; int id; double labs[MAX_LABS]; int num_labs; } student_t; int get_choice(); int get_students(student_t students[], int *num_students); void print_students(const student_t students[], int num_students); int insert_lab(student_t students[], int num_students); int save_students(const student_t students[], int num_students); int main(void) { student_t students[40]; int num_students; int choice; int result = 0; result = get_students(students, &num_students); if(!result) { printf("There was a problem retrieving data...cannot continue.\n"); return (1); //exit with } print_students(students, num_students); choice = get_choice(); while(choice != DONE) { if(choice == INSERT_LAB) { result = insert_lab(students, num_students); } else if(choice == SAVE) { result = save_students(students, num_students); } else { result = 0; } if(!result) { printf("Your choice could not be correctly performed.\n"); } print_students(students, num_students); choice = get_choice(); } return (0); } int get_choice() { int choice; printf("You can perform the following operations:\n"); printf("\t1. Insert a lab score\n"); printf("\t2. Save current data\n"); printf("\t3. Quit the program\n"); printf("Enter your choice:"); scanf("%d", &choice); return choice; } int get_students(student_t students[], int *num_students) { char filename[50]; char trash[10]; FILE *infile; int result, i, j; char *status; printf("Enter the name of the data file: "); gets(filename); infile = fopen(filename, "r"); if(infile != NULL) { result = fscanf(infile, "%d", num_students); fgets(trash, 10, infile); if(result == EOF) { printf("Your file is corrupt.\n"); return (0); } for(i = 0; i < *num_students; i++) { status = fgets(students[i].name, 80, infile); if(status == NULL) { printf("Your file is corrupt.\n"); return (0); } if(students[i].name[strlen(students[i].name)-1] == '\n') { students[i].name[strlen(students[i].name)-1] = '\0'; } result = fscanf(infile, "%d", &students[i].id); fgets(trash, 10, infile); if(result == EOF) { printf("Your file is corrupt.\n"); return (0); } result = fscanf(infile, "%d", &students[i].num_labs); fgets(trash, 10, infile); if(result == EOF) { printf("Your file is corrupt.\n"); return (0); } for(j = 0; j < students[i].num_labs; j++) { result = fscanf(infile, "%lf", &students[i].labs[j]); if(result == EOF) { printf("Your file is corrupt.\n"); return (0); } } fgets(trash, 10, infile); } fclose(infile); return (1); } else { printf("The file %s could not be opened correctly.\n", filename); return (0); } } void print_students(const student_t students[], int num_students) { int i, j; printf("\n*****************************************************\n\n"); for(i = 0; i < num_students; i++) { printf("Student %d:\n", (i+1)); printf("\tName: %s\n", students[i].name); printf("\tID: %d\n", students[i].id); for(j = 0; j < students[i].num_labs; j++) { printf("\tLab %d: %lf\n", (j+1), students[i].labs[j]); } } printf("\n*****************************************************\n\n"); } int insert_lab(student_t students[], int num_students) { double new_lab_score = 1; int i; fflush(stdin); for(i = 0; i < num_students; i++) { if(students[i].num_labs < MAX_LABS) { do { if(new_lab_score < 0) { printf("Score must be greater than 0.\n"); } printf("Enter lab score for %s: ", students[i].name); scanf("%lf", &new_lab_score); } while(new_lab_score < 0); students[i].labs[students[i].num_labs++] = new_lab_score; } else { printf("Student %s has finished all labs.\n", students[i].name); } } return 1; } int save_students(const student_t students[], int num_students) { char filename[50]; //char trash[10]; FILE *outfile; int i, j; //char *status; fflush(stdin); printf("Enter the name of the data file: "); gets(filename); outfile = fopen(filename, "w"); if(outfile != NULL) { fprintf(outfile, "%d\n", num_students); for(i = 0; i < num_students; i++) { fprintf(outfile, "%s\n", students[i].name); fprintf(outfile, "%d\n", students[i].id); fprintf(outfile, "%d\n", students[i].num_labs); for(j = 0; j < students[i].num_labs; j++) { fprintf(outfile, "%lf\n", students[i].labs[j]); } } fclose(outfile); } else { printf("Cannot write to file %s.\n", filename); return 0; } printf("Data saved...\n"); return 1; }