M

Almost Done

public
mdjunayet May 26, 2024 Never 49
Clone
C Almost Done 764 lines (685 loc) | 26.64 KB
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <time.h>
5
#include <conio.h>
6
#include <windows.h>
7
8
#define MAX_USERS 100
9
#define MAX_SESSIONS 100
10
#define MAX_TODO 100
11
#define MAX_POMODOROS 100
12
#define USER_FILE "users.dat"
13
#define SESSION_FILE "sessions.dat"
14
#define TODO_FILE "todos.dat"
15
16
// Define structures for user accounts, study sessions, and to-do items
17
typedef struct {
18
char description[100];
19
int completed;
20
} ToDoItem;
21
22
typedef struct {
23
char category[50];
24
struct tm startTime;
25
struct tm endTime;
26
int duration; // in seconds
27
int sessionActive; // Indicates if a session is currently active
28
int countdownActive; // Indicates if a countdown is currently active
29
int countdownDuration; // in seconds
30
char sessionType[20]; // Type of the session (Timer, Countdown, Pomodoro)
31
} StudySession;
32
33
typedef struct {
34
int studyDuration; // in seconds
35
int breakDuration; // in seconds
36
} PomodoroSession;
37
38
typedef struct {
39
char username[50];
40
int sessionCount;
41
StudySession sessions[MAX_SESSIONS];
42
ToDoItem todos[MAX_TODO];
43
int todoCount;
44
PomodoroSession pomodoros[MAX_POMODOROS];
45
int pomodoroCount;
46
} User;
47
48
// Global array to store users and sessions
49
User users[MAX_USERS];
50
int userCount = 0;
51
int loggedIn = -1; // Index of logged-in user, -1 if no user is logged in
52
53
// Function prototypes
54
void clearScreen();
55
void displayHeader(const char* header);
56
void displayMainMenu();
57
void displayUserMenu();
58
void createUser();
59
void loginUser();
60
void startStudyOption();
61
void endSession();
62
void showStatistics();
63
void manageToDoList();
64
int findUserIndex(const char* username);
65
void pressEnterToContinue();
66
void startTimer();
67
void startCountdown();
68
int updateCountdown(int totalSeconds);
69
void pomodoro(int userIndex);
70
void loadUsers();
71
void saveUsers();
72
void loadSessions();
73
void saveSessions();
74
void loadToDos();
75
void saveToDos();
76
77
// Function to clear the screen
78
void clearScreen() {
79
#ifdef _WIN32
80
system("cls");
81
#else
82
system("clear");
83
#endif
84
}
85
86
// Function to display a header
87
void displayHeader(const char* header) {
88
clearScreen();
89
printf("== %s ==\n\n", header);
90
}
91
92
// Function to display the main menu
93
void displayMainMenu() {
94
system("color 3F");
95
displayHeader("Main Menu");
96
printf("1. Create new user\n");
97
printf("2. Login\n");
98
printf("3. Exit\n");
99
printf("Enter your choice: ");
100
}
101
102
// Function to display the user menu
103
void displayUserMenu() {
104
displayHeader("User Menu");
105
printf("Welcome back %s! Ready to start the day? ^V^\n",users[loggedIn].username);
106
printf("1. Start a study session\n");
107
printf("2. Show statistics\n");
108
printf("3. Manage to-do list\n");
109
printf("4. Logout\n");
110
printf("Enter your choice: ");
111
}
112
113
// Function to create a new user
114
void createUser() {
115
displayHeader("Create New User");
116
if (userCount >= MAX_USERS) {
117
printf("Error: Maximum number of users reached.\n");
118
pressEnterToContinue();
119
return;
120
}
121
printf("Enter username: ");
122
scanf("%49s", users[userCount].username);
123
users[userCount].sessionCount = 0;
124
users[userCount].todoCount = 0;
125
userCount++;
126
printf("User created successfully.\n");
127
pressEnterToContinue();
128
saveUsers(); // Save user data after creation
129
}
130
131
// Function to login a user
132
void loginUser() {
133
displayHeader("Login");
134
printf("Enter username: ");
135
char username[50];
136
scanf("%49s", username);
137
int index = findUserIndex(username);
138
if (index == -1) {
139
printf("Error: User not found.\n");
140
pressEnterToContinue();
141
} else {
142
loggedIn = index;
143
printf("Login successful.\n");
144
pressEnterToContinue();
145
}
146
}
147
148
// Function to find a user index
149
int findUserIndex(const char* username) {
150
for (int i = 0; i < userCount; i++) {
151
if (strcmp(users[i].username, username) == 0) {
152
return i;
153
}
154
}
155
return -1;
156
}
157
158
// Function to press enter to continue
159
void pressEnterToContinue()
160
{
161
printf("\nPress Enter to continue...");
162
getchar(); // Wait for Enter key
163
while (getchar() != '\n'); // Ensure buffer is empty
164
}
165
166
// Function to start a study session option
167
void startStudyOption() {
168
if (loggedIn == -1 || users[loggedIn].sessionCount >= MAX_SESSIONS) {
169
printf("Error: No user logged in or maximum sessions reached.\n");
170
pressEnterToContinue();
171
return;
172
}
173
displayHeader("Study Session Options");
174
printf("1. Timer\n");
175
printf("2. Countdown\n");
176
printf("3. Pomodoro\n");
177
printf("4. Exit to User Menu\n");
178
printf("Enter your choice: ");
179
int choice;
180
scanf("%d", &choice);
181
getchar(); // Clear the newline character after the number input
182
183
switch (choice) {
184
case 1:
185
startTimer();
186
break;
187
case 2:
188
startCountdown();
189
break;
190
case 3:
191
pomodoro(loggedIn);
192
break;
193
case 4:
194
return; // Exit to user menu
195
default:
196
printf("Invalid choice. Please try again.\n");
197
pressEnterToContinue();
198
}
199
}
200
201
// Function to start a timer
202
void startTimer() {
203
if (users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive) {
204
printf("Error: A session is already active. Please end the current session before starting a new one.\n");
205
pressEnterToContinue();
206
return;
207
}
208
209
displayHeader("Starting a New Study Session");
210
printf("Enter study category: ");
211
scanf("%49s", users[loggedIn].sessions[users[loggedIn].sessionCount].category);
212
getchar(); // Clear the newline character after the input
213
214
users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 1;
215
strcpy(users[loggedIn].sessions[users[loggedIn].sessionCount].sessionType, "Timer");
216
time_t start = time(NULL);
217
users[loggedIn].sessions[users[loggedIn].sessionCount].startTime = *localtime(&start);
218
displayHeader("Timer is on");
219
printf("%s\n", users[loggedIn].sessions[users[loggedIn].sessionCount].category);
220
printf("Press 'p' to pause the timer, 'r' to resume, 'q' to quit, and Enter to end the timer...\n");
221
222
int paused = 0;
223
time_t pauseStart, pauseEnd;
224
int pauseDuration = 0;
225
226
while (1) {
227
if (!_kbhit()) {
228
if (!paused) {
229
time_t current = time(NULL);
230
int elapsed = (int)difftime(current, start) - pauseDuration;
231
232
int hours = elapsed / 3600;
233
int minutes = (elapsed % 3600) / 60;
234
int seconds = elapsed % 60;
235
236
printf("\rElapsed time: %02d:%02d:%02d", hours, minutes, seconds);
237
fflush(stdout);
238
239
Sleep(100); // Use a shorter sleep to reduce drift
240
}
241
} else {
242
char key = _getch();
243
if (key == 'p') {
244
if (!paused) {
245
paused = 1;
246
pauseStart = time(NULL);
247
printf("\nTimer paused. Press 'r' to resume, 'q' to quit, and Enter to end...\n");
248
}
249
} else if (key == 'r') {
250
if (paused) {
251
paused = 0;
252
pauseEnd = time(NULL);
253
pauseDuration += (int)difftime(pauseEnd, pauseStart);
254
printf("\nTimer resumed. Press 'p' to pause, 'q' to quit, and Enter to end...\n");
255
}
256
} else if (key == 'q') {
257
printf("\nTimer stopped.\n");
258
saveSessions(); // Save session data before returning
259
return;
260
} else if (key == '\r') {
261
printf("\nTimer stopped.\n");
262
break;
263
}
264
}
265
}
266
267
time_t end = time(NULL);
268
users[loggedIn].sessions[users[loggedIn].sessionCount].endTime = *localtime(&end);
269
users[loggedIn].sessions[users[loggedIn].sessionCount].duration = (int)difftime(end, start) - pauseDuration;
270
users[loggedIn].sessions[users[loggedIn].sessionCount].sessionActive = 0;
271
users[loggedIn].sessionCount++;
272
printf("\nStudy session ended. Duration: %02d:%02d:%02d.\n",
273
users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration / 3600,
274
(users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration % 3600) / 60,
275
users[loggedIn].sessions[users[loggedIn].sessionCount - 1].duration % 60);
276
277
// Beep 3 times
278
for (int i = 0; i < 3; i++) {
279
Beep(750, 300);
280
Sleep(200); // Add a short delay between beeps
281
}
282
283
pressEnterToContinue();
284
saveSessions(); // Save session data before returning
285
}
286
287
288
// Function to start a countdown
289
void startCountdown() {
290
if (users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive) {
291
printf("Error: A countdown is already active. Please wait for it to finish before starting a new one.\n");
292
pressEnterToContinue();
293
return;
294
}
295
displayHeader("Countdown:");
296
printf("Enter study time in HH MM SS format: ");
297
int hours, minutes, seconds;
298
scanf("%d %d %d", &hours, &minutes, &seconds);
299
getchar(); // Clear the newline character after the number input
300
int totalSeconds = hours * 3600 + minutes * 60 + seconds;
301
if (totalSeconds <= 0 || hours < 0 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60) {
302
printf("Error: Invalid time.\n");
303
pressEnterToContinue();
304
return;
305
}
306
users[loggedIn].sessions[users[loggedIn].sessionCount].countdownDuration = totalSeconds;
307
users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive = 1;
308
strcpy(users[loggedIn].sessions[users[loggedIn].sessionCount].sessionType, "Countdown");
309
310
// Start the countdown and measure the duration
311
time_t start = time(NULL);
312
int totalElapsed = updateCountdown(totalSeconds);
313
time_t end = time(NULL);
314
315
users[loggedIn].sessions[users[loggedIn].sessionCount].startTime = *localtime(&start);
316
users[loggedIn].sessions[users[loggedIn].sessionCount].endTime = *localtime(&end);
317
users[loggedIn].sessions[users[loggedIn].sessionCount].duration = totalElapsed;
318
users[loggedIn].sessions[users[loggedIn].sessionCount].countdownActive = 0;
319
320
// Increment session count
321
users[loggedIn].sessionCount++;
322
323
// Save session data
324
saveSessions();
325
}
326
327
328
// Function to update countdown
329
int updateCountdown(int totalSeconds) {
330
displayHeader("Countdown");
331
int elapsed = 0;
332
333
while (totalSeconds > 0) {
334
int hours = totalSeconds / 3600;
335
int minutes = (totalSeconds % 3600) / 60;
336
int seconds = totalSeconds % 60;
337
printf("Time left: %02d:%02d:%02d\r", hours, minutes, seconds);
338
fflush(stdout);
339
Sleep(1000);
340
totalSeconds--;
341
elapsed++;
342
}
343
printf("\nCountdown finished!\n");
344
for (int i = 0; i < 3; i++) {
345
Beep(750, 300);
346
Sleep(200);
347
}
348
pressEnterToContinue();
349
saveSessions(); // Save session data before returning
350
return elapsed;
351
}
352
353
// Function to start a Pomodoro session
354
void pomodoro(int userIndex) {
355
displayHeader("Pomodoro Timer");
356
357
printf("Enter study duration in minutes: ");
358
int studyMinutes;
359
scanf("%d", &studyMinutes);
360
361
printf("Enter break duration in minutes: ");
362
int breakMinutes;
363
scanf("%d", &breakMinutes);
364
365
printf("Enter number of Pomodoro sessions: ");
366
int sessions;
367
scanf("%d", &sessions);
368
369
getchar(); // Clear the newline character after the input
370
371
if (studyMinutes <= 0 || breakMinutes <= 0 || sessions <= 0) {
372
printf("Error: Study, break durations, and number of sessions must be positive.\n");
373
pressEnterToContinue();
374
return;
375
}
376
377
int totalStudySeconds = studyMinutes * 60;
378
int totalBreakSeconds = breakMinutes * 60;
379
int pomodoroSessions = 0;
380
int key;
381
382
while (pomodoroSessions < sessions) {
383
// Start study session
384
users[userIndex].sessions[users[userIndex].sessionCount].sessionActive = 1;
385
strcpy(users[userIndex].sessions[users[userIndex].sessionCount].sessionType, "Pomodoro");
386
time_t start = time(NULL);
387
users[userIndex].sessions[users[userIndex].sessionCount].startTime = *localtime(&start);
388
389
displayHeader("Pomodoro Study Session");
390
391
392
for (int i = totalStudySeconds; i > 0; i--) {
393
printf("\rStudy time remaining: %02d:%02d", i / 60, i % 60);
394
fflush(stdout);
395
Sleep(1000);
396
397
if (_kbhit()) {
398
key = _getch();
399
if (key == 'p') {
400
printf("\nPaused. Press 'r' to resume, 'q' to quit.\n");
401
while (1) {
402
if (_kbhit()) {
403
key = _getch();
404
if (key == 'q') {
405
saveSessions(); // Save session data before returning
406
return;
407
} else if (key == 'r') {
408
break;
409
}
410
}
411
}
412
} else if (key == 'q') {
413
saveSessions(); // Save session data before returning
414
return;
415
}
416
}
417
}
418
419
time_t end = time(NULL);
420
users[userIndex].sessions[users[userIndex].sessionCount].endTime = *localtime(&end);
421
users[userIndex].sessions[users[userIndex].sessionCount].duration = (int)difftime(end, start);
422
users[userIndex].sessions[users[userIndex].sessionCount].sessionActive = 0;
423
users[userIndex].sessionCount++;
424
425
// Bell ringing after study session ends
426
for (int i = 0; i < 3; i++) {
427
Beep(750, 300);
428
Sleep(200);
429
}
430
431
saveSessions(); // Save session data
432
433
// Break session
434
displayHeader("Pomodoro Break");
435
for (int i = totalBreakSeconds; i > 0; i--) {
436
printf("\rBreak time remaining: %02d:%02d", i / 60, i % 60);
437
fflush(stdout);
438
Sleep(1000);
439
440
if (_kbhit()) {
441
key = _getch();
442
if (key == 'p') {
443
printf("\nPaused. Press 'r' to resume, 'q' to quit.\n");
444
while (1) {
445
if (_kbhit()) {
446
key = _getch();
447
if (key == 'q') {
448
saveSessions(); // Save session data before returning
449
return;
450
} else if (key == 'r') {
451
break;
452
}
453
}
454
}
455
} else if (key == 'q') {
456
457
458
459
saveSessions(); // Save session data before returning
460
return;
461
}
462
}
463
}
464
465
pomodoroSessions++;
466
}
467
for (int i = 0; i < 3; i++) {
468
Beep(750, 300);
469
Sleep(200);
470
}
471
472
// All Pomodoro sessions are complete
473
printf("\nDo you want to start another Pomodoro session? (y/n): ");
474
char choice = getchar();
475
getchar(); // Clear the newline character after the input
476
477
if (choice == 'y' || choice == 'Y') {
478
pomodoro(userIndex); // Start another Pomodoro session
479
} else {
480
printf("Pomodoro sessions completed.\n");
481
482
pressEnterToContinue();
483
}
484
}
485
486
487
488
// Function to show statistics
489
void showStatistics() {
490
displayHeader("Statistics");
491
int timerSessions = 0, countdownSessions = 0, pomodoroSessions = 0;
492
int totalTimerDuration = 0, totalCountdownDuration = 0, totalPomodoroDuration = 0;
493
494
// Calculate total duration for each session type
495
for (int i = 0; i < users[loggedIn].sessionCount; i++) {
496
if (strcmp(users[loggedIn].sessions[i].sessionType, "Timer") == 0) {
497
timerSessions++;
498
totalTimerDuration += users[loggedIn].sessions[i].duration;
499
} else if (strcmp(users[loggedIn].sessions[i].sessionType, "Countdown") == 0) {
500
countdownSessions++;
501
totalCountdownDuration += users[loggedIn].sessions[i].duration;
502
} else if (strcmp(users[loggedIn].sessions[i].sessionType, "Pomodoro") == 0) {
503
pomodoroSessions++;
504
totalPomodoroDuration += users[loggedIn].sessions[i].duration;
505
}
506
}
507
508
// Convert total timer duration to hours, minutes, and seconds
509
int totalTimerHours = totalTimerDuration / 3600;
510
int totalTimerMinutes = (totalTimerDuration % 3600) / 60;
511
int totalTimerSeconds = totalTimerDuration % 60;
512
513
printf("Total sessions: %d\n", users[loggedIn].sessionCount);
514
printf("Timer sessions: %d (Total duration: %02d:%02d:%02d)\n", timerSessions,
515
totalTimerHours, totalTimerMinutes, totalTimerSeconds);
516
printf("Countdown sessions: %d (Total duration: %02d:%02d:%02d)\n", countdownSessions,
517
totalCountdownDuration / 3600, (totalCountdownDuration % 3600) / 60, totalCountdownDuration % 60);
518
printf("Pomodoro sessions: %d (Total duration: %02d:%02d:%02d)\n", pomodoroSessions,
519
totalPomodoroDuration / 3600, (totalPomodoroDuration % 3600) / 60, totalPomodoroDuration % 60);
520
printf("Well done! Keep going! ");
521
pressEnterToContinue();
522
}
523
524
// Function to manage to-do list
525
void manageToDoList() {
526
loadToDos(); // Load the to-do list data when the function starts
527
528
while (1) {
529
displayHeader("To-Do List");
530
printf("1. Add to-do item(s)\n");
531
printf("2. Mark item as completed\n");
532
printf("3. View all to-do items\n");
533
printf("4. Delete a to-do item\n"); // Added delete option
534
printf("5. Exit to User Menu\n");
535
printf("Enter your choice: ");
536
int choice;
537
scanf("%d", &choice);
538
getchar(); // Clear the newline character after the number input
539
540
switch (choice) {
541
case 1: {
542
while (1) {
543
if (users[loggedIn].todoCount >= MAX_TODO) {
544
printf("Error: Maximum number of to-do items reached.\n");
545
pressEnterToContinue();
546
break;
547
}
548
displayHeader("Add To-Do Item");
549
printf("Enter description: ");
550
fgets(users[loggedIn].todos[users[loggedIn].todoCount].description, 100, stdin);
551
users[loggedIn].todos[users[loggedIn].todoCount].description[strcspn(users[loggedIn].todos[users[loggedIn].todoCount].description, "\n")] = '\0'; // Remove trailing newline
552
users[loggedIn].todos[users[loggedIn].todoCount].completed = 0;
553
users[loggedIn].todoCount++;
554
printf("To-do item added.\n");
555
556
printf("Do you want to add another item? (y/n): ");
557
char addMore;
558
scanf(" %c", &addMore);
559
getchar(); // Clear the newline character after the input
560
if (addMore == 'n' || addMore == 'N') {
561
break;
562
}
563
}
564
saveToDos(); // Save the updated to-do list
565
break;
566
}
567
case 2: {
568
if (users[loggedIn].todoCount == 0) {
569
printf("No to-do items to mark as completed. Get to work RIGHT NOW >:( \n");
570
pressEnterToContinue();
571
break;
572
}
573
displayHeader("Mark To-Do Item as Completed");
574
printf("To-do items:\n");
575
for (int i = 0; i < users[loggedIn].todoCount; i++) {
576
printf("%d. [%c] %s\n", i + 1, users[loggedIn].todos[i].completed ? 'x' : ' ', users[loggedIn].todos[i].description);
577
}
578
printf("Enter the number of the item to mark as completed: ");
579
int itemNumber;
580
scanf("%d", &itemNumber);
581
getchar(); // Clear the newline character after the number input
582
if (itemNumber < 1 || itemNumber > users[loggedIn].todoCount) {
583
printf("Error: Invalid item number.\n");
584
pressEnterToContinue();
585
break;
586
}
587
users[loggedIn].todos[itemNumber - 1].completed = 1;
588
printf("To-do item marked as completed.\n");
589
pressEnterToContinue();
590
saveToDos(); // Save the updated to-do list
591
break;
592
}
593
case 3: {
594
displayHeader("View All To-Do Items");
595
if (users[loggedIn].todoCount == 0) {
596
printf("No to-do items.\n");
597
} else {
598
printf("To-do items:\n");
599
for (int i = 0; i < users[loggedIn].todoCount; i++) {
600
printf("%d. [%c] %s\n", i + 1, users[loggedIn].todos[i].completed ? 'x' : ' ', users[loggedIn].todos[i].description);
601
}
602
}
603
pressEnterToContinue();
604
break;
605
}
606
case 4: {
607
if (users[loggedIn].todoCount == 0) {
608
printf("No to-do items to delete.\n");
609
pressEnterToContinue();
610
break;
611
}
612
displayHeader("Delete To-Do Item");
613
printf("To-do items:\n");
614
for (int i = 0; i < users[loggedIn].todoCount; i++) {
615
printf("%d. %s\n", i + 1, users[loggedIn].todos[i].description);
616
}
617
printf("Enter the number of the item to delete: ");
618
int itemNumber;
619
scanf("%d", &itemNumber);
620
getchar(); // Clear the newline character after the number input
621
if (itemNumber < 1 || itemNumber > users[loggedIn].todoCount) {
622
printf("Error: Invalid item number.\n");
623
pressEnterToContinue();
624
break;
625
}
626
// Shift remaining items to overwrite the deleted item
627
for (int i = itemNumber - 1; i < users[loggedIn].todoCount - 1; i++) {
628
users[loggedIn].todos[i] = users[loggedIn].todos[i + 1];
629
}
630
users[loggedIn].todoCount--;
631
printf("To-do item deleted.\n");
632
pressEnterToContinue();
633
saveToDos(); // Save the updated to-do list
634
break;
635
}
636
case 5:
637
saveToDos(); // Save the to-do list before exiting
638
return; // Exit to user menu
639
default:
640
printf("Invalid choice. Please try again.\n");
641
pressEnterToContinue();
642
}
643
}
644
}
645
646
// Function to load users from file
647
void loadUsers() {
648
FILE* file = fopen(USER_FILE, "rb");
649
if (file != NULL) {
650
fread(&userCount, sizeof(int), 1, file);
651
fread(users, sizeof(User), userCount, file);
652
fclose(file);
653
}
654
}
655
656
// Function to save users to file
657
void saveUsers() {
658
FILE* file = fopen(USER_FILE, "wb");
659
if (file != NULL) {
660
fwrite(&userCount, sizeof(int), 1, file);
661
fwrite(users, sizeof(User), userCount, file);
662
fclose(file);
663
}
664
}
665
666
// Function to load sessions from file
667
void loadSessions() {
668
FILE* file = fopen(SESSION_FILE, "rb");
669
if (file != NULL) {
670
fread(&userCount, sizeof(int), 1, file);
671
fread(users, sizeof(User), userCount, file);
672
fclose(file);
673
}
674
}
675
676
// Function to save sessions to file
677
void saveSessions() {
678
FILE* file = fopen(SESSION_FILE, "wb");
679
if (file != NULL) {
680
fwrite(&userCount, sizeof(int), 1, file);
681
fwrite(users, sizeof(User), userCount, file);
682
fclose(file);
683
}
684
}
685
686
// Function to load to-dos from file
687
void loadToDos() {
688
FILE* file = fopen(TODO_FILE, "rb");
689
if (file != NULL) {
690
fread(&userCount, sizeof(int), 1, file);
691
for (int i = 0; i < userCount; i++) {
692
fread(&users[i].todoCount, sizeof(int), 1, file);
693
fread(users[i].todos, sizeof(ToDoItem), users[i].todoCount, file);
694
}
695
fclose(file);
696
}
697
}
698
699
void saveToDos() {
700
FILE* file = fopen(TODO_FILE, "wb");
701
if (file != NULL) {
702
fwrite(&userCount, sizeof(int), 1, file);
703
for (int i = 0; i < userCount; i++) {
704
fwrite(&users[i].todoCount, sizeof(int), 1, file);
705
fwrite(users[i].todos, sizeof(ToDoItem), users[i].todoCount, file);
706
}
707
fclose(file);
708
}
709
}
710
711
712
int main() {
713
loadUsers(); // Load users from file
714
loadSessions();// Load users from file
715
loadToDos(); // Load to-do data at startup
716
while (1) {
717
if (loggedIn == -1) {
718
displayMainMenu();
719
int choice;
720
scanf("%d", &choice);
721
getchar(); // Clear the newline character after the number input
722
switch (choice) {
723
case 1:
724
createUser();
725
break;
726
case 2:
727
loginUser();
728
break;
729
case 3:
730
saveUsers(); // Save users before exiting
731
return 0; // Exit the program
732
default:
733
printf("Invalid choice. Please try again.\n");
734
pressEnterToContinue();
735
}
736
} else {
737
displayUserMenu();
738
int choice;
739
scanf("%d", &choice);
740
getchar(); // Clear the newline character after the number input
741
switch (choice) {
742
case 1:
743
startStudyOption();
744
break;
745
case 2:
746
showStatistics();
747
break;
748
case 3:
749
manageToDoList();
750
break;
751
case 4:
752
loggedIn = -1; // Logout
753
break;
754
default:
755
printf("Invalid choice. Please try again.\n");
756
pressEnterToContinue();
757
}
758
}
759
}
760
saveUsers(); // Save user data before exiting
761
saveSessions(); // Save session data before exiting
762
saveToDos(); // Save to-do data before exiting
763
return 0;
764
}