-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathANSWER.c
94 lines (51 loc) · 1.47 KB
/
ANSWER.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdbool.h>
int main(){
char studentID[];
int answers[4];
bool valid = true;
int i, j, numberOfCorrectAnswers;
FILE *fptr;
fptr = fopen("answers.dat", "w");
for (j = 1; j <= 2; j++){
printf("Enter user ID : ");
scanf("%s", studentID);
for(i = 0; i < 4; i++){
//do{--->code bloat
printf(" Question %d : ", i + 1);
scanf("%d", &answers[i]);
if(answers[i] > 5 || answers[i] < 1){
printf("Answer Shuld be in between 1 to 5, Try again !\n");
i--; // Preventing storing the wrong answer
}
//}while(answers[i] > 5 || answers[i] < 1); --->code bloat
}
fprintf(fptr, "%s %d %d %d %d \n", studentID, answers[0], answers[1], answers[2], answers[3]); // \n
if(j < 2){
fprintf(fptr, "\n");
}
}
fclose(fptr);
//FILE *fptr2; //---> we can use fpr.. memory management
fptr = fopen("answers.dat", "r");
while(!feof(fptr)){
numberOfCorrectAnswers = 0;
fscanf(fptr, "%8s %d %d %d %d", studentID, &answers[0], &answers[1], &answers[2], &answers[3]);
if(answers[0] == 1){
numberOfCorrectAnswers++;
}
if(answers[1] == 4){
numberOfCorrectAnswers++;
}
if(answers[2] == 2){
numberOfCorrectAnswers++;
}
if(answers[3] == 3){
numberOfCorrectAnswers++;
}
printf("\nStident ID Marks\n");
printf("%s %d\n", studentID, numberOfCorrectAnswers);
}
fclose(fptr);
return 0;
}