Nathan J.
asked 10/02/14Create a program that uses a menu with options to enter student information (name, ID, GPA),
1 Expert Answer
Patrick B. answered 04/21/20
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STUDENT_NAME_LEN (255)
#define STUDENT_ID_LEN (55)
#define STUDENT_REC_FILENAME ("studentrecs.dat")
typedef struct _TStudentRec
{
char studentLastName[STUDENT_NAME_LEN];
char studentFirstName[STUDENT_NAME_LEN];
char studentID[STUDENT_ID_LEN];
double GPA;
} * TStudentRec;
char * SerializeStudentRecToCSV(TStudentRec studentRec, char * csvBuff )
{
sprintf(csvBuff,"%s,%s,%s,%12.6lf\n",studentRec->studentID,
studentRec->studentLastName,
studentRec->studentFirstName,
studentRec->GPA);
return(csvBuff);
}
void OutputStudentRec(TStudentRec studentRec, char * debugMsg)
{
if (debugMsg!=NULL)
{
printf("******************************************************\n");
printf(debugMsg);
printf("\n");
}
printf("*****************************************************\n");
printf(" student id >%s< \n",studentRec->studentID);
printf(" student last name >%s< \n",studentRec->studentLastName);
printf(" student first name >%s< \n",studentRec->studentFirstName);
printf(" student GPA :%12.6lf \n",studentRec->GPA);
printf("----------------------------------------------------- \n");
}
void parseStudentRec( TStudentRec studentRec, char * csvBuff)
{
char token[255];
strcpy(studentRec->studentID,strtok(csvBuff,","));
strcpy(studentRec->studentLastName,strtok(NULL,","));
strcpy(studentRec->studentFirstName,strtok(NULL,","));
strcpy(token,strtok(NULL,",")) ;
studentRec->GPA = atof(token);
OutputStudentRec(studentRec,(char *)"parsed student rec");
}
int menu()
{
int iReturn=-1;
printf("****************************************\n");
printf(" Please Select One of these Options \n");
printf("*****************************************\n");
printf(" <1> INPUT NEW STUDENT RECORD \n");
printf(" <2> PRINT STUDENT RECORDS \n");
printf(" <0> EXIT // QUIT \n");
printf("------------------------------------------\n");
printf(" PLEASE SELECT ONE OF THESE OPTIONS :>");
scanf("%d",&iReturn);
return(iReturn);
}
int NewStudentRecord()
{
struct _TStudentRec studentRec;
char inbuff[255];
char outbuff[255];
printf(" Please input the student id # :>");
scanf("%s",inbuff);
char *commaPtr;
while ( (commaPtr=strchr(inbuff,','))!=NULL)
{
*commaPtr = '_';
}
strcpy(studentRec.studentID,inbuff);
printf(" Please input the student LAST NAME # :>");
scanf("%s",inbuff);
while ( (commaPtr=strchr(inbuff,','))!=NULL)
{
*commaPtr='_';
}
strcpy(studentRec.studentLastName,inbuff);
printf(" Please input the student FIRST NAME # :>");
scanf("%s",inbuff);
while ( (commaPtr=strchr(inbuff,','))!=NULL)
{
*commaPtr='_';
}
strcpy(studentRec.studentFirstName,inbuff);
double gpa=-1;
while (
(gpa>5) || (gpa<0)
)
{
printf(" Please input the student GPA :>");
scanf("%lf",&gpa);
}
studentRec.GPA = gpa;
SerializeStudentRecToCSV(&studentRec,outbuff);
FILE * fptr = fopen(STUDENT_REC_FILENAME,"a");
if (fptr!=NULL)
{
fprintf(fptr,outbuff);
fclose(fptr);
printf(" Student record saved \n");
}
}
void PrintStudentRecords()
{
char inbuff[255];
struct _TStudentRec curStudentRec;
FILE * fptr = fopen(STUDENT_REC_FILENAME,"r");
if (fptr!=NULL)
{
while (fgets(inbuff,255,fptr)!=NULL)
{
parseStudentRec(&curStudentRec,inbuff);
OutputStudentRec(&curStudentRec,NULL);
}
fclose(fptr);
}
}
int Go()
{
int iReturn=-1;
while (
(iReturn = menu()) != 0
)
{
switch (iReturn)
{
case 1:
{
int iReturn = NewStudentRecord();
if (iReturn<0) { printf(" create new student record fails \n"); }
break;
}
case 2:
{
PrintStudentRecords();
break;
}
}
}
return 0;
}
int main()
{
return(Go());
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
I assume what you mean by PRINT is To the SCREEN;... otherwise you will have to shell out to the command prompt and issue a print command to the OS04/21/20