// Problem Statement :
//
// You are tasked with developing a Student Information System using C++ that
manages student records.
// The system should be menu-driven, allowing users to interact with it through a
series of options.
// You will implement this system using 2D arrays to store student data.
// The following functionalities must be included in the program MENU:
//
// 1. Enter the Number of Students : The user should be able to input the number of
student records to be managed by the system.
//
// 2. Insert Student Record : The user can enter details such as Student ID, Name, and
Marks for each student.
// These records should be stored in a 2D array.
//
// 3. Update Student Record : The user should be able to update the information of a
student by searching for the Student ID and modifying their record.
//
// 4. Search Student Record : The system should allow the user to search for a student
by Student ID Or by Student Name and display the corresponding record.
//
// 5. Delete Student Record : The system should enable the user to delete a specific
student's record using the Student ID.
// After deletion, the space should be freed up for a new student record.
//
// 6. Display All Records : The system should display all student records stored in the
2D array in a tabular format.
//
// Requirements :
// - The program must be fully menu-driven.
// - Use a 2D array to store student information (Student ID, Name, and Marks).
// - Implement input validation where necessary (e.g., ensuring valid Student IDs).
// - The program should be able to handle at least 10 students.
// - Properly manage array indices to prevent out-of-bounds errors.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int No_Of_Students; // Number of students to be managed
string** students; // 2D dynamic array to store student data
// Function to display the main menu options
void DisplayMenu() {
cout << "\n1. Enter the number of Students\n";
cout << "2. Insert Student Record\n";
cout << "3. Update Student Record\n";
cout << "4. Search Student Record\n";
cout << "5. Delete Student Record\n";
cout << "6. Display All Records\n";
cout << "7. Exit.\n";
}
// Function to set the number of students and initialize the 2D array with required
attributes
void No_OF_Students_Func(int attributes) {
cout << "Enter the number of students( at least 10 ): ";
cin >> No_Of_Students;
/*if (No_Of_Students < 10) {
cout << "Setting min Students to 10.\n";
No_Of_Students = 10;
}*/
students = new string * [No_Of_Students]; // Allocate memory for rows (students)
for (int i = 0; i < No_Of_Students; i++) {
students[i] = new string[attributes]; // Allocate memory for columns
(attributes per student)
}
}
// Function to check if a Student ID is unique
bool ID_Validation(const string& id, int currentCount) {
for (int i = 0; i < currentCount; i++) {
if (students[i][1] == id) {
return false; // ID already exists
}
}
return true; // ID is unique
}
// Function to input student records including Name, ID, and Marks with validation
checks
void Input_Student_Rec() {
int attributes = 3; // Number of attributes per student (Name, ID,
Marks)
int student_Count;
for (student_Count = 0; student_Count < No_Of_Students; student_Count++) {
if (student_Count >= No_Of_Students) {
cout << "Student limit reached. Cannot add more records.\n";
return;
}
cout << "\nDetails of Student " << student_Count + 1 << ": \n";
cout << "Name: ";
cin.ignore();
getline(cin, students[student_Count][0]);
// Loop to ensure ID is unique
while (true) {
cout << "Enter a Unique ID: ";
cin >> students[student_Count][1];
if (ID_Validation(students[student_Count][1], student_Count)) {
break;
}
else {
cout << "ID must be Unique. Please enter again.\n";
}
}
cout << "Marks: ";
cin >> students[student_Count][2];
cout << endl;
}
}
// Function to update an existing student record based on Student ID
void Update_Student_Rec() {
string searchID;
cout << "Enter ID to update Student Record: ";
cin >> searchID;
for (int i = 0; i < No_Of_Students; i++) {
if (searchID == students[i][1]) {
cout << "Updating Record of " << students[i][0];
cout << "\nNew/existing Name: ";
cin.ignore();
getline(cin, students[i][0]);
cout << "New/existing ID( unique ): ";
cin >> students[i][1];
cout << "New Marks: ";
cin >> students[i][2];
return;
}
}
cout << searchID << " doesn't match any record!\n";
}
// Function to search for a student record by Name or ID and display the results
void Search_Student_Record() {
int nameRep = 0; // Counter for repeated names
string searchRec;
cout << "Enter Name or ID to search student's record: ";
cin.ignore();
getline(cin, searchRec);
for (int i = 0; i < No_Of_Students; i++) {
if (searchRec == students[i][0]) {
nameRep++;
if (nameRep > 1) {
cout << "The name " << searchRec << " is found multiple times. Please
enter the unique Student ID: ";
cin >> searchRec;
if (searchRec == students[i][1]) {
cout << "Record Found!\n";
cout << " Name: " << students[i][0] << "\n ID: " << students[i][1] << "\n
Marks: " << students[i][2] << endl;
return;
}
}
}
}
for (int i = 0; i < No_Of_Students; i++) {
if (searchRec == students[i][0] || searchRec == students[i][1]) {
cout << "Record found:\n";
cout << "Name: " << students[i][0] << ", ID: " << students[i][1] << ", Marks: "
<< students[i][2] << endl;
return;
}
}
cout << "Record not found!\n";
}
// Function to delete a student record based on Student ID by clearing their record
fields
void Delete_Student_Record() {
string delID;
bool isRecord = false;
cout << "Enter the student's ID of which you want to delete record: ";
cin >> delID;
for (int i = 0; i < No_Of_Students; i++) {
if (delID == students[i][1]) {
students[i][0] = " "; // Clear Name
students[i][1] = " "; // Clear ID
students[i][2] = " "; // Clear Marks
cout << "Record is deleted ... !\n";
}
isRecord = true;
}
if (!isRecord) {
cout << "Record not found!\n";
}
}
// Function to display all student records in a formatted table
void Display_All_Records() {
cout << string(40, '-') << endl;
cout << left << setw(20) << "Name" << setw(10) << "ID" << setw(10) << "Marks"
<< endl;
cout << string(40, '-') << endl;
bool dataFound = false;
for (int i = 0; i < No_Of_Students; i++) {
if (!students[i][0].empty()) {
cout << left << setw(20) << students[i][0] << setw(10) << students[i][1] <<
setw(10) << students[i][2];
cout << endl;
dataFound = true;
}
}
if (!dataFound) {
cout << string(10, ' ') << "No Data Found...\n";
cout << "Please Go to 1 / 2 and Input Data !\n";
}
}
// Function to exit the program and display a goodbye message
void Exit_Program() {
cout << "\nExiting... ... ... \n";
cout << string(266, '*') << "\n" << string(30, ' ') << "Thanks for Visiting Student
Information System (by Muhammad_Awais).\n\n\n";
}
// Main function that displays the menu and performs actions based on user input
int main() {
int choice;
int attributes = 3; // Number of attributes (Name, ID, Marks)
cout << string(50, ' ') << "STUDENT INFORMATION SYSTEM\n" << string(266, '*') <<
endl;
while (true) {
DisplayMenu();
cout << "\nEnter choice: ";
cin >> choice;
switch (choice) {
case 1:
No_OF_Students_Func(attributes);
break;
case 2:
Input_Student_Rec();
break;
case 3:
Update_Student_Rec();
break;
case 4:
Search_Student_Record();
break;
case 5:
Delete_Student_Record();
break;
case 6:
Display_All_Records();
break;
case 7:
Exit_Program();
exit(0);
default:
cout << "Invalid choice.\n";
}
}
return 0;
}