-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1159 lines (1148 loc) · 28.5 KB
/
main.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author AgentQandhisassitant
* @brief This is code for school management project for KTLT
*/
#include<bits/stdc++.h> //Includes all Standard C++ Headers
#ifdef unix //If Compilation Environment is UNIX
#include <math.h>
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <stdlib.h>
#include <unistd.h> //For usleep() function
#define delay(x) usleep(x*1000) //For Windows delay function
#define CLS() cout<<"\033[2J\033[1;1H" //ANSI Escape sequence for clearing screen
int login();
void gotoxy(int x,int y){ //gotoxy function for UNIX
printf("%c[%d;%df",0x1B,y,x); //ANSI Escape Seqeuence for gotoxy
}
#endif
#ifndef unix //If Compilation Environment is not UNIX
#define CLS() system("cls") //Windows clearscreen function
#include<windows.h>
void gotoxy(int x,int y){ //gotoxy for Windows - it isn't my code
y--;
static HANDLE h=NULL;
if(!h){
h = GetStdHandle(STD_OUTPUT_HANDLE);
}
COORD c = {x,y};
SetConsoleCursorPosition(h,c);
}
void delay(unsigned int secs){ //Defining delay function for Windows
clock_t goal = secs + clock(); //Using clock to set time to delay till
while(goal>clock()); //Waiting till that time
}
#endif
#define FLBSTUD "student.txt" //Macros for storing filenames for ease
#define FLBCLAS "class.txt" //The files are binary files
#define RULE(x) cout<<'\n'; for(int _=0;_<80;_++) cout<<x; cout<<'\n' //Outputs Horizontal Consisting of 'x's
#define CL(cl,x) cl==0?1:cl==x //Macro for disabling search through class
using namespace std;
int strcmpi(const char * s1, const char* s2){ //String compare without case
return strcasecmp(s1,s2);
}
bool strcmpis(pair <string, int> s1, pair <string,int> s2){ //Compare function for sort()
return (strcasecmp(s1.first.c_str(),s2.first.c_str()))<0;
}
void load(){ //Loader function [c-p cp]
CLS();
cout<<"\n\n\n\n\t\t\t\t Loading\n\n";
for (int i=0;i<80;i++){
cout<<"!";
gotoxy(i,3); //Top Loading line
cout<<"!";
cout.flush(); //Flush output buffer for delay()
gotoxy(i+1,7); //Bottom Loading line
delay((rand()%80) + 20);
}
cout.flush();
delay(200);
}
int scan(){ //Scan function for input of only non-negative integers
string ch; //Taking inital input through string
int i,v;
do{
v=1;
cin>>ch;
for(i=0;i<ch.size();i++){ //Checking each character is digit
if(!isdigit(ch[i])){
v=0;
break;
}
}
}while(!v);
return atoi(ch.c_str()); //Coverting string back to integer
}
char bGs[8][4] = {"A+","B+", "AB+", "O+", "A-", "B-", "AB-", "O-"}; //Allowed Blood Groups
int fee[12] = { //Fee for each standard
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000,
1000
};
char * strTitle(int x){ //Coverting Title from Integer to readable text
static char title[8] = " ";
if(x==1)
strcpy(title,"Master");
else if(x==2)
strcpy(title,"Mr");
else
strcpy(title,"Miss");
return title;
}
class Student { //Student class for storing Records in the student file
int title; //Master = 1, Mr = 2, Miss = 3
char studentName[30];
int rollNo;
char fatherName[30];
char motherName[30];
char address[80];
char bloodGroup[4];
public:
void getDetails(void); //Get Student Details from user
void printDetails(void){ //Printing the details of Student
cout<<"Student Name : "<<strTitle(title)<<' '<<studentName<<endl;
cout<<"Roll No. : "<<rollNo<<endl;
cout<<"Father's Name : "<<fatherName<<endl;
cout<<"Mother's Name : "<<motherName<<endl;
cout<<"Address : "<<address<<endl;
cout<<"Blood Group : "<<bloodGroup<<endl;
}
int retRollNo(){ //Return Roll No
return rollNo;
}
char * retString(char x){ //Return all strings avaialable from the Student Class
if(x=='T')
return strTitle(title);
if(x=='N')
return studentName;
if(x=='F')
return fatherName;
if(x=='M')
return motherName;
if(x=='A')
return address;
if(x=='B')
return bloodGroup;
}
char * retStudentName(){ //Returns Student Name
return retString('N');
}
void modDetail(char ch); //Modify Details for Student
};
void Student::getDetails(){ //Get Student Details from user
system("CLS");
cout<<"Enter Title \n(Master = 1, Mr = 2, Miss = 3) : ";
do{
title = scan();
}while(title!=1 && title!=2 && title!=3);
cout<<"Enter Student Name : ";
do{
gets(studentName);
}while(strlen(studentName)==0);
cout<<"Enter Roll No. : ";
do{
rollNo=scan();
}while(rollNo<1);
cout<<"Enter Father Name : ";
do{
gets(fatherName);
}while(strlen(fatherName)==0);
cout<<"Enter Mother Name : ";
do{
gets(motherName);
}while(strlen(motherName)==0);
cout<<"Enter Address : ";
do{
gets(address);
}while(strlen(address)==0);
cout<<"Enter Blood Group : ";
int v = 0,i;
do{
gets(bloodGroup);
for (i=0;i<strlen(bloodGroup);i++) bloodGroup[i] = toupper(bloodGroup[i]);
for(i=0;i<8;i++)
if(!strcmp(bloodGroup,bGs[i])){
v=1;
break;
}
}while(!v);
}
void Student::modDetail(char ch){ //Modify Details for Student
system("CLS");
if(ch=='T'){ //Argument will tell which detail to modify
cout<<"Enter Title \n(Master = 1, Mr = 2, Miss = 3) : ";
do{
title=scan();
}while(title!=1 && title!=2 && title!=3);
}
else if(ch=='N'){
cout<<"Enter Student Name : ";
do{
gets(studentName);
}while(strlen(studentName)==0);
}
else if(ch=='R'){
int r=rollNo; //Save current Roll No.
cout<<"Enter Roll No. : ";
fstream fl(FLBSTUD,ios::in|ios::binary);
Student obj;
do{
rollNo = scan();
fl.close();
fl.open(FLBSTUD,ios::in|ios::binary);
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
if(obj.retRollNo()==rollNo && r!=rollNo){ //Check if the new rollNo already exists
cout<<"\nSame Roll No. Already Exists !\n";
cout<<"Enter Roll No. : ";
rollNo = -1;
}
}
}while(rollNo<1);
fl.close();
}
else if(ch=='F'){
cout<<"Enter Father Name : ";
do{
gets(fatherName);
}while(strlen(fatherName)==0);
}
else if(ch=='M'){
cout<<"Enter Mother Name : ";
do{
gets(motherName);
}while(strlen(motherName)==0);
}
else if(ch=='A'){
cout<<"Enter Address : ";
do{
gets(address);
}while(strlen(address)==0);
}
else {
cout<<"Enter Blood Group : ";
int v = 0,i;
do{
gets(bloodGroup); //Loop for checking valid Blood Groups
for (i=0;i<strlen(bloodGroup);i++) bloodGroup[i] = toupper(bloodGroup[i]);
for(i=0;i<8;i++)
if(!strcmp(bloodGroup,bGs[i])){
v=1;
break;
}
}while(!v);
}
}
class Class { //Class class for storing Records in class file
int class_standard;
char studentName[30];
int rollNo;
char Subject[30];
public:
void getDetails(); //Get Class Record Detail from user
void printDetails(int i=1){ //Print Class Record Details
cout<<"Student Name : "<<studentName<<endl;
if(i)
cout<<"Class Standard : "<<class_standard<<endl;
cout<<"Roll No. : "<<rollNo<<endl;
cout<<"Subject : "<<Subject<<endl;
}
int retClass(){ //Return Class Standard
return class_standard;
}
int retRollNo(){ //Return Roll No
return rollNo;
}
char * retString(char x){ //Return all strings avaialable from the Class class
if(x=='N')
return studentName;
if(x=='S')
return Subject;
}
char * retStudentName(){ //Return Student Name
return retString('N');
}
void modDetail(char ch);
};
void Class::getDetails(){ //Get Class Record Details from user
system("CLS");
cout<<"Enter Class Standard : ";
do{
class_standard = scan();
}while(class_standard>12 || class_standard<1);
cout<<"Enter Student Name : ";
do{
gets(studentName);
}while(strlen(studentName)==0);
cout<<"Enter Roll No. : ";
do{
rollNo = scan();
}while(rollNo<1);
cout<<"Enter Subject : ";
do{
gets(Subject);
}while(strlen(Subject)==0);
}
void Class::modDetail(char ch){ //Modify Class Record
system("CLS");
if(ch=='C'){
cout<<"Enter Class Standard : ";
do{
class_standard = scan();
}while(class_standard>12 || class_standard<1);
}
else if(ch=='N'){
cout<<"Enter Student Name : ";
do{
gets(studentName);
}while(strlen(studentName)==0);
}
else if(ch=='R'){
fstream fl(FLBCLAS,ios::in|ios::binary);
Class obj;
int r = rollNo; //Save Current Roll No.
cout<<"Enter Roll No. : ";
do{
rollNo = scan();
fl.close();
fl.open(FLBCLAS,ios::in|ios::binary);
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break; //Check if new Roll No. Already Exists
if(obj.retRollNo()==rollNo && r!=rollNo){
cout<<"\nSame Roll No. Already Exists !\n";
cout<<"Enter Roll No. : ";
rollNo=-1;
}
}
}while(rollNo<1);
fl.close();
}
else {
cout<<"Subject : ";
int v = 0,i;
do{
gets(Subject);
}while(strlen(Subject)==0);
}
}
void insertStudent(){ //Insert Student Record in File
system("CLS");
Student obj,obj2;
char ch;
int v=0;
cout<<"Enter Details for new Student :\n";
obj.getDetails();
fstream fl1(FLBSTUD, ios::in|ios::binary);
ofstream fl2;
if(!fl1){ //If file does not exist, create new file
fl2.open(FLBSTUD,ios::out|ios::binary);
fl2.write((char*)&obj, sizeof(obj));
fl2.close();
cout<<"Record successfully inserted !\n";
return;
}
while(!fl1.eof()){
fl1.read((char*)&obj2,sizeof(obj));
if(fl1.eof()){
break;
}
if(obj.retRollNo()==obj2.retRollNo()){ //If record with same Roll No. exists, then abort insertion
cout<<"Record with same Roll No. with following details already exists : \n";
obj2.printDetails();
cout<<"Insertion Aborted !\n";
return;
}
else if(strcmpi(obj.retStudentName(),obj2.retStudentName())==0){
if (!v) //Warns user that Record with same name exists
cout<<"Warning : Record with same name exists with follwing details : \n";
obj2.printDetails();
cout<<'\n';
v=1;
}
}
if(v){
cout<<"Do you still wish to insert record (Y/N) ? ";
do{ //Asks for user confirmation after warning
cin>>ch;
ch = toupper(ch);
}while(ch!= 'Y' && ch!='N');
if(ch=='N'){
cout<<"Insertion Aborted !\n";
return;
}
}
fl2.open(FLBSTUD,ios::out|ios::app|ios::binary);
fl2.seekp(0,ios::end);
fl2.write((char*)&obj, sizeof(obj));
fl2.close();
cout<<"Record Inserted successfully !\n";
}
void insertClass(){ //Insert Class Record in File
system("CLS");
Class obj,obj2;
char ch;
int v=0;
cout<<"Enter Class Details :\n";
obj.getDetails();
fstream fl1(FLBCLAS, ios::in|ios::binary);
ofstream fl2;
if(!fl1){ //Create new file if it does not exist
fl2.open(FLBCLAS,ios::out|ios::binary);
fl2.write((char*)&obj, sizeof(obj));
fl2.close();
cout<<"Record Inserted successfully !\n";
return;
}
while(!fl1.eof()){
fl1.read((char*)&obj2,sizeof(obj));
if(fl1.eof()){
break;
}
if(obj.retRollNo()==obj2.retRollNo()){ //Abort if same Roll No already exists
cout<<"Record with same Roll No. with following details already exists : \n";
obj2.printDetails();
cout<<"Insertion Aborted !\n";
return;
}
else if(strcmpi(obj.retStudentName(),obj2.retStudentName())==0){
if (!v) //Warns user if record with same Roll No. Already Exists
cout<<"Warning : Record with same name exists with follwing details : \n";
obj2.printDetails();
cout<<'\n';
v=1;
}
}
if(v){ //Asks for confirmation after warning
cout<<"Do you still wish to insert record (Y/N) ? ";
do{
cin>>ch;
ch = toupper(ch);
}while(ch!= 'Y' && ch!='N');
if(ch=='N'){
cout<<"Insertion Aborted !\n";
return;
}
}
fl2.open(FLBCLAS,ios::out|ios::app|ios::binary);
fl2.seekp(0,ios::end);
fl2.write((char*)&obj, sizeof(obj));
fl2.close();
cout<<"Record Inserted successfully !\n";
}
int dispClassRecord(){ //Display all Class Records
Class obj;
int v=0;
fstream fl(FLBCLAS, ios::in|ios::binary);
if(!fl){ //If file does not exist
cout<<"Empty Records !\n";
return 0;
}
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
v=1;
obj.printDetails();
RULE('-');
}
fl.close();
if(!v)
cout<<"Empty Records !\n";
return v;
}
int dispStudentRecord(){ //Display all Student Records
system("CLS");
Student obj;
int v=0;
fstream fl(FLBSTUD, ios::in|ios::binary);
if(!fl){ //If file does not exist
cout<<"Empty Records !\n";
return 0;
}
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
v=1;
obj.printDetails();
RULE('-');
}
fl.close();
if(!v)
cout<<"Empty Records !\n";
return v;
}
int searchClassID(const string str = "search"){ //Searching Class Record by different Attributes
fstream fl(FLBCLAS,ios::in|ios::ate|ios::binary);
if((!fl)||fl.tellg()==0){ //If file is empty or zero size
cout<<"No Records Found !\n";
return 0;
}
fl.close();
fl.open(FLBCLAS,ios::in|ios::binary);
cout<<"Enter class to "<<str<<" (0 to disable) : "; //0 to search independent of class
int cl;
char ch;
char query[30];
Class obj;
int found = 0;
do{
cin>>cl;
}while(cl>12 || cl<0);
cout<<"Enter Attribute to search :\n";
cout<<" (N)ame of Student.\n";
cout<<" (S)ubject.\n";
cout<<"Enter your choice : ";
do{
cin>>ch;
ch = toupper(ch);
}while(ch!='N' && ch!='S');
cout<<"Enter Query : ";
do{
gets(query);
}while(strlen(query)==0);
while(!fl.eof()){
fl.read((char*)&obj,sizeof(obj));
if(fl.eof()){
break;
}
if(CL(cl,obj.retClass())){ //Check class using the defined Macro
if((strcmpi(query,obj.retString(ch))==0)){
if(!found)
cout<<"\nSearch Results : \n\n";
obj.printDetails();
RULE('-');
found = 1;
}
}
}
if(!found)
cout<<"No Records Found !\n";
fl.close();
return found;
}
int searchStudentID(const string str = "search"){ //Search Student by Attributes
system("CLS");
fstream fl(FLBSTUD,ios::in|ios::ate|ios::binary);
if((!fl)||fl.tellg()==0){ //If file is empty or zero size
cout<<" No Records Found !\n";
return 0;
}
fl.close();
fl.open(FLBSTUD,ios::in|ios::binary);
char ch;
char query[30];
Student obj;
int found = 0;
cout<<"Enter Attribute to "<<str<<" :\n";
cout<<" (T)itle.\n";
cout<<" (N)ame of Student.\n";
cout<<" (F)ather's Name.\n";
cout<<" (M)other's Name.\n";
cout<<" (A)ddress.\n";
cout<<" (B)lood Group.\n";
cout<<"Enter your choice : ";
do{
cin>>ch;
ch = toupper(ch);
}while(ch!='T' && ch!='N' && ch!='F' && ch!='M' && ch!='A' && ch!='B');
cout<<"\nEnter Query : ";
do{
gets(query);
}while(strlen(query)==0);
while(!fl.eof()){
fl.read((char*)&obj,sizeof(obj));
if(fl.eof()){
break;
}
if((strcmpi(query,obj.retString(ch))==0)){
if(!found)
cout<<"\nSearch Results : \n\n";
obj.printDetails();
RULE('-');
found = 1;
}
}
if(!found)
cout<<"No Records Found !\n";
fl.close();
return found;
}
int searchByRollNo(int i){ //Search Record by Roll No., 1 for Class, 2 for Student
system("CLS");
int r;
if(i==1){
Class obj;
int found = 0;
int cl;
cout<<"Enter class to search in (0 to disable) : "; //0 to search independent of class
do{
cin>>cl;
}while(cl>12 || cl<0);
cout<<"Enter Roll No. to search for : ";
cin>>r;
fstream fl(FLBCLAS,ios::in|ios::binary);
if(!fl){ //No file exists
cout<<"No Records Found !\n";
return 0;
}
while(!fl.eof()){
fl.read((char*)&obj,sizeof(obj));
if(fl.eof()){
break;
}
if(CL(cl,obj.retClass())){
if(r==obj.retRollNo()){ //Match attribute for each Record
if(!found)
cout<<"\nSearch Results : \n\n";
obj.printDetails();
RULE('-');
found = 1;
}
}
}
if(!found)
cout<<"No Records Found !\n";
fl.close();
return found;
}
else{
int found=0;
Student obj;
cout<<"Enter Roll No. to search for : ";
cin>>r;
fstream fl(FLBSTUD,ios::in|ios::binary);
if(!fl){ //No file exists
cout<<"No Records Found !\n";
return 0;
}
while(!fl.eof()){
fl.read((char*)&obj,sizeof(obj));
if(fl.eof()){
break;
}
if(r==obj.retRollNo()){
if(!found)
cout<<"\nSearch Results : \n\n";
obj.printDetails();
RULE('-');
found = 1;
}
}
if(!found)
cout<<"No Records Found !\n";
fl.close();
return found;
}
}
void sortByStudents(char ch){ //Sort Records
vector <pair<string,int> > lst; //Make vector of pairs of string to sort by and Roll No.
int i;
if(ch=='C'){ //Sort Class Records
Class obj;
int v=0;
fstream fl(FLBCLAS, ios::in|ios::binary);
if(!fl){
cout<<"Empty Records !\n";
return;
}
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
v=1;
lst.push_back(make_pair(obj.retString('N'),obj.retRollNo())); //Push each pair in the vector
}
fl.close();
if(v==0){
cout<<"Empty Records !\n";
return;
}
sort(lst.begin(),lst.end(),strcmpis); //Sort using <algorithm> sort and Custom Comparison
fstream tmp("temp.txt",ios::out|ios::binary);
fl.open(FLBCLAS,ios::in|ios::binary);
fl.seekg(0,ios::beg);
for(i=0;i<lst.size();i++){
fl.close();
fl.open(FLBCLAS,ios::in|ios::binary);
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
if(obj.retRollNo()==lst[i].second){ //Check each Roll No. from each pair and write record to new file
tmp.write((char*)&obj,sizeof(obj));
}
}
}
fl.close();
tmp.close();
remove(FLBCLAS);
rename("temp.txt",FLBCLAS);
cout<<"\nThe Records have been successfully sorted !\n\n";
dispClassRecord();
}
else{ //Sort Student Records
Student obj;
int v=0;
char c;
fstream fl(FLBSTUD, ios::in|ios::binary);
system("CLS");
cout<<"Enter criteria to sort :\n";
cout<<" (N)ame of Student.\n";
cout<<" (T)itle.\n";
cout<<"Enter your choice : \n";
do{
cin>>c;
c=toupper(c);
}while(c!='N' && c!='T');
if(!fl){
cout<<"Empty Records !\n";
return;
}
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
v=1;
lst.push_back(make_pair(obj.retString(c),obj.retRollNo())); //Push each pair in the vector
}
fl.close();
if(v==0){
cout<<"Empty Records !\n";
return;
}
sort(lst.begin(),lst.end(),strcmpis); //Sort using <algorithm> sort and Custom Comparison
fstream tmp("temp.txt",ios::out|ios::binary);
fl.open(FLBSTUD,ios::in|ios::binary);
fl.seekg(0,ios::beg);
for(i=0;i<lst.size();i++){
fl.close();
fl.open(FLBSTUD,ios::in|ios::binary);
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
if(obj.retRollNo()==lst[i].second){ //Check each Roll No. from each pair and write record to new file
tmp.write((char*)&obj,sizeof(obj));
}
}
}
fl.close();
tmp.close();
remove(FLBSTUD);
rename("temp.txt",FLBSTUD);
cout<<"\nThe Records have been successfully sorted !\n\n";
dispStudentRecord();
}
}
void delClassRecord(){ //Delete Class Records
system("CLS");
Class obj; //Writes to new file except record to be deleted
int f=0;
if(!searchClassID("delete from"))
return;
cout<<"\nEnter Roll No. to delete : ";
int r;
char ch;
cin>>r;
fstream fl(FLBCLAS, ios::in|ios::binary);
fstream fo("temp.dat", ios::out|ios::binary);
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
if (r==obj.retRollNo()){
cout<<"Record with following info will be deleted :\n\n";
obj.printDetails();
cout<<"Do you wish to continue ? (Y/N) : ";
do{
cin>>ch;
ch = toupper(ch);
}while(ch!='N' && ch!='Y');
if(ch=='N'){
cout<<"Deletion Aborted !\n";
fl.close();
fo.close();
remove("temp.dat");
return;
}
f=1;
continue;
}
fo.write((char*)&obj,sizeof(obj));
}
fl.close();
fo.close();
remove(FLBCLAS);
rename("temp.dat",FLBCLAS);
if(f)
cout<<"Record Successfully Deleted !\n";
else
cout<<"No Such Record Exists !\n";
}
void delStudentRecord(){ //Delete Student Records
system("CLS");
Student obj; //Writes to new file except record to be deleted
int f=0;
if(!searchStudentID("delete using"))
return;
cout<<"\nEnter Roll No. to delete : ";
int r;
char ch;
cin>>r;
fstream fl(FLBSTUD, ios::in|ios::binary);
fstream fo("temp.dat", ios::out|ios::binary);
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
if (r==obj.retRollNo()){
cout<<"Record with following info will be deleted :\n\n";
obj.printDetails();
cout<<"Do you wish to continue ? (Y/N) : ";
do{
cin>>ch;
ch = toupper(ch);
}while(ch!='N' && ch!='Y');
if(ch=='N'){
cout<<"Deletion Aborted !\n";
fl.close();
fo.close();
remove("temp.dat");
return;
}
f=1;
continue;
}
fo.write((char*)&obj,sizeof(obj));
}
fl.close();
fo.close();
remove(FLBSTUD);
rename("temp.dat",FLBSTUD);
if(f)
cout<<"Record Successfully Deleted !\n";
else
cout<<"No Such Record Exists !\n";
}
int checkNoInClass(){ //Prints total number of students in each class
system("CLS");
int cl[12]={0,0,0,0,0,0,0,0,0,0,0,0},i,found =0;
Class obj;
int cnt=0;
fstream fl(FLBCLAS,ios::in|ios::binary);
if(!fl){
cout<<"No Records Found !\n";
return 0;
}
while(!fl.eof()){
fl.read((char*)&obj,sizeof(obj));
if(fl.eof()){
break;
}
found=1;
cl[obj.retClass()-1]++; //Gets each record from file and stores count of each class [c-p cp]
}
if(!found)
cout<<"No Records Found !\n";
fl.close();
cout<<"\tNumber of Students in :\n";
for(i=0;i<12;i++){
cout<<"\t\tClass "<<i+1<<((i>8)?" ":" ")<<" :\t";
printf("%3d",cl[i]);
cout<<'\n';
cnt+=cl[i];
}
cout<<"\t\t-------------------\n";
cout<<"\t\tTotal Number :\t";
printf("%3d", cnt);
cout<<"\n\t\t-------------------\n";
return found;
}
int dispByStandard(){ //Display each Class record by Standard
Class obj;
int v=0,cl=1,cnt;
fstream fl(FLBCLAS, ios::in|ios::binary);
if(!fl){
cout<<"No Records Found !\n";
return 0;
}
for(cl=1;cl<=12;cl++){
cnt=0;
fl.close();
fl.open(FLBCLAS, ios::in|ios::binary); //Starts with Class 1 to 12 and checks each class
while(!fl.eof()){
fl.read((char*)&obj, sizeof(obj));
if(fl.eof())
break;
v=1;
if(obj.retClass()==cl){
if(cnt==0){
RULE('*');
cout<<"\t\t\t\t Class "<<cl;
RULE('*');
cnt=1;
}
obj.printDetails(0);
RULE('-');
}
}
}
fl.close();
if(!v)
cout<<"No Records Found !\n";
return v;
}
int totalRevenueGenerated(){ //Calculates total fee based on Total=Sum(Fee for Each class * Total students in that class)
system("CLS");
int i,found =0;
Class obj;
int total = 0;
fstream fl(FLBCLAS,ios::in|ios::binary);
if(!fl){
cout<<" \n\n Total Fee Revenue Generated : \t"<<total<<".00"<<'\n';
return 0;
}
while(!fl.eof()){
fl.read((char*)&obj,sizeof(obj));
if(fl.eof()){
break;
}
total += ::fee[obj.retClass()-1];
}
fl.close();
cout<<" \n\n Total Fee Revenue Generated : \t"<<total<<".00"<<'\n';
return found;
}
void modEntry(char c){ //Modify Record Entry
system("CLS");
if(c=='C'){
Class obj;
if(!searchClassID("search for"))
return; //Searches for Records and Modifies using Roll No. based on attribute
fstream fl(FLBCLAS,ios::in|ios::binary);
int r,pos;
char ch;
int found = 0;
cout<<"Enter Roll No. of Record to modify : ";
do{
cin>>r;
}while(r<1);
while(!fl.eof()){
pos=fl.tellg();
fl.read((char*)&obj,sizeof(obj));
if(fl.eof())
break;
if(r==obj.retRollNo()){
cout<<'\n';
fl.close();
fl.open(FLBCLAS,ios::out|ios::in|ios::binary);
while(pos--) fl.get();
cout<<"Enter Attribute to modify :\n";
cout<<" (N)ame of Student.\n";
cout<<" (C)lass Standard.\n";
cout<<" (R)oll No.\n";
cout<<" (S)ubject.\n";
cout<<"Enter your choice : ";
do{
cin>>ch;
ch = toupper(ch);
}while(ch!='N' && ch!='C' && ch!='R' && ch!='S');
obj.modDetail(ch);
obj.printDetails();
fl.write((char*)&obj,sizeof(obj));
RULE('-');
break;
}
}
return;
}
Student obj;
if(!searchStudentID("search for"))
return; //Searches for Records and Modifies using Roll No. based on attribute
fstream fl(FLBSTUD,ios::in|ios::binary);
int r,pos;
char ch;
int found = 0;
cout<<"Enter Roll No. of Record to modify : ";
do{
cin>>r;
}while(r<1);
while(!fl.eof()){
pos=fl.tellg();
fl.read((char*)&obj,sizeof(obj));
if(fl.eof())
break;
if(r==obj.retRollNo()){
cout<<'\n';
fl.close();