0% found this document useful (0 votes)
14 views7 pages

Sample 2

Uploaded by

Malik Areeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Sample 2

Uploaded by

Malik Areeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#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;
cin.ignore();
getline(cin, student.name);

cout << "FATHER'S NAME: " << endl;


getline(cin, student.fname);

do {
cout << "ID (4 digits): " << endl;
cin >> student.id;
if (student.id < 1000 || student.id > 9999) {
cout << "Invalid ID! It must be a 4-digit number." << endl;
}
} while (student.id < 1000 || student.id > 9999);

// Input and validate Date of Birth


do {
cout << "Enter Date of Birth (dd mm yyyy): ";
cin >> student.dob.dd >> student.dob.mm >> student.dob.yy;

if (student.dob.dd < 1 || student.dob.dd > 31) {


cout << "Invalid day! Please enter a day between 1 and 31." << endl;
} else if (student.dob.mm < 1 || student.dob.mm > 12) {
cout << "Invalid month! Please enter a month between 1 and 12." <<
endl;
} else if (student.dob.yy < 1900 || student.dob.yy > 2023) {
cout << "Invalid year! Please enter a year between 1900 and 2023." <<
endl;
}
} while (student.dob.dd < 1 || student.dob.dd > 31 ||
student.dob.mm < 1 || student.dob.mm > 12 ||
student.dob.yy < 1900 || student.dob.yy > 2023);

// Input and validate Date of Admission


do {
cout << "Enter Date of Admission (dd mm yyyy): ";
cin >> student.doa.dd >> student.doa.mm >> student.doa.yy;

if (student.doa.dd < 1 || student.doa.dd > 31) {


cout << "Invalid day! Please enter a day between 1 and 31." << endl;
} else if (student.doa.mm < 1 || student.doa.mm > 12) {
cout << "Invalid month! Please enter a month between 1 and 12." <<
endl;
} else if (student.doa.yy < 1900 || student.doa.yy > 2023) {
cout << "Invalid year! Please enter a year between 1900 and 2023." <<
endl;
}
} while (student.doa.dd < 1 || student.doa.dd > 31 ||
student.doa.mm < 1 || student.doa.mm > 12 ||
student.doa.yy < 1900 || student.doa.yy > 2023);

// Validate year difference between DOB and DOA


while ((student.doa.yy - student.dob.yy) < 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 >> student.doa.dd >> student.doa.mm >> student.doa.yy;
}

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 >> student.marks[j];
if (student.marks[j] < 0 || student.marks[j] > 100) {
cout << "Invalid marks! Please enter a value between 0 and 100." <<
endl;
}
} while (student.marks[j] < 0 || student.marks[j] > 100);

student.total_marks += student.marks[j];
}

student.percentage = (student.total_marks * 100.0) / (MAX_SUBJECTS * 100.0);

// Set grade based on percentage


if (student.percentage >= 80) student.grade = 'A';
else if (student.percentage >= 70) student.grade = 'B';
else if (student.percentage >= 60) student.grade = 'C';
else if (student.percentage >= 50) student.grade = 'D';
else student.grade = 'F';

do {
cout << "Enter GPA (0.0 - 4.0): " << endl;
cin >> student.gpa;
if (student.gpa < 0.0 || student.gpa > 4.0) {
cout << "Invalid GPA! Please enter a value between 0.0 and 4.0." <<
endl;
}
} while (student.gpa < 0.0 || student.gpa > 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].dob.dd << "/" <<
students[pos].dob.mm << "/" << students[pos].dob.yy << endl;
cout << "DATE OF ADMISSION: " << students[pos].doa.dd << "/" <<
students[pos].doa.mm << "/" << students[pos].doa.yy << 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].dob.dd << "/" <<
students[i].dob.mm << "/" << students[i].dob.yy << endl;
cout << "DATE OF ADMISSION: " << students[i].doa.dd << "/" <<
students[i].doa.mm << "/" << students[i].doa.yy << 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].dob.dd << "/" <<
students[pos].dob.mm << "/" << students[pos].dob.yy << endl;
cout << "DATE OF ADMISSION: " << students[pos].doa.dd << "/" <<
students[pos].doa.mm << "/" << students[pos].doa.yy << 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;
}

You might also like