-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1014 lines (861 loc) · 30.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
/*
WELCOME TO INTEC STUDENT DATA MANAGEMENT SYSTEM!
We (Irfan, Iyas & Khilfi) proudly present our project source code to you.
All in all, the source code is separated into three main parts from top to bottom. The sections are listed below:
1. Headers and Initializations
2. Main Program
3. Prototypes and Function Declarations
Each function and global variable have been documented to provide clarification of their existence. You can view each
function purpose on top of its function header to know its purpose and return value.
Lastly, we hope this source code serve as a prime example of what a structured and readable code should be. Please
contact us if you have further inquiries or bug reports on this project.
Thanks ^_^
Github: https://github.com/KhilfiKhairulAmin/uscs20-project
*/
/*============================================== SECTION 1: HEADERS AND INITIALIZATIONS ==============================================*/
#include <iostream> // For user input and output
#include <fstream> // For file operations
#include <algorithm> // For string processing
#include <iomanip> // For data manipulation
using namespace std;
// Student datatype
struct Student
{
int id = -1;
string firstName;
string lastName;
int age;
string icNumber;
string programme;
int numOfSubjects;
double cgpa;
};
// Maximum size of array
const int MAX_SIZE = 200;
// Store Students data
Student Students[MAX_SIZE];
// Store the current user's username
string globalUsername;
/*===================================================== SECTION 2: MAIN PROGRAM =====================================================*/
void authUI();
void mainUI();
int main()
{
system("Color 70");
cout << "+======================================================================================================================+\n"
<< "+ WELCOME TO INTEC STUDENT DATA ATLAS! +\n"
<< "+======================================================================================================================+\n";
// Authorize the user by signing up or logging in
authUI();
// User has logged in successfully
// Go to the main menu
mainUI();
return 0;
}
/*========================================= SECTION 3: PROTOTYPES AND FUNCTION DECLARATIONS =========================================*/
/*++++++++++++ SECTION 3A: PROTOTYPES ++++++++++++*/
// Prototypes for sign up and login functionality
void signUp(),
login();
// Prototypes for file operation functionality
void readStudents(),
writeStudents();
// Prototypes for student data management functionality
void viewStudents(),
addStudent(),
editStudent(),
deleteStudent(),
searchStudents();
// Prototypes for utility functionality
int findStudent(int),
lenStudents();
void printStudent(Student),
printStudentTable(Student[MAX_SIZE]),
pressEnterToContinue(string = "Press Enter to continue... "),
// For data validation purpose
inputStringData(string&, string, int, int = 1, int = 22),
inputChoice(char&, string),
inputIntData(int&, string),
inputProgramme(string&, string),
inputCgpa(double&, string);
/*++++++ SECTION 3A: FUNCTION DECLARATIONS ++++++*/
/**
* Authorization user interface
*/
void authUI()
{
char choice;
// Input choice ('y' or 'n')
inputChoice(choice, "\nDo you have an existing account? [Y/n]: ");
if (choice == 'y')
login();
else if (choice == 'n')
signUp();
return;
}
/**
* Main program interface
*/
void mainUI()
{
readStudents();
while (true)
{
// Clear the terminal on each loop
system("cls");
char select;
cout << "+======================================================================================================================+\n"
<< "+ MAIN MENU +\n"
<< "+======================================================================================================================+\n"
<< "Welcome " << globalUsername << "!\n\n"
<< "What action do you want to perform?\n"
<< "1.View \n"
<< "2.Search \n"
<< "3.Add \n"
<< "4.Edit \n"
<< "5.Delete \n"
<< "6.Exit \n";
cout << "\nPlease make your selection [1-6]: ";
cin >> select;
cin.ignore(); // Ignore '\n' character
switch(select)
{
case '1':
viewStudents();
break;
case '2':
searchStudents();
break;
case '3':
addStudent();
break;
case '4':
editStudent();
break;
case '5':
deleteStudent();
break;
case '6':
system("cls");
cout << "+======================================================================================================================+\n"
<< "+ HAVE A GREAT DAY! +\n"
<< "+======================================================================================================================+\n";
cout << "\nLogging out " << globalUsername << "...\n"
<< "\nKACHING~DONE!\n"
<< "\nCheck out our project on GitHub: https://github.com/KhilfiKhairulAmin/uscs20-project\n";
return;
default:
cout << "Invalid selection.\n";
}
}
}
/**
* Register new account
*/
void signUp()
{
// Clear the terminal
system("cls");
string username, password, repeatPassword, fullname, program;
int age, nosub, studentid, year, day, month;
cout << "+======================================================================================================================+\n";
cout << "+ ACCOUNT REGISTRATION +\n";
cout << "+======================================================================================================================+\n";
bool validUsername = false;
// Username input and validation
do
{
// Begin with assumption that username is valid
validUsername = true;
// Input username
inputStringData(username, "Username: ", 1);
// Read the username already registered
ifstream readAccount("accounts.txt");
string usernameRegistered, _;
// Validate whether the username has been taken or not
readAccount >> usernameRegistered >> _;
while (readAccount.good())
{
// Checks if username is already taken
if(username == usernameRegistered)
{
// Username is not valid because it already used by other user
validUsername = false;
readAccount.close();
cout << "The username is already taken. Please choose other username.\n";
break;
}
readAccount >> usernameRegistered;
}
} while (!validUsername);
globalUsername = username;
// Password input and validation
do
{
// Password input
inputStringData(password, "Password: ", 1, 8);
system("cls");
cout << "+======================================================================================================================+\n";
cout << "+ ACCOUNT REGISTRATION +\n";
cout << "+======================================================================================================================+\n";
// Repeat the same password
inputStringData(repeatPassword, "Repeat password: ", 1);
if (password == repeatPassword)
break;
system("cls");
cout << "+======================================================================================================================+\n";
cout << "+ ACCOUNT REGISTRATION +\n";
cout << "+======================================================================================================================+\n";
cout << "The repeat password was not correct. Please re-enter password. \n\n";
}
while (true);
// Write this account at the end of the text file
ofstream writeAccount("accounts.txt", ios::app);
writeAccount << username << " " << password << endl;
writeAccount.close();
return;
}
/**
* Login account
*/
void login()
{
// Clear the terminal
system("cls");
cout << "+======================================================================================================================+\n";
cout << "+ ACCOUNT LOG IN +\n";
cout << "+======================================================================================================================+\n";
do
{
string usernameIn, passwordIn, username, password;
// Input username
inputStringData(usernameIn, "Please enter username: ", 1);
// Input password
inputStringData(passwordIn, "Please enter password: ", 1);
ifstream readAccount("accounts.txt");
readAccount >> username >> password;
while (readAccount.good())
{
if(usernameIn == username && passwordIn == password)
{
globalUsername = username;
readAccount.close();
return;
}
readAccount >> username >> password;
}
readAccount.close();
cout << "\nFail: Username or password is incorrect.\n\n";
}
while(true);
}
/**
* Retrieve all student data from text file into the Students array
*/
void readStudents()
{
// Open student text file
ifstream readStudentsData("students.txt");
// Variables to store Student data
int studentId, age, numOfSubjects;
double cgpa;
string firstName, lastName, icNumber, programme;
int j = 0;
// Read next line
readStudentsData >> studentId >> firstName >> lastName >> age >> icNumber
>> programme >> numOfSubjects >> cgpa;
// No student exists in file
if (studentId <= 0)
return;
while (readStudentsData.good())
{
// Parse the first and last name
replace(firstName.begin(), firstName.end(), '_', ' ');
replace(lastName.begin(), lastName.end(), '_', ' ');
// Assign each `Student` into the global array
Student student;
student.id = studentId;
student.firstName = firstName;
student.lastName = lastName;
student.age = age;
student.icNumber = icNumber;
student.programme = programme;
student.numOfSubjects = numOfSubjects;
student.cgpa = cgpa;
Students[j++] = student;
// Read next line
readStudentsData >> studentId >> firstName >> lastName >> age >> icNumber
>> programme >> numOfSubjects >> cgpa;
}
// Close the file
readStudentsData.close();
return;
}
/**
* Save all student data into text file
*/
void writeStudents()
{
ofstream writeStudents("students.txt");
for (int i = 0; i < lenStudents(); i++)
{
Student s = Students[i];
// Unparse first and last name to store inside text file
replace(s.firstName.begin(), s.firstName.end(), ' ', '_');
replace(s.lastName.begin(), s.lastName.end(), ' ', '_');
// Write to text file
writeStudents << s.id << ' ' << s.firstName << ' ' << s.lastName << ' '
<< s.age << ' ' << s.icNumber << ' ' << s.programme << ' '
<< s.numOfSubjects << ' ' << s.cgpa << endl;
}
// Close the file
writeStudents.close();
return;
}
/**
* Print all student data
*/
void viewStudents()
{
system("cls");
cout << "+======================================================================================================================+\n"
<< "+ INTEC STUDENT DATA ATLAS +\n"
<< "+======================================================================================================================+\n"
<< "\nNumber of Student Records: " << lenStudents() << "/" << MAX_SIZE << "\n\n";
printStudentTable(Students);
pressEnterToContinue("\n[Back to Main Menu]");
}
/**
* Search student based on an attribute. Display the student data if found.
*/
void searchStudents()
{
system("cls");
char searchKey;
Student found[MAX_SIZE];
int count = 0;
cout << "+======================================================================================================================+\n"
<< "+ SEARCH STUDENT DATA +\n"
<< "+======================================================================================================================+\n";
cout << "Please choose attribute to search for:\n"
<< "1.Student ID\n"
<< "2.Name\n"
<< "3.Programme\n"
<< "\nEnter your choice [1-3]: ";
cin >> searchKey;
cin.ignore();
cout << endl;
switch (searchKey)
{
case '1':
{
int idIn;
// Input ID to be searched
inputIntData(idIn, "Student ID to search for: ");
for (int i = 0; i < lenStudents(); i++)
{
if (idIn == Students[i].id)
{
found[count++] = Students[i];
break;
}
}
break;
}
case '2':
{
string nameIn;
inputStringData(nameIn, "Name to search: ", 2);
string fullName;
for (int i = 0; i < lenStudents(); i++)
{
fullName = Students[i].firstName + ' ' + Students[i].lastName;
// Convert nameIn and fullName to lowercase for better search results
for (int k = 0; k < fullName.length(); k++)
{
fullName[k] = (char)tolower(fullName[k]);
}
for (int j = 0; j < nameIn.length(); j++)
{
nameIn[j] = (char)tolower(nameIn[j]);
}
if (fullName.find(nameIn) != fullName.npos)
{
found[count++] = Students[i];
}
}
break;
}
case '3':
{
string programmeIn;
inputProgramme(programmeIn, "Programme to search [1-12]: ");
string programme;
for (int i = 0; i < lenStudents(); i++)
{
programme = Students[i].programme;
// Convert programmeIn and programme to lowercase for better search results
for (int j = 0; j < programmeIn.length(); j++)
{
programmeIn[j] = (char)tolower(programmeIn[j]);
}
for (int k = 0; k < programme.length(); k++)
{
programme[k] = (char)tolower(programme[k]);
}
if (programmeIn.find(programme) != programmeIn.npos)
{
found[count++] = Students[i];
}
}
break;
}
default:
cout << "Invalid option. Search operation cancelled.\n";
pressEnterToContinue("\n[Exit]");
return;
}
if (count == 0)
{
cout << "\nNo student found.\n";
}
else
{
cout << "\nFound: " << count << " record(s)\n";
printStudentTable(found);
}
pressEnterToContinue("\n[Back to Main Menu]");
}
/**
* Create a new student
*/
void addStudent()
{
system("cls");
string firstName, lastName, icNumber, programme;
int age, numOfSubjects;
double cgpa;
// Get the position of empty index in the array
int empty = lenStudents();
// Array is fully used
if (empty == MAX_SIZE)
{
cout << "Can't add Student. Maximum size of " << MAX_SIZE << " students reached.\n";
return;
}
cout << "+======================================================================================================================+\n"
<< "+ ADD NEW STUDENT +\n"
<< "+======================================================================================================================+\n";
// Input first name
inputStringData(firstName, "First Name: ", 2);
// Input last name
inputStringData(lastName, "Last Name: ", 2);
// Input age
inputIntData(age, "Age: ");
// Input IC number
inputStringData(icNumber, "IC Number: ", 1, 12, 12);
// Input programme
inputProgramme(programme, "Choose Programme [1-12]: ");
// Input number of subjects
inputIntData(numOfSubjects, "Number of Subjects: ");
// Input CGPA
inputCgpa(cgpa, "CGPA: ");
// Generate a new Student ID based on highest Student ID + 1
int studentId = Students[lenStudents() - 1].id + 1;
Student newStudent;
newStudent.id = studentId;
newStudent.firstName = firstName;
newStudent.lastName = lastName;
newStudent.age = age;
newStudent.icNumber = icNumber;
newStudent.programme = programme;
newStudent.numOfSubjects = numOfSubjects;
newStudent.cgpa = cgpa;
// Assign to empty location in `Accounts` array
Students[empty] = newStudent;
writeStudents();
cout << "\n+======================================================================================================================+\n";
printStudent(Students[empty]);
cout << "+======================================================================================================================+";
cout << "\n\nStudent is successfully added.\n";
pressEnterToContinue("\n[OK]");
}
/**
* Update the student data to the new one if provided.
*/
void editStudent()
{
system("cls");
string firstName, lastName, icNumber, programme;
int studentId, age, numOfSubjects, pos;
double cgpa;
cout << "+======================================================================================================================+\n"
<< "+ EDIT STUDENT DATA +\n"
<< "+======================================================================================================================+\n";
do
{
// Input student id
inputIntData(studentId, "\nEnter the student ID: ");
// Find position of this student in array
pos = findStudent(studentId);
if (pos == -1)
{
cout << "Student with id of " << studentId << " does not exist.\n";
continue;
}
system("cls");
break;
} while (true);
while (true)
{
char editOption;
cout << "+======================================================================================================================+\n"
<< "+ EDIT STUDENT DATA +\n"
<< "+======================================================================================================================+\n\n";
printStudent(Students[pos]);
cout << "\n\nChoose your action to edit the Student data:\n";
cout << "1.First Name\n"
<< "2.Last Name\n"
<< "3.Age\n"
<< "4.IC Number\n"
<< "5.Programme\n"
<< "6.Number of Subjects\n"
<< "7.CGPA\n"
<< "8.[Exit]\n"
<< "\nEnter your choice [1-8]: ";
cin >> editOption;
cin.ignore();
cout << "\n";
switch (editOption)
{
case '1':
{
cout << "Current First Name: " << Students[pos].firstName << "\n";
inputStringData(firstName, "New First Name: ", 2);
Students[pos].firstName = firstName;
break;
}
case '2':
{
cout << "Current Last Name: " << Students[pos].lastName << "\n" << "\n";
inputStringData(lastName, "New Last Name: ", 2);
Students[pos].lastName = lastName;
break;
}
case '3':
{
cout << "Current Age: " << Students[pos].age << "\n";
inputIntData(age, "New Age: ");
Students[pos].age = age;
break;
}
case '4':
{
cout << "Current IC Number: " << Students[pos].icNumber << "\n";
inputStringData(icNumber, "New IC Number: ", 1, 12, 12);
Students[pos].icNumber = icNumber;
break;
}
case '5':
{
cout << "Current Programme: " << Students[pos].programme << "\n";
inputProgramme(programme, "New Programme [1-12]: ");
Students[pos].programme = programme;
break;
}
case '6':
{
cout << "Current Number of Subjects: " << Students[pos].numOfSubjects << "\n";
inputIntData(numOfSubjects, "New Number of Subjects: ");
Students[pos].numOfSubjects = numOfSubjects;
break;
}
case '7':
{
cout << "Current CGPA: " << Students[pos].cgpa << "\n";
inputCgpa(cgpa, "New CGPA: ");
Students[pos].cgpa = cgpa;
break;
}
case '8':
{
return;
}
default:
cout << "Please enter a number between 1 and 8.\n";
continue;
}
writeStudents();
cout << "\nStudent data updated successfully.\n";
pressEnterToContinue("\n[OK]");
system("cls");
}
}
/**
* Delete a student
*/
void deleteStudent()
{
system("cls");
int studentId, studentIndex;
cout << "+======================================================================================================================+\n"
<< "+ DELETE STUDENT DATA +\n"
<< "+======================================================================================================================+\n";
do
{
// Input student Id
inputIntData(studentId, "\nEnter the student ID: ");
// Find position of this student in array
studentIndex = findStudent(studentId);
if (studentIndex != -1)
{
cout << "Student with id of " << Students[studentIndex].id << " found.\n";
break;
}
cout << "Student with id of " << studentId << " does not exist.\n";
} while (true);
cout << endl;
printStudent(Students[studentIndex]);
char assure;
inputChoice(assure, "\nAre you sure? [Y/n]: ");
if (assure == 'n')
return;
int initLen = lenStudents();
Student reset;
// Reset the data at index
Students[studentIndex] = reset;
// Move other elements forward to fill empty spaces
for (int i = studentIndex; i < initLen - 1; i++)
{
// Swap the position of element in the array
swap(Students[i], Students[i+1]);
}
writeStudents();
cout << "\nStudent has been deleted successfully.\n";
pressEnterToContinue("\n[OK]");
}
/**
* Display information of a Student
*/
void printStudent(Student s)
{
cout << "Student Information"
<< "\n|- Student ID: " << s.id
<< "\n|- First Name: " << s.firstName
<< "\n|- Last Name: " << s.lastName
<< "\n|- Age: " << s.age
<< "\n|- IC Number: " << s.icNumber
<< "\n|- Programme: " << s.programme
<< "\n|- Subjects: " << s.numOfSubjects
<< "\n|- CGPA: " << s.cgpa
<< "\n";
}
/**
* Display Student data in table
*/
void printStudentTable(Student students[MAX_SIZE])
{
cout <<"+======================================================================================================================+\n";
cout <<setw(10)<<setiosflags(ios::left)<<"|ID"<<"|"
<<setw(23)<<setiosflags(ios::left) <<" First Name"<<"|"
<<setw(22)<<setiosflags(ios::left)<<" Last Name"<<"|"
<<setw(10)<<setiosflags(ios::left)<<" Age"<<"|"
<<setw(14)<<setiosflags(ios::left)<<" IC Number"<<"|"
<<setw(8)<<setiosflags(ios::left)<<"Programme"<<"|"
<<setw(19)<<setiosflags(ios::left)<<"Number Of Subjects"<<"|"
<<setw(5)<<setiosflags(ios::left)<<"CGPA"<<"|" << endl;
int i = 0;
while (students[i].id > 0)
{
Student s = students[i++];
cout <<"+======================================================================================================================+\n|";
cout <<setw(9)<<setiosflags(ios::left)<< s.id << "|"
<<setw(23)<<setiosflags(ios::left)<< s.firstName << "|"
<<setw(22)<<setiosflags(ios::left)<< s.lastName << "|"
<<setw(10)<<setiosflags(ios::left)<<s.age << "|"
<<setw(14)<<setiosflags(ios::left)<< s.icNumber << "|"
<<setw(9)<<setiosflags(ios::left)<< s.programme << "|"
<<setw(19)<<setiosflags(ios::left)<< s.numOfSubjects << "|"
<<setw(5)<<setiosflags(ios::left)<<fixed<<setprecision(2)<< s.cgpa <<"|"<< endl;
}
cout << "+======================================================================================================================+\n";
}
/**
* Get the number of elements in the Students array
*/
int lenStudents()
{
// Search for unused position in array (i.e. id which value is -1)
for (int i = 0; i < MAX_SIZE; i++)
{
if (Students[i].id <= 0)
return i;
}
// Array is fully used
return MAX_SIZE;
}
/**
* Find index of `targetId`. Return `-1` if not found.
*/
int findStudent(int targetId)
{
// Search for index of target id
for (int i = 0; i < lenStudents(); i++)
{
if (Students[i].id == targetId)
return i;
}
return -1;
}
/**
* Prompt the user to press Enter to continue the program
*/
void pressEnterToContinue(string message)
{
string _;
cout << message;
getline(cin, _, '\n');
}
/**
* Get and validate choice ('y' or 'n')
*/
void inputChoice(char& choiceIn, string prompt)
{
do
{
cout << prompt;
cin >> choiceIn;
choiceIn = tolower(choiceIn);
cin.ignore();
if (choiceIn != 'n' && choiceIn != 'y')
{
cout << "Hint: Please enter 'y' for yes or 'n' for no.\n";
continue;
}
break;
}
while(true);
}
/**
* Get and validate string input by the user. If mode = 1, get only first word. If mode = 2, get whole input.
*/
void inputStringData(string& stringIn, string prompt, int mode, int minSize, int maxSize)
{
do
{
// Mode 1: Singular word input
// Mode 2: Multiple word Input
cout << prompt;
getline(cin, stringIn, '\n');
// Validate empty
if (stringIn == "")
{
cout << "Data can't be empty.\n";
continue;
}
else if (stringIn.length() > maxSize || stringIn.length() < minSize)
{
cout << "Please enter data between " << minSize << " and " << maxSize << " characters.\n";
continue;
}
if (mode == 1)
{
if (stringIn.find(" ") != stringIn.npos)
{
cout << "Data can't contain spaces.\n";
continue;
}
}
break;
} while (true);
}
/**
* Get and validate integer data
*/
void inputIntData(int& numIn, string prompt)
{
do
{
cout << prompt;
cin >> numIn;
cin.ignore();
if (numIn <= 0)
{
cout << "Number must be more than 0.\n";
continue;
}
break;
} while (true);
}
/**
* Get and validate Programme data
*/
void inputProgramme(string& programmeIn, string prompt)
{
cout << "Programme Number\n"
<< " 1. ACCA\n"
<< " 2. ADTP\n"
<< " 3. ALG\n"
<< " 4. ALUK\n"
<< " 5. CFAB\n"
<< " 6. DIPLOMA\n"
<< " 7. FIA\n"
<< " 8. FRANCE\n"
<< " 9. ICAEW\n"
<< "10. KTJ\n"
<< "11. KOREA\n"
<< "12. SACE\n";
do
{
string choose;
inputStringData(choose, prompt, 1, 1, 2);
if (choose == "1")
programmeIn = "ACCA";
else if (choose == "2")
programmeIn = "ADTP";
else if (choose == "3")
programmeIn = "ALG";
else if (choose == "4")
programmeIn = "ALUK";
else if (choose == "5")
programmeIn = "CFAB";
else if (choose == "6")
programmeIn = "DIPLOMA";
else if (choose == "7")
programmeIn = "FIA";
else if (choose == "8")
programmeIn = "FRANCE";
else if (choose == "9")
programmeIn = "ICAEW";
else if (choose == "10")
programmeIn = "KTJ";
else if (choose == "11")
programmeIn = "KOREA";
else if (choose == "12")
programmeIn = "SACE";
else
{
cout << "Hint: Please enter number between 1 - 12.\n";
continue;
}
break;
} while (true);
}
/**
* Get and validate CGPA data
*/
void inputCgpa(double& cgpaIn, string prompt)
{