Student Data Management In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Student Data Management In C++

Student Data Management In C++

BLUF: Mastering Student Data Management In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Student Data Management In C++

C++ is renowned for its efficiency. Learn how Student Data Management In C++ enables low-level control and high-performance computing in the tutorial below.

A basic C++ program known as Student Management System is employed by academic institutions to manage student information and fulfill various data needs pertaining to students within a school. Users have the ability to generate, access, and modify data within this project for student management.

Nowadays, databases play a crucial role in various aspects of our daily routines. Data repositories on servers globally store immense amounts of information. An alternative approach to interacting with these databases is through SQL. Yet, have you ever pondered the prospect of employing C++ for database management tasks? Within this guide, we will explore the process of generating diverse perspectives of a text file depending on user roles and adapting them accordingly.

This code stores the following information:

  • Name and registration number
  • Grades for CSE10001
  • Grades for CSE10002
  • Proctor ID
  • Program Breakdown

Example

# include < fstream >
# include < iostream >
# include < stdio.h >
# include < string.h >
using namespace std ;

We are incorporating all essential libraries into this program to prevent any potential compilation errors.

Example

int main ( )
{
	char data [ 15 ] ;
	int n = 0 , option = 0 , count_n = 0 ;
	// This is the initial mark alloted to a subject.
	string empty = "00" ;
	string proctor = "" ;
	ifstream f ( "Example.txt" ) ;
	string line ;

We are assuming a maximum data input length of 15 characters. The file "Example.txt" is being accessed using ifstream, with the database sorted.

Example

for (int i = 0 ; std :: getline ( f , line ) ; + + i ) {
		count_n + + ;
	}

The aforementioned for loop is utilized to tally the total number of lines present in the file.

Example

while ( option ! = 6 ) {
		// This prints out all the available options in the
		// DB
		cout << " \ n Available operations: \ n1. Add New "
				"Students \ n2 . "
			<< "Student Login \ n3 . Faculty Login \ n4 . "
				"Proctor Login \ n5 . Admin View \ n "
			<< "6. Exit\nEnter option: " ;
		cin >> option ;

The while loop mentioned above will run only if the chosen selection is not 6. It will display the list of operations step by step, which includes adding a new student, student login, faculty login, proctor login, admin view, exiting for the 6th option, and prompting for input.

Example

if ( option == 1 ) {
			cout << "Enter the number of students: " ; 
			cin >> n ; 
			count_n = count_n + n ; 
			for ( int i = 0 ; i < n ; i + + ) {
				ofstream outfile ;
				outfile.open ( "Example.txt" , ios :: app ) ;
				// The entire data of a single student is
				// stored line-by-line.
				cout << "Enter your registration number: " ;
				cin >> data ;
				outfile << data << " \ t " ;
				cout << "Enter your name: " ;
				cin >> data ;
				int len = strlen ( data ) ; 
				while ( len < 15 ) {
					data [ len ] = ' ' ;
					len = len + 1 ;
				}

The provided code snippet will be executed only if the user selects the number 1 as the option. Upon selection, the program will prompt the user to input the total number of students. Subsequently, the program will collect and store each student's information sequentially, followed by requesting the registration number and name input.

Example

else if ( option = = 2 ) {
			char regno [ 9 ] ;
			cout << "Enter your registration number: " ;
			cin >> regno ;
			ifstream infile ;
			int check = 0 ;
			infile.open ( "Example.txt" , ios :: in ) ;

			// This loop prints out the data according to
			// the registration number specified.
			while ( infile > > data ) {
				if (strcmp ( data , regno ) = = 0 ) {
					cout
						<< " \ n Registration Number: " << data
						<< endl ;
					infile >> data ;
					cout << "Name: " << data << endl ;

					infile >> data ;
					cout << "CSE10001 mark: " << data
						<< endl ;

					infile >> data ;
					cout << "CSE10002 mark: " << data
						<< endl ;

					infile >> data ;
					cout << "Proctor ID: " << data << endl ;

					infile.close ( ) ;
					check = 1 ;
				}
			}

In the code snippet shown above, the else if block will be triggered when the user selects the number 2 as the second choice. The registration number consists of 9 characters, prompting the user to input their registration number, which will then be displayed. Subsequently, the program will prompt the user to enter the marks for each subject.

Example

if ( check = = 0 ) {
				cout << "No such registration number found!"
					<< endl ;
			}
		}

If a user selects 0 as an option, it will show a message stating "No registration number was found."

Example

else if ( option == 3 ) {
			char subcode [ 7 ] ;
			cout << "Enter your subject code: ";
			cin >> subcode ;
			string code1 = "CSE10001" ,  code2 =  "CSE10002" ,
				mark = "" ;
			ifstream infile ;
			int check = 0 ;

			cout << " \ n Available operations: \ n1 . Add data "
					"about marks \ n "
				<< "2. View data\nEnter option: " ;
			cin >> option ;

The preceding if statement will run exclusively when the user opts for the third choice. Within this block, the user will input the subject code, select from available operations, and view data. Essentially, this section is designed for viewing or inputting grades into the student database.

Example

if ( option = = 1 ) {
				cout
					<< "Warning! You would need to add mark"
					<< "details for all the students!"
					<< endl ;
				for ( int i = 0 ; i < count_n ; i + + ) {
					fstream file( "Example.txt" ) ;

					// The seek in file has been done
					// according to the length
					// of the data being inserted. It needs
					// to adjusted accordingly for different
					// lengths of data.
					if ( strcmp ( subcode , code1.c_str ( ) )
						== 0 ) {
						file.seekp ( 26 + 37 * i ,
								std : : ios_base : : beg ) ;
						cout << " Enter the mark of student# "
							<< ( i + 1 ) << " : " ;
						cin >> mark ;
						file.write ( mark.c_str ( ) , 2 ) ;
					}

If the user chooses option 1 in the provided code snippet, a warning message will be displayed prompting them to include marks. The file seek operation is based on the length of the data being inserted, necessitating adjustments for varying data lengths.

Example

else if ( option == 2 ) {
				infile.open ( " Example.txt " , ios : : in ) ;
				if ( strcmp ( subcode , code1.c_str ( ) ) == 0 ) {
					cout << " Registration number - Marks \ n "
						<< endl ;
					while ( infile >> data ) {
						cout << data ;
						infile >> data ;
						infile >> data;
						cout << " - " << data << endl ;
						infile >> data ;
						infile >> data ;
						check = 1 ;
					}
				}
				infile.close ( ) ;
				infile.open ( " Example.txt " , ios : : in ) ;
				if ( strcmp ( subcode , code2.c_str ( ) ) = = 0 ) {
					cout << " Registration number - Marks \ n"
						<< endl ;
					while ( infile >> data ) {
						cout << data ;
						infile >> data ;
						infile >> data ;
						infile >> data ;
						cout << " - " << data << endl ;
						infile >> data ;
						check = 1 ;
					}
				}
			} 	infile.close ( ) ;

			if ( check == 0 ) {
				cout << " No such subject code found! "
					<< endl ;
			}
		}

The else block in the provided code snippet will be triggered solely if the user opts for choice 2. It is designed for the purpose of displaying a student's grades. The additional infile directives have been implemented to extract individual grades based on the tab-delimited data format.

Example

else if ( option = = 4 ) {
			char procid [ 7 ] ;
			cout << " Enter your proctor ID : " ;
			cin >> procid ;
			int check = 0 ;
			char temp1 [ 100 ] , temp2 [ 100 ] , temp3 [ 100 ] ;
			char temp4 [ 100 ] , id [ 100 ] ;
			ifstream infile ;
			infile.open ( " Example.txt " , ios : : in ) ;

			while ( infile >> temp1 ) {
				infile >> temp2 ;
				infile >> temp3 ;
				infile >> temp4 ;
				infile >> id ;

				if ( strcmp ( id , procid ) = = 0 ) {
					cout << " \ n Registration Number: "
						<< temp1 << endl ;
					cout << " Name : " << temp2 << endl ;
					cout << " CSE1001 Mark : " << temp3
						<< endl ;
					cout << " CSE1002 Mark : " << temp4
						<< endl ;
					check = 1 ;
				}
			}

			if ( check == 0 ) {
				cout << " No such proctor ID found! " << endl ;
			}
		}

In the provided code snippet, the loop exhibits the information of all students associated with a common proctor ID.

Example

// This loop acts as an admin view to see all the
		// data in the file. 
		else if ( option = = 5 ) {
			char password [ 25 ] ;
			cout << " Enter the admin password : " ;
			cin >> password ;

			// This variable value can be changed according
			// to your requirement of the administrator
			// password.

			string admin_pass = " admin " ;

			if ( strcmp ( password , admin_pass.c_str ( ) ) = = 0 ) {
				cout << " Reg No.	 "
						" \ t Name \ t CSE10001 \ t CSE10002 \ t Proctor "
						" ID "
					<< endl ;
				ifstream infile ;
				infile.open ( " Example.txt " , ios : : in ) ;
				char data [ 20 ] ; 
				while ( infile >> data ) {
					cout << data << " \ t " ;
					infile >> data ;
					cout << data << " \ t " ;
					infile >> data ;
					cout << data << " \ t " ;
					infile >> data ;
					cout << data << " \ t " ;
					infile >> data ;
					cout << data << endl ;
				}
			}
		}
	}
}

This iteration serves as an administrative interface to display all the information stored in the file. The value of this variable can be modified as needed for the administrator password.

Student Database Management Program in C++

Example

// Include all the necessary libraries.
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
    // Considering the max length of data entered (name) to
    // be 15.
    char data[15];
    int n = 0, option = 0, count_n = 0;
    // This is the initial mark alloted to a subject.
    string empty = "00";
    string proctor = "";
    // Name of the file in which DB is stored.
    ifstream f("Example.txt");
    string line;

    // The following for loop counts the total number of
    // lines in the file.
    for (int i = 0; std::getline(f, line); ++i)
    {
        count_n++;
    }

    while (option != 6)
    {
        // This prints out all the available options in the
        // DB
        cout << "\nAvailable operations: \n1. Add New "
                "Students\n2."
             << "Student Login\n3. Faculty Login\n4. "
                "Proctor Login\n5. Admin View\n"
             << "6. Exit\nEnter option: ";
        cin >> option;

        if (option == 1)
        {
            cout << "Enter the number of students: ";
            cin >> n;

            count_n = count_n + n;

            for (int i = 0; i < n; i++)
            {
                ofstream outfile;
                outfile.open("Example.txt", ios::app);
                // The entire data of a single student is
                // stored line-by-line.
                cout << "Enter your registration number: ";
                cin >> data;
                outfile << data << "\t";

                cout << "Enter your name: ";
                cin >> data;
                int len = strlen(data);

                while (len < 15)
                {
                    data[len] = ' ';
                    len = len + 1;
                }
                outfile << data << "\t";
                // Inserting empty data initially into the
                // file
                outfile << empty << "\t";
                outfile << empty << "\t";

                cout << "Enter your proctor ID: ";
                cin >> proctor;

                outfile << proctor << endl;
            }
        }

        else if (option == 2)
        {
            char regno[9];
            cout << "Enter your registration number: ";
            cin >> regno;
            ifstream infile;
            int check = 0;
            infile.open("Example.txt", ios::in);

            // This loop prints out the data according to
            // the registration number specified.
            while (infile >> data)
            {
                if (strcmp(data, regno) == 0)
                {
                    cout
                        << "\nRegistration Number: " << data
                        << endl;
                    infile >> data;
                    cout << "Name: " << data << endl;

                    infile >> data;
                    cout << "CSE1001 mark: " << data
                         << endl;

                    infile >> data;
                    cout << "CSE1002 mark: " << data
                         << endl;

                    infile >> data;
                    cout << "Proctor ID: " << data << endl;

                    infile.close();
                    check = 1;
                }
            }

            if (check == 0)
            {
                cout << "No such registration number found!"
                     << endl;
            }
        }

        // This loop is used to view and add marks to the
        // database of a student.
        else if (option == 3)
        {
            char subcode[7];
            cout << "Enter your subject code: ";
            cin >> subcode;
            string code1 = "CSE1001", code2 = "CSE1002",
                   mark = "";
            ifstream infile;
            int check = 0;

            cout << "\nAvailable operations: \n1. Add data "
                    "about marks\n"
                 << "2. View data\nEnter option: ";
            cin >> option;

            if (option == 1)
            {
                cout
                    << "Warning! You would need to add mark"
                    << "details for all the students!"
                    << endl;
                for (int i = 0; i < count_n; i++)
                {
                    fstream file("Example.txt");

                    // The seek in file has been done
                    // according to the length
                    // of the data being inserted. It needs
                    // to adjusted accordingly for different
                    // lengths of data.

                    if (strcmp(subcode, code1.c_str()) == 0)
                    {
                        file.seekp(26 + 37 * i,
                                   std::ios_base::beg);
                        cout << "Enter the mark of student#"
                             << (i + 1) << " : ";
                        cin >> mark;
                        file.write(mark.c_str(), 2);
                    }

                    if (strcmp(subcode, code2.c_str()) == 0)
                    {
                        file.seekp(29 + 37 * i,
                                   std::ios_base::beg);
                        cout << "Enter the mark of student#"
                             << (i + 1) << " : ";
                        cin >> mark;
                        file.write(mark.c_str(), 2);
                    }
                }
            }

            // This loop is used to view marks of a student.
            // The extra infile commands have been used to
            // get a specific mark only since the data has
            // been separated by a tabspace.

            else if (option == 2)
            {
                infile.open("Example.txt", ios::in);
                if (strcmp(subcode, code1.c_str()) == 0)
                {
                    cout << "Registration number - Marks\n"
                         << endl;
                    while (infile >> data)
                    {
                        cout << data;
                        infile >> data;
                        infile >> data;
                        cout << " - " << data << endl;
                        infile >> data;
                        infile >> data;
                        check = 1;
                    }
                }

                infile.close();
                infile.open("Example.txt", ios::in);

                if (strcmp(subcode, code2.c_str()) == 0)
                {
                    cout << "Registration number - Marks\n"
                         << endl;
                    while (infile >> data)
                    {
                        cout << data;
                        infile >> data;
                        infile >> data;
                        infile >> data;
                        cout << " - " << data << endl;
                        infile >> data;
                        check = 1;
                    }
                }
            }

            infile.close();

            if (check == 0)
            {
                cout << "No such subject code found!"
                     << endl;
            }
        }

        // This loop displays all the details of students
        // under the same proctor ID.

        else if (option == 4)
        {
            char procid[7];
            cout << "Enter your proctor ID: ";
            cin >> procid;
            int check = 0;
            char temp1[100], temp2[100], temp3[100];
            char temp4[100], id[100];
            ifstream infile;
            infile.open("Example.txt", ios::in);

            while (infile >> temp1)
            {
                infile >> temp2;
                infile >> temp3;
                infile >> temp4;
                infile >> id;

                if (strcmp(id, procid) == 0)
                {
                    cout << "\nRegistration Number: "
                         << temp1 << endl;
                    cout << "Name: " << temp2 << endl;
                    cout << "CSE1001 Mark: " << temp3
                         << endl;
                    cout << "CSE1002 Mark: " << temp4
                         << endl;
                    check = 1;
                }
            }

            if (check == 0)
            {
                cout << "No such proctor ID found!" << endl;
            }
        }

        // This loop acts as an admin view to see all the
        // data in the file.

        else if (option == 5)
        {
            char password[25];
            cout << "Enter the admin password: ";
            cin >> password;

            // This variable value can be changed according
            // to your requirement of the administrator
            // password.

            string admin_pass = "admin";

            if (strcmp(password, admin_pass.c_str()) == 0)
            {
                cout << "Reg No.	 "
                        "\tName\tCSE1001\tCSE1002\tProctor "
                        "ID"
                     << endl;
                ifstream infile;
                infile.open("Example.txt", ios::in);
                char data[20];

                while (infile >> data)
                {
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << "\t";
                    infile >> data;
                    cout << data << endl;
                }
            }
        }
    }
}

Output:

Output

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 1
Enter the number of students: 2
Enter your registration number: 12BCE2083
Enter your name: Prateek
Enter your proctor ID: 10001
Enter your registration number: 15BCE2022
Enter your name: shubham
Enter your proctor ID: 10002

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 3
Enter your subject code: CSE10001

Available operations: 
1. Add data about marks
2. View data
Enter option: 1
Warning! You would need to add mark details for all the students!
Enter the mark of student#1 : 54
Enter the mark of student#2 : 90
No such subject code found!

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 5
Enter the admin password: admin
Reg No.       Name    CSE1001    CSE1002    Proctor ID
15BCE2083    Prateek    54         00         10001
15BCE2082    Shubham    89         00         10002

Available operations: 
1. Add New Students
2. Student Login
3. Faculty Login
4. Proctor Login
5. Admin View
6. Exit
Enter option: 6 
- - - - - - - - - - - - - - - - - - - - - - - 
(Program exited with code: 0)
Press return to continue

NOTE: Please note that the pointer position has been adjusted to reflect the length of the data we are adding to the text file. We have assumed a registration number that is always 9 characters long, a subject code that is either CSE10001 or CSE10002, a proctor ID that is 4 characters long, and tags that are only 2 characters long. If you wanted to enter a different type of data, you would have to modify the code accordingly.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience