#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
using namespace std;
// Structure to represent student details
struct Student {
int studentID;
string name;
string contactInfo;
};
// Structure to represent course enrollment details
struct Enrollment {
int courseID;
string enrollmentDate;
};
// Function prototypes
void displayMenu();
void addStudent(Student students[], int& studentCount);
void displayAllStudents(Student students[], int studentCount);
void enrollInCourse(Student students[], int studentCount, Enrollment enrollments[], int& enrollmentCount);
void displayAllEnrollments(Student students[], int studentCount, Enrollment enrollments[], int enrollmentCount);
void calculateFine(Enrollment enrollments[], int enrollmentCount);
void displayAllFines(Student students[], int studentCount, Enrollment enrollments[], int enrollmentCount);
void saveData(Student students[], int studentCount, Enrollment enrollments[], int enrollmentCount);
void loadData(Student students[], int& studentCount, Enrollment enrollments[], int& enrollmentCount);
int main() {
const int MAX_STUDENTS = 100;
const int MAX_ENROLLMENTS = 500;
Student students[MAX_STUDENTS];
Enrollment enrollments[MAX_ENROLLMENTS];
int studentCount = 0;
int enrollmentCount = 0;
loadData(students, studentCount, enrollments, enrollmentCount);
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent(students, studentCount);
break;
case 2:
displayAllStudents(students, studentCount);
break;
case 3:
enrollInCourse(students, studentCount, enrollments, enrollmentCount);
break;
case 4:
displayAllEnrollments(students, studentCount, enrollments, enrollmentCount);
break;
case 5:
calculateFine(enrollments, enrollmentCount);
break;
case 6:
displayAllFines(students, studentCount, enrollments, enrollmentCount);
break;
case 7:
saveData(students, studentCount, enrollments, enrollmentCount);
cout << "Data saved successfully.\n";
break;
case 8:
loadData(students, studentCount, enrollments, enrollmentCount);
cout << "Data loaded successfully.\n";
break;
case 9:
cout << "Exiting the program. Thank you!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 9);
return 0;
}
void displayMenu() {
cout << "\n===== Student Registration System Menu =====\n";
cout << "1. Add new student\n";
cout << "2. Display all students\n";
cout << "3. Enroll in a course\n";
cout << "4. Display all enrollments\n";
cout << "5. Calculate fine for late registration\n";
cout << "6. Display all fines\n";
cout << "7. Save data to file\n";
cout << "8. Load data from file\n";
cout << "9. Exit\n";
}
void addStudent(Student students[], int& studentCount) {
cout << "Enter student ID: ";
cin >> students[studentCount].studentID;
cout << "Enter student name: ";
cin.ignore();
getline(cin, students[studentCount].name);
cout << "Enter contact information: ";
getline(cin, students[studentCount].contactInfo);
cout << "Student added successfully.\n";
// Increment student count
studentCount++;
}
void displayAllStudents(Student students[], int studentCount) {
if (studentCount == 0) {
cout << "No students registered yet.\n";
} else {
cout << "\n===== List of Registered Students =====\n";
cout << setw(10) << "Student ID" << setw(20) << "Name" << setw(30) << "Contact Information\n";
for (int i = 0; i < studentCount; ++i) {
cout << setw(10) << students[i].studentID << setw(20) << students[i].name << setw(30) << students[i].contactInfo << endl;
}
}
}
void enrollInCourse(Student students[], int studentCount, Enrollment enrollments[], int& enrollmentCount) {
int studentID;
int courseID;
cout << "Enter student ID: ";
cin >> studentID;
// Validate student ID
int studentIndex = -1;
for (int i = 0; i < studentCount; ++i) {
if (students[i].studentID == studentID) {
studentIndex = i;
break;
}
}
if (studentIndex == -1) {
cout << "Student not found. Please register the student first.\n";
return;
}
cout << "Enter course ID: ";
cin >> courseID;
// Validate course ID
// Assuming a course ID validation mechanism is implemented
// Enroll the student in the course
enrollments[enrollmentCount].courseID = courseID;
// Get current date and time
time_t currentTime = time(0);
enrollments[enrollmentCount].enrollmentDate = ctime(¤tTime);
cout << "Enrollment successful.\n";
// Increment enrollment count
enrollmentCount++;
}
void displayAllEnrollments(Student students[], int studentCount, Enrollment enrollments[], int enrollmentCount) {
if (enrollmentCount == 0) {
cout << "No enrollments yet.\n";
} else {
cout << "\n===== List of Enrollments =====\n";
cout << setw(10) << "Student ID" << setw(15) << "Course ID" << setw(30) << "Enrollment Date\n";
for (int i = 0; i < enrollmentCount; ++i) {
cout << setw(10) << students[i].studentID << setw(15) << enrollments[i].courseID << setw(30) << enrollments[i].enrollmentDate;
}
}
}
void calculateFine(Enrollment enrollments[], int enrollmentCount) {
const double FINE_RATE_PER_DAY = 0.5; // Example fine rate per day
for (int i = 0; i < enrollmentCount; ++i) {
// Assuming today's date for simplicity
time_t currentTime = time(0);
string currentDate = ctime(¤tTime);
// Convert enrollment date to time_t
struct tm enrollmentTime = {};
strptime(enrollments[i].enrollmentDate.c_str(), "%a %b %d %H:%M:%S %Y", &enrollmentTime);
time_t enrollmentDate = mktime(&enrollmentTime);
// Calculate days difference
double daysDifference = difftime(currentTime, enrollmentDate) / (60 * 60 * 24);
// Calculate fine
double fine = max(0.0, daysDifference) * FINE_RATE_PER_DAY;
cout << "Fine for enrollment " << i + 1 << ": $" << fixed << setprecision(2) << fine << endl;
}
}
void displayAllFines(Student students[], int studentCount, Enrollment enrollments[], int enrollmentCount) {
cout << "\n===== List of Fines =====\n";
calculateFine(enrollments, enrollmentCount);
}
void saveData(Student students[], int studentCount, Enrollment enrollments[], int enrollmentCount) {
ofstream outFile("student_data.txt");
// Save student data
outFile << studentCount << endl;
for (int i = 0; i < studentCount; ++i) {
outFile << students[i].studentID << " " << students[i].name << " " << students[i].contactInfo << endl;
}
// Save enrollment data
outFile << enrollmentCount << endl;
for (int i = 0; i < enrollmentCount; ++i) {
outFile << enrollments[i].courseID << " " << enrollments[i].enrollmentDate;
}
outFile.close();
}
void loadData(Student students[], int& studentCount, Enrollment enrollments[], int& enrollmentCount) {
ifstream inFile("student_data.txt");
if (!inFile) {
cout << "No previous data found.\n";
return;
}
// Load student data
inFile >> studentCount;
for (int i = 0; i < studentCount; ++i) {
inFile >> students[i].studentID;
inFile.ignore();
getline(inFile, students[i].name);
getline(inFile, students[i].contactInfo);
}
// Load enrollment data
inFile >> enrollmentCount;
for (int i = 0; i < enrollmentCount; ++i) {
inFile >> enrollments[i].courseID;
inFile.ignore();
getline(inFile, enrollments[i].enrollmentDate);
}
inFile.close();
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter