#include <iostream>
#include <string>
using namespace std;
#define MAX_STUDENTS 50
#define MAX_SUBJECTS 5
// Structure for Date
struct myDate {
int dd; // day
int mm; // month
int yy; // year
};
// Structure for Student Information
struct studInfo {
string name;
string fname; // Father's name
int id;
int marks[MAX_SUBJECTS];
int total_marks;
float percentage;
char grade;
float gpa; // GPA field
myDate dob; // Date of Birth
myDate doa; // Date of Admission
};
// FUNCTION PROTOTYPES
void input_student(studInfo students[], int& count);
void add_student(studInfo students[], int& count);
int find_student(const studInfo students[], int count, int id);
void update_student(studInfo students[], int count, int id);
void sort_student(studInfo students[], int count);
void remove_student(studInfo students[], int& count, int id);
void display_students(const studInfo students[], int count);
void fill_student_info(studInfo& student);
// FUNCTION DEFINITIONS
void fill_student_info(studInfo& student) {
cout << "NAME: " << endl;
[Link]();
getline(cin, [Link]);
cout << "FATHER'S NAME: " << endl;
getline(cin, [Link]);
do {
cout << "ID (4 digits): " << endl;
cin >> [Link];
if ([Link] < 1000 || [Link] > 9999) {
cout << "Invalid ID! It must be a 4-digit number." << endl;
}
} while ([Link] < 1000 || [Link] > 9999);
// Input and validate Date of Birth
do {
cout << "Enter Date of Birth (dd mm yyyy): ";
cin >> [Link] >> [Link] >> [Link];
if ([Link] < 1 || [Link] > 31) {
cout << "Invalid day! Please enter a day between 1 and 31." << endl;
} else if ([Link] < 1 || [Link] > 12) {
cout << "Invalid month! Please enter a month between 1 and 12." <<
endl;
} else if ([Link] < 1900 || [Link] > 2023) {
cout << "Invalid year! Please enter a year between 1900 and 2023." <<
endl;
}
} while ([Link] < 1 || [Link] > 31 ||
[Link] < 1 || [Link] > 12 ||
[Link] < 1900 || [Link] > 2023);
// Input and validate Date of Admission
do {
cout << "Enter Date of Admission (dd mm yyyy): ";
cin >> [Link] >> [Link] >> [Link];
if ([Link] < 1 || [Link] > 31) {
cout << "Invalid day! Please enter a day between 1 and 31." << endl;
} else if ([Link] < 1 || [Link] > 12) {
cout << "Invalid month! Please enter a month between 1 and 12." <<
endl;
} else if ([Link] < 1900 || [Link] > 2023) {
cout << "Invalid year! Please enter a year between 1900 and 2023." <<
endl;
}
} while ([Link] < 1 || [Link] > 31 ||
[Link] < 1 || [Link] > 12 ||
[Link] < 1900 || [Link] > 2023);
// Validate year difference between DOB and DOA
while (([Link] - [Link]) < 20) {
cout << "Date of admission must be at least 20 years after date of birth!"
<< endl;
cout << "Enter Date of Admission (dd mm yyyy): ";
cin >> [Link] >> [Link] >> [Link];
}
student.total_marks = 0;
cout << "Enter marks for " << MAX_SUBJECTS << " subjects:" << endl;
for (int j = 0; j < MAX_SUBJECTS; j++) {
do {
cout << "Subject " << j + 1 << " (0-100): " << endl;
cin >> [Link][j];
if ([Link][j] < 0 || [Link][j] > 100) {
cout << "Invalid marks! Please enter a value between 0 and 100." <<
endl;
}
} while ([Link][j] < 0 || [Link][j] > 100);
student.total_marks += [Link][j];
}
[Link] = (student.total_marks * 100.0) / (MAX_SUBJECTS * 100.0);
// Set grade based on percentage
if ([Link] >= 80) [Link] = 'A';
else if ([Link] >= 70) [Link] = 'B';
else if ([Link] >= 60) [Link] = 'C';
else if ([Link] >= 50) [Link] = 'D';
else [Link] = 'F';
do {
cout << "Enter GPA (0.0 - 4.0): " << endl;
cin >> [Link];
if ([Link] < 0.0 || [Link] > 4.0) {
cout << "Invalid GPA! Please enter a value between 0.0 and 4.0." <<
endl;
}
} while ([Link] < 0.0 || [Link] > 4.0);
}
void input_student(studInfo students[], int& count) {
do {
cout << "Enter the number of students [MAX " << MAX_STUDENTS << "]: " <<
endl;
cin >> count;
if (count < 1 || count > MAX_STUDENTS) {
cout << "Invalid input! Please enter a number between 1 and " <<
MAX_STUDENTS << "." << endl;
}
} while (count < 1 || count > MAX_STUDENTS);
for (int i = 0; i < count; i++) {
cout << "Enter details for student " << i + 1 << ":" << endl;
fill_student_info(students[i]);
}
}
void add_student(studInfo students[], int& count) {
if (count < MAX_STUDENTS) {
cout << "Enter details for new student (" << count + 1 << "):" << endl;
fill_student_info(students[count]);
count++;
} else {
cout << "No more space to add new record!!" << endl;
}
}
int find_student(const studInfo students[], int count, int id) {
for (int i = 0; i < count; i++) {
if (id == students[i].id) {
return i;
}
}
return -1; // not found
}
void sort_student(studInfo students[], int count) {
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (students[i].id > students[j].id) {
studInfo temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
cout << "Data sorted successfully!" << endl;
}
void remove_student(studInfo students[], int& count, int id) {
int rem = find_student(students, count, id);
if (rem != -1) {
for (int i = rem; i < count - 1; i++) {
students[i] = students[i + 1];
}
count--; // Correctly update the count
cout << "Student removed successfully." << endl;
} else {
cout << "Student not found, cannot be removed!" << endl;
}
}
void update_student(studInfo students[], int count, int id) {
int pos = find_student(students, count, id);
if (pos != -1) {
cout << "OLD RECORD:" << endl;
cout << "NAME: " << students[pos].name << endl;
cout << "FATHER'S NAME: " << students[pos].fname << endl;
cout << "ID: " << students[pos].id << endl;
cout << "DATE OF BIRTH: " << students[pos].[Link] << "/" <<
students[pos].[Link] << "/" << students[pos].[Link] << endl;
cout << "DATE OF ADMISSION: " << students[pos].[Link] << "/" <<
students[pos].[Link] << "/" << students[pos].[Link] << endl;
cout << "MARKS: ";
for (int j = 0; j < MAX_SUBJECTS; j++) {
cout << students[pos].marks[j] << " ";
}
cout << endl;
cout << "TOTAL MARKS: " << students[pos].total_marks << endl;
cout << "PERCENTAGE: " << students[pos].percentage << endl;
cout << "GRADE: " << students[pos].grade << endl;
cout << "GPA: " << students[pos].gpa << endl;
cout << "Enter new details:" << endl;
fill_student_info(students[pos]);
cout << "Student record updated successfully!" << endl;
} else {
cout << "Student not found!" << endl;
}
}
void display_students(const studInfo students[], int count) {
if (count == 0) {
cout << "No students to display." << endl;
return;
}
cout << "Displaying student records:" << endl;
for (int i = 0; i < count; i++) {
cout << "NAME: " << students[i].name << endl;
cout << "FATHER'S NAME: " << students[i].fname << endl;
cout << "ID: " << students[i].id << endl;
cout << "DATE OF BIRTH: " << students[i].[Link] << "/" <<
students[i].[Link] << "/" << students[i].[Link] << endl;
cout << "DATE OF ADMISSION: " << students[i].[Link] << "/" <<
students[i].[Link] << "/" << students[i].[Link] << endl;
cout << "MARKS: ";
for (int j = 0; j < MAX_SUBJECTS; j++) {
cout << students[i].marks[j] << " ";
}
cout << endl;
cout << "TOTAL MARKS: " << students[i].total_marks << endl;
cout << "PERCENTAGE: " << students[i].percentage << endl;
cout << "GRADE: " << students[i].grade << endl;
cout << "GPA: " << students[i].gpa << endl;
cout << "--------------------------" << endl;
}
}
void search_student(const studInfo students[], int count, int id) {
int pos = find_student(students, count, id);
if (pos != -1) {
cout << "STUDENT FOUND:" << endl;
cout << "NAME: " << students[pos].name << endl;
cout << "FATHER'S NAME: " << students[pos].fname << endl;
cout << "ID: " << students[pos].id << endl;
cout << "DATE OF BIRTH: " << students[pos].[Link] << "/" <<
students[pos].[Link] << "/" << students[pos].[Link] << endl;
cout << "DATE OF ADMISSION: " << students[pos].[Link] << "/" <<
students[pos].[Link] << "/" << students[pos].[Link] << endl;
cout << "MARKS: ";
for (int j = 0; j < MAX_SUBJECTS; j++) {
cout << students[pos].marks[j] << " ";
}
cout << endl;
cout << "TOTAL MARKS: " << students[pos].total_marks << endl;
cout << "PERCENTAGE: " << students[pos].percentage << endl;
cout << "GRADE: " << students[pos].grade << endl;
cout << "GPA: " << students[pos].gpa << endl;
} else {
cout << "Student not found!" << endl;
}
}
int main() {
studInfo students[MAX_STUDENTS];
int count = 0;
int choice, id;
do {
cout << "Menu:" << endl;
cout << "1. Input Students" << endl;
cout << "2. Add Student" << endl;
cout << "3. Update Student" << endl;
cout << "4. Remove Student" << endl;
cout << "5. Sort Students" << endl;
cout << "6. Display Students" << endl;
cout << "7. Search Student" << endl; // New option
cout << "8. Exit" << endl;
cout << "Choose an option: " << endl;
cin >> choice;
switch (choice) {
case 1:
input_student(students, count);
break;
case 2:
add_student(students, count);
break;
case 3:
cout << "Enter ID of student to update: " << endl;
cin >> id;
update_student(students, count, id);
break;
case 4:
cout << "Enter ID of student to remove: " << endl;
cin >> id;
remove_student(students, count, id);
break;
case 5:
sort_student(students, count);
break;
case 6:
display_students(students, count);
break;
case 7: // Search student case
cout << "Enter ID of student to search: " << endl;
cin >> id;
search_student(students, count, id);
break;
case 8:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (true);
return 0;
}