-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathC11E3StudentInfo.c
71 lines (61 loc) · 1.5 KB
/
C11E3StudentInfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Challenges Ch11E3 - StudentInfo */
// By Jim-Kristian Malmhaug | Date: 13 Juni 2015
//Libraries:
#include <stdio.h>
#include <stdlib.h>
//Main function
int main() {
//Variable declerations
char cName[25] = "\0";
char cSurname[25] = "\0";
int iSelection = 0;
int iJump = 0;
float fGpa;
FILE *pWrite;
FILE *pRead;
do {
iSelection = showMenu();
switch(iSelection){
case 1:
pWrite = fopen("student.dat", "a");
if (pWrite == NULL)
goto ErrorHandler;
else
printf("\nEnter Name Surname GPA. With space between each: ");
scanf("%s%s%f", cName, cSurname, &fGpa);
fprintf(pWrite, "%s\t%s\t%.2f\n", cName, cSurname, fGpa);
//printf("\nYou wrote %s\t%s\t%.2f\n", cName, cSurname, fGpa);
fclose(pWrite);
break;
case 2:
iJump = 0;
pRead = fopen("student.dat", "r");
if (pRead == NULL)
goto ErrorHandler;
else
printf("\n");
while(!feof(pRead)){
if(iJump > 0)
printf("%s %s %.2f\n", cName, cSurname, fGpa);
fscanf(pRead,"%s%s%f", cName, cSurname, &fGpa);
iJump += 1;
}
fclose(pRead);
break;
}
}while(iSelection != 3);
exit(EXIT_SUCCESS); //Exit program normally
ErrorHandler:
perror("The following error ocurred");
exit(EXIT_FAILURE); //Exit program failed
}
int showMenu() {
int iSelection;
printf("\n Menu \n------\n");
printf("1. Enter student info\n");
printf("2. Print student info\n");
printf("3. Quit\n");
printf("---> ");
scanf("%d", &iSelection);
return iSelection;
}