void prompt(void) { student students[STUDENT_MAX]; int i; unsigned short count = 0; while (count < STUDENT_MAX && registerStudent(&students[count])) { count++; } sortStudents(students, count); for (i = 0; i < count; i++) { printStudent(students[i]); } }
int main(int argc, char * argv[]) { //checks to make sure that there are 2 arguments - one for input and output inputCheck(argv); //Get the input file name... char *inputFileName = *(argv + 1); char *outputFileName = *(argv + 2); //Find out how many students are on the input file... int numberOfStudents = getRecordCount(inputFileName); //Create the number of students depending on the record count... Student listOfStudents[numberOfStudents]; createStudents(listOfStudents, numberOfStudents); //Read the data of the input file... FILE *myFile = openFile(inputFileName); //Fill out all the student's info... fillStudentRecord(myFile, listOfStudents, numberOfStudents); //sortStudents using the array of student struct sortStudents(listOfStudents, numberOfStudents); //Calculate each student's GPA and recorded in the structure... getGPA(listOfStudents, numberOfStudents); //Calculate each student's Letter Grade and record in the structure... getGrade(listOfStudents, numberOfStudents); //Create ClassGrade structure pointer... ClassGrade *classGrade; classGrade = (ClassGrade *)malloc((sizeof classGrade) * 20); //Call functions to calculate the scores... getScoreAverage(listOfStudents, classGrade, numberOfStudents); getMinScore(listOfStudents, classGrade, numberOfStudents); getMaxScore(listOfStudents, classGrade, numberOfStudents); //Generate and output file with the student grade information generateOutputFile(outputFileName, inputFileName, listOfStudents, numberOfStudents); //Print out student's info... printAllStudents(listOfStudents, classGrade, numberOfStudents, inputFileName, outputFileName); return 0; }
int main(int argc, char *argv[]){ char inputFileName[100], outputFileName[100], c; FILE *inputFile, *outputFile = stdout;//Output is stdout by default int i, numStudents = 1, errorFlag = 0; if(argc == 1) { //Try to open a file from stdin. //If it doesn't work, keep trying or q to quit. do{ printf("Enter a file to read information from (or q to exit):"); scanf("%s", inputFileName); if(strcmp(inputFileName, "q") == 0){ printf("Terminating program..."); return 0; } else if((inputFile = fopen(inputFileName, "r")) == NULL) printf("Error: file not found\n"); }while(inputFile == NULL); } else if(argc == 2) { //Only the input file was given strcpy(inputFileName, argv[1]); if((inputFile = fopen(inputFileName, "r")) == NULL) { printf("ERROR: Invalid file name"); return 1; } } else if(argc == 3) { //Both input and output files were given strcpy(inputFileName, argv[1]); if((inputFile = fopen(inputFileName, "r")) == NULL) { printf("ERROR: Invalid input file name"); return 1; } strcpy(outputFileName, argv[2]); if((outputFile = fopen(outputFileName, "w")) == NULL) { printf("ERROR: Invalid output file name"); return 1; } } //Find how many students are here numStudents = getNumStudents(inputFile); //Create the array of students Student students[numStudents]; //Code to scan info in for(i = 0; i < numStudents; i++) { //1301,107515018,"Boatswain","Michael R.",CSE, 230,="R01" if(fscanf(inputFile, "%d,", &(students[i].term)) != 1) errorFlag = 1; if(fscanf(inputFile, "%d,", &(students[i].id)) != 1) errorFlag = 1; if(fscanf(inputFile, "\"%[^,\"]\",", students[i].lastName) != 1) errorFlag = 1; if(fscanf(inputFile, "\"%[^,\"]\",", students[i].firstName) != 1) errorFlag = 1; if(fscanf(inputFile, "%[^,],", students[i].subject) != 1) errorFlag = 1; if(fscanf(inputFile, "%d", &(students[i].catalogNumber)) != 1) errorFlag = 1; fscanf(inputFile, "%*[^=]="); if(fscanf(inputFile, "\"%[^\"]", students[i].section) != 1) errorFlag = 1; fscanf(inputFile, "%*[^\n]");//Reads out to the end of the line to avoid errors if(errorFlag) printf("WARNING: Unexpected or invalid input encountered on line %d!\nUnexpected results may occur...\n", i+1); errorFlag = 0; } //We don't need this anymore fclose(inputFile); //Print the output header printStudentHeader(outputFile); //Sort the students sortStudents(students, numStudents); //Print the students printStudents(students, numStudents, outputFile); return 0; }
/* * * * * * * * * * * * * * * * * * * * * * * * * Main Program: Display menu, get and run user choice. * * * * * * * * * * * * * * * * * * * * * * * * */ int main() { int choice = 0; // store user selection int quit = 0; // flag to end program // print the menu, loop until user quits do { printf("\n\nWelcome to STUDENTS!\n===================\n\n" "There are currently %d students in memory.\n\n" "1. Add New Student(s)\n" "2. Find Student by Last Name\n" "3. List All Students\n" "4. Sort Database\n" "5. Load Students File (./students.dat)\n" "6. Save Students File (./students.dat)\n" "7. Clear Memory (New Database)\n" "8. Quit\n\n", numStudents); // input the user selection choice = inputInteger("Choice: "); printf("\n"); // run the appropriate function based on user choice switch (choice) { case 1: // add addStudents(); break; case 2: // find findStudent(); break; case 3: // list listStudents(); break; case 4: // sort sortStudents(); break; case 5: // load if (numStudents == 0 || confirm("LOAD file and LOSE all students in memory (y/n) ? ")) loadStudents(); break; case 6: // save (only if students[] is not empty) if (numStudents == 0) printf("There are no students to save yet!\n"); else if (confirm("OVERWRITE the file (y/n) ? ")) saveStudents(); break; case 7: // new if (confirm("DELETE all students from memory (y/n) ? ")) clearStudents(); break; case 8: // quit default: // quit if (confirm("QUIT and LOSE all data in memory (y/n) ? ")) { printf("\nThank you and goodbye!\n\n"); quit = 1; } break; } } while (quit == 0); // keep repeating until user chooses to quit // quit = 1: end program successfully #ifdef _MSC_VER getchar(); getchar(); #endif return 0; }