Question: Please code in C /********************************************************************** Purpose: Grades.c This program reads and lists student grades for a term, with total GPA. Command Line Arguments: -t gradesFileName

Please code in C /********************************************************************** Purpose: Grades.c This program reads and lists student grades for a term, with total GPA. Command Line Arguments: -t gradesFileName Input: Stream input file which contains a list of student class grade reports, each containing the following lines (terminated by newline): - Student Identification Information: o One line per student(fields separated by a comma). name rank(freshman,sophomore,junior, senior) 31[^,] (may contain spaces) 10s - Term class grades (many records) o One grade record per class taken in the term(fields separated by a comma). class catalog name(CS1713) credits grade (0 - 100) 31[^,] 1d 2d Results: Prints each student's identification on first line, followed by the list of each class name and grade received (one per line), followed by the GPA for the term. Example: ******************* Student Grades and GPA for semester ****************** Pete Moss Junior course credits grade CS1713 3 98 CS1143 3 96 Semester GPA: 4.0 *********************** Returns: 0 normal -1 invalid command line syntax -2 show usage only -3 error during processing, see stderr for more information Notes: grades -? will provide the usage information. In some shells, you will have to type reserve -\? **********************************************************************/ #include  #include  #include  #include "grades.h" FILE *pFileGrades; // stream Input for Student grade data int main(int argc, char *argv[]) { char *pGradesFileName = NULL; // Process the command switches processCommandSwitches(argc, argv, &pGradesFileName); // open the Student grades stream data file if ( pGradesFileName == NULL) exitError(ERR_MISSING_SWITCH, "-c"); else { pFileGrades = fopen(pGradesFileName, "r"); if (pFileGrades == NULL) exitError(ERR_FILENAME, pGradesFileName); } // process student grades and GPA for semester term processGrades(); fclose(pFileGrades); return 0; } /******************** processGrades ***************************** void processGrades() Purpose: Reads, prints and processes student grade records Parameters: n/a Notes: pFileGrades must be open **************************************************************************/ void processGrades() { char szInputBuffer[100]; // input buffer for a line of text int iscanfCnt; // for returning sscanf success Student student; // student name and ranking Grade grade; // class grade and hours credit printf("**************** Student Grades and GPA for semester *********** "); /* WHAT YOU NEED TO DO: write all logic to read the Grades.txt file unitl EOF, printing the name of each student and their list of class grades and credits followed by calculating and printing the GPA for all classes taken in the term. Each student has a grade for each class taken and is terminated with the line END, 0, 0. (see format and layout of Grades.txt). Use fgets(...) to read each line from the file into a buffer, then use sscanf(...) to extract the data fields from the buffer using the correct formatting mode. Check that the number of conversions is correct and throw an error if not. */ } // end function processGrades /******************** processCommandSwitches ***************************** void processCommandSwitches(int argc, char *argv[], char **ppGradesFileName) Purpose: Checks the syntax of command line arguments and returns the filenames. If any switches are unknown, it exits with an error. Parameters: I int argc count of command line arguments I char *argv[] array of command line arguments O char *ppGradesFileName address of grades file (char pointer) name Notes: If a -? switch is passed, the usage is printed and the program exits with USAGE_ONLY. If a syntax error is encountered (e.g., unknown switch), the program prints a message to stderr and exits with ERR_COMMAND_LINE_SYNTAX. **************************************************************************/ void processCommandSwitches(int argc, char *argv[], char **ppGradesFileName) { int i; for (i = 1; i < argc; i++) { // check for a switch if (argv[i][0] != '-') exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]); // determine which switch it is switch (argv[i][1]) { case 'g': // Grades File Name if (++i >= argc) exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]); else *ppGradesFileName = argv[i]; break; case '?': exitUsage(USAGE_ONLY, "", ""); break; default: exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]); } } } /******************** exitError ***************************** void exitError(char *pszMessage, char *pszDiagnosticInfo) Purpose: Prints an error message and diagnostic to stderr. Exits with ERROR_PROCESSING. Parameters: I char *pszMessage error message to print I char *pszDiagnosticInfo supplemental diagnostic information Notes: This routine causes the program to exit. **************************************************************************/ void exitError(char *pszMessage, char *pszDiagnosticInfo) { fprintf(stderr, "Error: %s %s " , pszMessage , pszDiagnosticInfo); exit(ERROR_PROCESSING); } /******************** exitUsage ***************************** void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo) Purpose: If this is an argument error (iArg >= 0), it prints a formatted message showing which argument was in error, the specified message, and supplemental diagnostic information. It also shows the usage. It exits with ERR_COMMAND_LINE_SYNTAX. If this is just asking for usage (iArg will be -1), the usage is shown. It exits with USAGE_ONLY. Parameters: I int iArg command argument subscript I char *pszMessage error message to print I char *pszDiagnosticInfo supplemental diagnostic information Notes: This routine causes the program to exit. **************************************************************************/ void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo) { if (iArg >= 0) fprintf(stderr, "Error: bad argument #%d. %s %s " , iArg , pszMessage , pszDiagnosticInfo); fprintf(stderr, "grades -c gradesFileName "); if (iArg >= 0) exit(-1); else exit(-2); } 

Down below is the Grades.h file

/******************************************************************* grades.h Purpose: Defines typedefs for Student - full name, rank(freshman, sophomore, junior, senior) Grade  class name, credits, grade (0  100) Defines constants for boolean values error messages program return codes Prototypes Notes: ********************************************************************/ /**** typedefs ****/ // Student information typedef struct { char name[32]; // student full name char rank[10]; // student rank/year (junior, senior) } Student; // Grade typedef struct { char class[32]; // class name, e.g. CS1713 int credits; // course credit hours int grade; // class grade ( 0 - 100) } Grade; // boolean #define FALSE 0 #define TRUE 1 /**** constants ****/ // Maximum sizes #define ERR_MISSING_SWITCH "missing switch" #define ERR_EXPECTED_SWITCH "expected switch, found" #define ERR_MISSING_ARGUMENT "missing argument for" #define ERR_GRADES_FILENAME "invalid grades file name" #define ERR_STUDENT_DATA "invalid student info data" #define ERR_GRADES_DATA "invalid grade data" #define ERR_FILENAME "invalid file name" /* program return codes */ #define ERR_COMMAND_LINE_SYNTAX -1 // invalid command line syntax #define USAGE_ONLY -2 // show usage only #define ERROR_PROCESSING -3 /* prototypes */ void processGrades(); void processCommandSwitches(int argc, char *argv[], char **ppGradesFileName); void exitError(char *pszMessage, char *pszDiagnosticInfo); void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo); 

Down below is the Grades.txt info

 Pete Moss, Junior CS1713, 3, 98 CS1143, 3, 86 CS2123, 3, 92 END, 0, 0 John Doe, Senior CS1713, 3, 92 FIN3003, 3, 85 END, 0, 0 Jane Swift, Freshman PSY2533, 3, 96 END, 0, 0 David Scott, Senior MAT3233, 3, 90 END, 0, 0 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!