Code:
/* Create a program that uses a menu with options to enter student information*
* (name, ID, GPA), print student information, or quit the program. Use data *
* files and FILE pointers to store and print information entered. */
#include <stdio.h>
#include <stdlib.h> // For fgets()
#include <string.h> // For strcpy()
#include <ctype.h> // For toupper()
static void showMenu();
static void enterStudentInformation(char [], char [], char []);
static void saveStudentInformation(char [], char [], char []);
static void findStudentInformation(char [], char [], char []);
static void showStudentInformation(char [], char [], char []);
int main()
{
char studentName[20] = "";
char id[20] = "";
char gpa[20] = "";
char response;
char newline;
showMenu();
(void) scanf("%c%c", &response, &newline); // second %c swallows \n
response = toupper(response);
while (response != 'Q')
{
switch (response)
{
case 'A': enterStudentInformation(studentName, id, gpa);
saveStudentInformation(studentName, id, gpa);
break;
case 'B': findStudentInformation(studentName, id, gpa);
showStudentInformation(studentName, id, gpa);
break;
}
showMenu();
(void) scanf("%c%c", &response, &newline);
response = toupper(response);
}
return 0;
} // end main()
void showMenu()
{
printf("\tMenu\n");
printf("\nA. Enter student information. <A/a>: ");
printf("\nB. Show student information. <B/b>: ");
printf("\nC. Quit the program. <Q/q>: ");
}
void enterStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
int stringSize = 19;
printf("\nEnter student name (firstname lastname): ");
(void) fgets(aStudentName, stringSize, stdin);
printf("\nEnter student ID: ");
(void) fgets(anID, stringSize, stdin);
printf("\nEnter student gpa: ");
(void) fgets(aGPA, stringSize, stdin);
}
void saveStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
FILE * ptrWriteRecord;
ptrWriteRecord = fopen("studentInformation.dat", "a");
if (ptrWriteRecord != NULL)
{
fprintf(ptrWriteRecord, "%s%s%s", aStudentName, anID, aGPA);
(void) fclose(ptrWriteRecord);
}
else
{
printf("\nFile studentInformation.dat cannot be opened.\n");
}
}
void findStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
FILE * ptrReadRecord;
char temp[20] = "";
int stringSize = 20;
ptrReadRecord = fopen("studentInformation.dat", "r");
if (ptrReadRecord != NULL)
{
printf("\nEnter student name (firstname lastname): ");
(void) fgets(temp, stringSize, stdin);
while (feof(ptrReadRecord) != EOF)
{
(void) fgets(aStudentName, stringSize, ptrReadRecord);
if (strcmp(aStudentName, temp)== 0)
{
(void) fgets(anID, stringSize, ptrReadRecord);
(void) fgets(aGPA, stringSize, ptrReadRecord);
fclose(ptrReadRecord);
return; // student information has been found
}
} // end while()
}
else
{
printf("\nFile studentInformation.dat cannot be opened.\n");
fclose(ptrReadRecord);
}
} // end findStudentInformation()
void showStudentInformation(char aStudentName[], char anID[], char aGPA[])
{
printf("\nName\t\tID\t\tGPA\n");
printf("%s%s%s\n", aStudentName, anID, aGPA);
}