C++ program to implement Student Attendance System

              Let us create a student class and attendance class to implement student attendance system.

Member variables: s_rollno, s_name,s_attendance.

Create a constructor to initialise the values.

MarkIt() is a function to mark the attendance.

displayIt() is a function to display the student details.

Attendance system is a class to calculate the attendance.

‘add_StudentDetails()’ is used to add the details of the student.

‘markIt()’ -it is used to mark the attendance.

‘final_display()’- it displays the attendance.

‘main()’ function is used to create the object and call the functions.

Using switch case, the operations are selected and executed accordingly.

Code:

#include <iostream>

#include <vector>

#include <string>

using namespace std;

// create a class for student

class School_Student {

private:

    int s_rollNo;

    string s_name;

    bool s_attendance;

public:

    School_Student(int rno, string name) {

        s_rollNo = rno;

        s_name = name;

        s_attendance = false;

        // let us make the attendance as absent as default

    }

    void markIt(bool present) {

        s_attendance = present;

    }

    void displayIt() {

        cout << "Student Records"<<endl;;

        cout << "Roll No: " << s_rollNo

             << " | Name: " << s_name

             << " | Attendance: " << (s_attendance ? "Present" : "Absent") << endl;

    }

    int getRollNo() { return s_rollNo; }

};

//Attendance system

class AttendanceSystem {

private:

    vector<School_Student> s1;

public:

    void add_StudentDetails(int s_rollNo, string s_name) {

        s1.push_back(School_Student(s_rollNo, s_name));

    }

    void markIt(int s_rollNo, bool present) {

        for (auto &s : s1) {

            if (s.getRollNo() == s_rollNo) {

                s.markIt(present);

                cout << "Attendance marked for Roll No " << s_rollNo << endl;

                return;

            }

        }

        cout << "Student is not found!" << endl;

    }

    void final_display() {

        cout << "\n--- Attendance Report ---\n";

        for (auto &s : s1) {

            s.displayIt();

        }

    }

};

int main() {

    AttendanceSystem sys1;

    int choice, s_rollNo;

    string s_name;

    do {

        cout << "\n===== Student Attendance Management =====\n";

        cout << "1. Add Student\n";

        cout << "2. Mark Attendance\n";

        cout << "3. Display Attendance Report\n";

        cout << "4. Exit\n";

        cout << "Enter your choice: ";

        cin >> choice;

        switch (choice) {

        case 1:

            cout << "Enter Roll No: ";

            cin >> s_rollNo;

            cout << "Enter Name: ";

            cin.ignore();

            getline(cin, s_name);

            sys1.add_StudentDetails(s_rollNo, s_name);

            break;

        case 2:

            cout << "Enter Roll No: ";

            cin >> s_rollNo;

            cout << "Mark Present (1) or Absent (0): ";

            int attend_status;

            cin >> attend_status;

            sys1.markIt(s_rollNo, attend_status == 1);

            break;

        case 3:

            sys1.final_display();

            break;

        case 4:

            cout << "Exiting program...\n";

            break;

        default:

            cout << "Invalid choice! Try again.\n";

        }

    } while (choice != 4);

    return 0;

}

Output:

===== Student Attendance Management =====

1. Add Student

2. Mark Attendance

3. Display Attendance Report

4. Exit

Enter your choice: 1

Enter Roll No: 1

Enter Name: ajay

===== Student Attendance Management =====

1. Add Student

2. Mark Attendance

3. Display Attendance Report

4. Exit

Enter your choice: 2

Enter Roll No: 1

Mark Present (1) or Absent (0): 1

Attendance marked for Roll No 1

===== Student Attendance Management =====

1. Add Student

2. Mark Attendance

3. Display Attendance Report

4. Exit

Enter your choice: 1

Enter Roll No: 2

Enter Name: jeyan

===== Student Attendance Management =====

1. Add Student

2. Mark Attendance

3. Display Attendance Report

4. Exit

Enter your choice: 2

Enter Roll No: 2

Mark Present (1) or Absent (0): 1

Attendance marked for Roll No 2

===== Student Attendance Management =====

1. Add Student

2. Mark Attendance

3. Display Attendance Report

4. Exit

Enter your choice: 3

--- Attendance Report ---

Student Records

Roll No: 1 | Name: ajay | Attendance: Present

Student Records

Roll No: 2 | Name: jeyan | Attendance: Present

===== Student Attendance Management =====

1. Add Student

2. Mark Attendance

3. Display Attendance Report

4. Exit

Enter your choice: 4

Exiting program...

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

How to write your first XML program?

Java NIO examples to illustrate channels and buffers.