-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
278 lines (248 loc) · 8.28 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// structure
struct LoginCredentials {
char username[50];
char password[50];
};
struct Student {
char name[50];
int age;
char major[50];
float percentage;
int attendance;
};
// functions declaration
void calculatePERC(struct Student *student);
float percentage(float total, float fM);
void inputAttendance(struct Student *student);
void executeUserLogin();
// main function
int main() {
executeUserLogin();
struct Student students[100];
FILE *fptr;
int choice, num_students = 0, num_saved_stds = 1;
do {
printf("------------------------------------------------");
printf("\n*********************Menu***********************\n");
printf("------------------------------------------------\n");
printf("1. Add Student\n");
printf("2. Calculate Percentage\n");
printf("3. Input Attendance\n");
printf("4. View Recent Students\n");
printf("5. View Saved Students\n");
printf("6. Save Recent Info\n");
printf("7. Exit\n");
printf("------------------------------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
system("clear");
switch (choice) {
case 1:
printf("\n------------------------------------------------");
printf("\n****************Student Form********************\n");
printf("------------------------------------------------\n");
printf("Enter name: ");
fgets(students[num_students].name, 50, stdin);
printf("Enter age: ");
scanf("%d", &students[num_students].age);
getchar(); // Consume the newline character left by scanf
printf("Enter major: ");
fgets(students[num_students].major, 50, stdin);
// getchar();
students[num_students].percentage = 0;
students[num_students].attendance = 0;
num_students++;
system("clear");
printf("\nStudent Added Successfully!!\n");
printf("\n");
break;
case 2:
if (num_students > 0) {
int student_index;
printf("\n------------------------------------------------\n");
printf("Enter student index (0 to %d): ", num_students - 1);
scanf("%d", &student_index);
if (student_index >= 0 && student_index < num_students) {
calculatePERC(&students[student_index]);
system("clear");
printf("\nPercentage calculated for %s\n",
students[student_index].name);
} else {
system("clear");
printf("\n⚠ Invalid student index\n");
}
} else {
system("clear");
printf("\n⚠No students to calculate Percentage\n");
}
printf("\n");
break;
case 3:
if (num_students > 0) {
int student_index;
printf("\n------------------------------------------------\n");
printf("Enter student index (0 to %d): ", num_students - 1);
scanf("%d", &student_index);
if (student_index >= 0 && student_index < num_students) {
inputAttendance(&students[student_index]);
system("clear");
printf("Attendance recorded for %s!!!\n",
students[student_index].name);
} else {
system("clear");
printf("\n⚠Invalid student index\n");
}
} else {
system("clear");
printf("\n⚠No students to record attendance\n");
}
printf("\n");
break;
case 4:
if (num_students > 0) {
printf("\nRecently added Students:\n");
for (int i = 0; i < num_students; i++) {
printf("-----------------------\n");
printf("Name: %s", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Major: %s", students[i].major);
printf("Percentage: %.2f%\n", students[i].percentage);
printf("Attendance: %d/30\n", students[i].attendance);
printf("-----------------------\n");
printf("You should save it permanently!!\n");
}
} else {
printf("\n⚠No students to display\n");
}
break;
case 5:
if (num_saved_stds > 0) {
fptr = fopen("stdsData.txt", "r");
char ch;
ch = fgetc(fptr);
while (ch != EOF) {
printf("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
} else {
printf("\n⚠No students to display\n");
}
break;
case 6:
if (num_students > 0) {
fptr = fopen("stdsData.txt", "a");
for (int i = 0; i < num_students; i++) {
fprintf(fptr, "-----------------------\n");
fprintf(fptr, "Name: %s", students[i].name);
fprintf(fptr, "Age: %d\n", students[i].age);
fprintf(fptr, "Major: %s", students[i].major);
fprintf(fptr, "Percentage: %.2f%\n", students[i].percentage);
fprintf(fptr, "Attendance: %d/30\n", students[i].attendance);
fprintf(fptr, "-----------------------\n");
num_saved_stds++;
}
fclose(fptr);
printf("\nData Successfully saved \n");
} else {
printf("\n⚠No students to save!!\n");
}
break;
case 7:
system("clear");
printf("\nExiting program......\n");
break;
default:
printf("\n⚠Invalid choice, please try again\n");
}
} while (choice != 7);
printf("\n");
return 0;
}
// Functions defination
void executeUserLogin() {
int option1;
while(1) {
printf("SIGN UP (1) or LOGIN (2)?\n");
scanf("%d", &option1);
if (option1 == 1) {
///////////////////SIGN UP//////////////////////
struct LoginCredentials user;
FILE *file = fopen("login.txt", "r");
if (file == NULL) {
file = fopen("login.txt", "w");
printf("Enter a username: ");
scanf("%s", user.username);
printf("Enter a password: ");
scanf("%s", user.password);
fprintf(file, "%s\t%s", user.username, user.password);
printf("Sign up successful!\n");
fclose(file);
break;
} else {
printf("YOU HAVE ALREADY SIGNED UP!!! \n");
fclose(file);
}
/////////////////opt1 end
} else if (option1 == 2) {
//////////////////////////LOGIN///////////////////////
char userInputUsername[50];
char userInputPassword[50];
printf("Enter your username: ");
scanf("%s", userInputUsername);
printf("Enter your password: ");
scanf("%s", userInputPassword);
FILE *fptr;
fptr = fopen("login.txt", "r");
if (fptr != NULL) {
char username[50];
char password[50];
fscanf(fptr, "%s\t%s", username, password);
if (strcmp(userInputUsername, username) == 0 && strcmp(userInputPassword, password) == 0) {
printf("LOGIN SUCCESSFUL!\n");
fclose(fptr);
break;
} else {
printf("Invalid username or password!\n");
fclose(fptr);
}
} else {
printf("Error opening file\n");
}
////////////////////////LOGIN END//////////////////////
} else {
continue; // exit while loop if neither 1 nor 2 is entered
}
}
}
void calculatePERC(struct Student *student) {
float phy, che, mat, nep, eng, comp, fM, percent;
printf("\nEnter full marks : ");
scanf("%f", &fM);
printf("\nEnter physics marks : ");
scanf("%f", &phy);
printf("\nEnter chemistry marks : ");
scanf("%f", &che);
printf("\nEnter math marks : ");
scanf("%f", &mat);
printf("\nEnter nepali marks : ");
scanf("%f", &nep);
printf("\nEnter english marks : ");
scanf("%f", &eng);
printf("\nEnter computer marks : ");
scanf("%f", &comp);
float total = phy + che + mat + nep + eng + comp;
percent = percentage(total, fM);
student->percentage = percent;
}
void inputAttendance(struct Student *student) {
int att;
printf("\nEnter this month attendence:");
scanf("%d", &att);
student->attendance = att;
}
float percentage(float total, float fM) { return (total / (fM * 6)) * 100; }