Player Management System Using C++

Player Management System using C++ (With Source Code)

 Player Management System using C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a Player Management System using C++ (With Source Code).

A Player Management System is implemented in C++ is a simple console based application which allows users to add, modify, delete, and display player records. It may also provide functionality to search for players based on their attributes, such as ID or name.

Player Management System Using
C++

This system assists the authorities in managing incoming players. In terms of the project, the user may control their player profiles. He has the ability to look for and update them. This effort also helps to reduce the danger of data loss. When you save a player record, it is automatically saved in text format in the database.

Creating a Pizza Hut Management system using C++ (With Source Code)

Objective of the System:

The player management system’s goal is to provide an organized and efficient method of managing player records inside a sports organization. Its goal is to centralize and organize player information such that it is easily accessible and retrievable. The system helps administrators to maintain track of players’ personal information, game data, and contact information by allowing them to input, modify, delete, and display player details. The technology may also make it easier to find certain players based on features such as ID or name. The ultimate objective is to improve communication and maintain smooth operations within the sports organization by streamlining the maintenance of player records.

Overview of the UI:

=============== PLAYER MANAGEMENT MENU ===================
        1: Add a new Player Record
        2: Show all Players Record
        3: Delete Player by its Player Id from Record
        4: Delete Player by its Player Name from Record
        5: Search Player using Player Name
        6: Search Player using Player Id
        7: Search Player using Dorm No.
        8: Search Player using Bed No.
        9: Search Players using Parents Name
        10: Search Players in a specific Game
        11: Count the No. of Player in all Games
        12: Count the Total no. of Player’s Record
        13: Clear the complete database of Player’s Record
        14: Exit from Program”;
        Enter your choice…!

The user will choose an option to perform the operation of their choice. User will then enter a values according to the prompts that will be displayed on the console to perform that operation.

Player Management System Source code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include<iostream>
#include<fstream>  #For File Handling  fstream, ifstream, ofstream
#include<string>   #For String Manipulation
#include<stdlib.h> #
#include<conio.h>  #clrscr() function which is used to clear the console
//#include<bits/stdc++.h> //this header includes all the library in the Program
using namespace std;

class file
{
    private:
        int playerid;
        string playerName;
        int age;
        char add[30];
        long long phno;   // we are not taking int because Mob No. is 10 digit which is outside the range of int
        string parentName;
        char game[30];
      //  char coachn[30];
        string coachName;
        int dorm;
        int bed;



    public:

        /*
            * This function takes input from user about Player Details
            * The input taken by this function is stored in the File using writeFile() function
        */
        void input(){

            cout<<"\nEnter player id no: ";
            cin>>playerid;
            cout<<"\nEnter player age: ";
            cin>>age;

            //Taking input of Player Name as First Name and Second Name
            string strFname="", strLname="";
            cout<<"\nEnter the First Name of Player: ";
            cin>>strFname;
            cout<<"\nEnter the Last Name of Player: ";
            cin>>strLname;
            playerName = strFname +" "+ strLname;//Stores the Player as Fname_Lname ex. Aastha_Anand


            cout<<"\nEnter player address(City): ";
            cin>>add;
            cout<<"\nEnter player phone no.: ";
            cin>>phno;

            //Taking input of Player's Parent Name
            cout<<"\nEnter the First Name of Player's Parent: ";
            cin>>strFname;
            cout<<"\nEnter the Last Name of Player's Parent: ";
            cin>>strLname;
            parentName = strFname +" "+ strLname;//Stores the Player as Fname_Lname ex. Aastha_Anand



            cout<<"\nEnter game name 'Basketball', 'TableTennis', 'Cricket', 'Volleyball', 'Hockey', 'Taekwondo', 'Badminton'\nGame Name: ";
            cin>>game;

            //Taking input of coach name as Fname and Lname
            cout<<"\nEnter the First Name of Coach: ";
            cin>>strFname;
            cout<<"\nEnter the Last Name of Coach: ";
            cin>>strLname;
            coachName = strFname +" "+ strLname;//Stores the Player as Fname_Lname ex. Aastha_Anand


            //cout<<"\nEnter coach name alloted: ";
           // cin>>coachn;
            cout<<"\nEnter dorm no.: ";
            cin>>dorm;
            cout<<"\nEnter bed no.: ";
            cin>>bed;

        }//end of input()function

        void output(){
            cout<<"\nPlayer Details: \n";
            cout<<"playerid==>"<<playerid<<endl;
            cout<<"player name==>"<<playerName<<endl;
            cout<<"player add==>"<<add<<endl;
            cout<<"phone no==>"<<phno<<endl;
            cout<<"parent name==>"<<parentName<<endl;
            cout<<"game prefered==>"<<game<<endl;
            cout<<"coach alloted==>"<<coachName<<endl;
            cout<<"dorm no==>"<<dorm<<endl;
            cout<<"bed no==>"<<bed<<endl;

        }

       // char* retplayername(){
       //     return playername;
       // }

        string retplayername(){
            return playerName;
        }


        int retplayerid(){
            return playerid;
        }

        int retage(){
            return age;
        }

        char* radd(){
            return add;
        }

        long long  rphno(){ //int cannot store the mobile since it is out of Range
            return phno;
        }

       // char* rparentn(){
       //     return parentn;
       // }

       string rparentn(){
            return parentName;
       }


        char* rgame(){
            return game;
        }


       // char* rcoachn(){
        //    return coachn;
        //}

        string rcoachn(){
            return coachName;
        }


        int rdorm(){
            return dorm;
        }

        int rbed(){
            return bed;
        }

};// End of Class file


/*
    * This function is used to write the details in the file
    * It takes details of class instance and append that into the file
    *
*/
void writeInFile(file fobj){

    char arr[]= "     ";
    ofstream fout;
    fout.open("PlayersRecord.txt", ios::out | ios::app);

    fout<<"id";
    fout<<fobj.retplayerid();fout<<arr;
    fout<<fobj.retplayername();fout<<arr;
    fout<<fobj.retage();fout<<arr;
    fout<<fobj.radd();fout<<arr;
    fout<<fobj.rphno();fout<<arr;

    fout<<"pn";
    fout<<fobj.rparentn();fout<<arr;
    fout<<fobj.rgame();fout<<arr;

    fout<<"cn";
    fout<<fobj.rcoachn();fout<<arr;

    fout<<'D';   //D will starting char of Dorm no. in file for unique identification
                    //All Dorm No. will start with D
    fout<<fobj.rdorm();fout<<arr;

    fout<<'B';  //B will be the Starting char of every BedNo.  to uniquely identify this
    fout<<fobj.rbed();fout<<arr;
    fout<<'\n';


    fout.close();

}


/*
    * This functions shows all the Players record which we have stored in file
*/
void showAllRecords(){

    ifstream readFile;
    readFile.open("PlayersRecord.txt");
    char charsInLine[1024];
    int count=0;
    cout<<"Players records :\n\n";
    while(!readFile.eof()){
        readFile.getline(charsInLine,1024);
        cout<<charsInLine<<endl;
    }
    readFile.close();

}



/*
    * deleteInRecords is function which deletes the Record of Player from Playersrecord.txt file
    * it can delete the Record of Player using Player Id and Player Name both
    * if the choice is 1: it asks for Player's Id
    * else the choice is not 1 here it is 2 as passed by main as 2(deleInRecords(2)): it asks for Player's Name
    * It searches the Player's id and if found it deletes that record

*/
void deleteInRecords(int choice){

    string deleteString;
    string strFname="", strLname="";
    if(choice == 1){   //1: Deletion using PlayerId
        cout<<"Enter the Player's Id whose record needs to be deleted\n";
        cin>>deleteString;
        deleteString = "id"+deleteString;
        cout<<"deleteString: "<<deleteString<<endl;
    }
    else{               //2: Deletion using Player Name
        cout<<"Enter the First Name of Player: ";
        cin>>strFname;
        cout<<"\nEnter the Last Name of Player: ";
        cin>>strLname;
        deleteString = strFname+"_"+strLname;  //ex. Aastha_Anand  here Fname= Aastha Lname= Anand
        cout<<"deleteString: "<<deleteString<<endl;
    }


    ifstream readFile;
    ofstream writeFile("sample.txt");

    char charsInLine[1024];

    readFile.open("PlayersRecord.txt");
   // writeFile.open("sample2.txt", ios::out | ios::app);
    int count=0;
    bool found = false; //checks wheather Player Record is there or not
    //running the loop till end of file
    while(!readFile.eof()){

        readFile.getline(charsInLine,1024);
        string strLine(charsInLine);  //converting to STL string
        cout<<count<<": "<<charsInLine<<endl;

        size_t found = strLine.find(deleteString);

        if(found != string::npos){

            found = true; //stores the found value as true since there is a record with that Id or Name
            break;
        }

        //writes the Line in the write File which doesn't the string
        else{
            writeFile<<charsInLine;//writes the read line of 1024 char which doesn't have that string into the write file
            writeFile<<"\n";
          //  cout<<"Written: "<<charsInLine<<endl;
        }
        count++;

    }//end of While loop

    readFile.close();  //closing the PlayersRecord,txt file
    writeFile.close(); //closing sample.txt file

    remove("PlayersRecord.txt");     //deleting Playersrecord.txt file since unmatched(contents which doesn't need to be deleted) content is copied to sample.txt file
    rename("sample.txt", "PlayersRecord.txt");  //Rename sample.txt as PlayersRecord.txt since Sample.txt contains the contents which were not needed to be deleted

   //if no records found with entered Id or Name
    if(found == false){
       cout<<"No records available...!\n";
    }
}



/*
    * It counts the Total number Players available in our Record File ("PlayersRecord.txt")
    * It uses the string "id" to uniquely identify a record(line) since there is id for every Player
    * So, No. of Player available in Record is equal to no.of times "id" is found in Record File("PlayersRecord.txt")
*/
void countPlayers(){
    ifstream readFile;
    char charsInLine[1024];
    int countPlayer=0;

    readFile.open("PlayersRecord.txt");  //open the File
    while(!readFile.eof()){

        readFile.getline(charsInLine,1024);//scans a single line of File and stores in the charsInLine array
        string strLine(charsInLine); //converting char array(charsInLine) --> string("strLine")

        //finds the string here "id" in strLine which contains a single line of file
        size_t found = strLine.find("id"); //we are looking for id since each row contains id
                                            //so number of rows will be equal to no.of time id is found..!!

        /*
            * If it finds that id in the line(strLine) it increments the countGame
        */
        if(found!= string::npos){
            countPlayer++;
            cout<<charsInLine<<endl;  //printing the Record for the game
        }
        else{
            break;  //if id is not there in Line records are empty
        }
    }//end of while(searching the record)

    cout<<"Number of Players available in our Records: "<<countPlayer<<endl;
    readFile.close(); //closes the file

}//end of countPlayers function




/*
    * Counts Total number of Players in all Games here "Basketball","TableTennis","Cricket","Volleyball","Hockey", "Taekwondo", "Badminton";
*/
void countPlayersInGame(){

    ifstream readFile;
    readFile.open("PlayersRecord.txt");
    char charsInLine[1024];
    int count=0;  //string strLine; // 1024 since a line contains 1024 chars

   //Contains the list of games available for enrollment
    string games[7]={"Basketball","TableTennis","Cricket","Volleyball","Hockey", "Taekwondo", "Badminton"};
    string gameName="";


    for(int i=0; i<7 ; i++){    //remove this loop

        gameName = games[i]; //selects the specific game from array
       // cout<<"GameName: "<<gameName<<endl;
        int countGame = 0;

        ifstream readFile;
        readFile.open("PlayersRecord.txt");

        while(!readFile.eof()){

            readFile.getline(charsInLine, 1024);
            string strLine(charsInLine);  //Casting a char array to string


            size_t found = strLine.find(gameName); //stores the info whether a particular string(Game) form in the line of not
            if(found!= string::npos){
                countGame++;
                //cout<<charsInLine<<endl;  //printing the Record for the game

            }//end of if

        }//end of while

        readFile.close();

        cout<<"No. of Players enrolled in "<<gameName<<": "<<countGame<<endl;
        countGame = 0; //initializing again as 0 to count for next game
   }//end of For Loop


}//end of Function



/*
    *This function finds the Player record by using the PlayerId, PlayerName,Parents Name, Game, Dorm No. Bed No.
    *It takes the Player Name as from user & Returns the Details of the player
    *It searches the Entered Player name in File "PlayersRecord.txt" and returns the record if it is there
    *If the Player is not found in the file it Returns "Player not found...!!"
*/

void searchInRecords(int choice){
   // char game[12];  //string game
    string searchString="";
    string strFname="", strLname="";

    switch(choice){

        case 1: //string strFname="", strLname="";
                cout<<"Enter the First Name of Player: ";
                cin>>strFname;
                cout<<"Enter the Last Name of Player: ";
                cin>>strLname;
                searchString = strFname+" "+strLname;  //ex. Aastha_Anand  here Fname= Aastha Lname= Anand
                cout<<"Search String: "<<searchString<<endl;
                break;

        case 2: cout<<"Enter the Player Id: ";
                cin>>searchString;
                searchString = "id"+searchString;
                cout<<"Search String: "<<searchString<<endl;
                break;

        case 3: cout<<"Enter the Dorm No.: ";
                cin>>searchString;
                searchString = "D"+searchString;
                cout<<"Search String: "<<searchString<<endl;
                break;

        case 4: cout<<"Enter the Bed No.: ";
                cin>>searchString;
                searchString = "B"+searchString;
                cout<<"Search String: "<<searchString<<endl;
                break;

        case 5:// cout<<"\nEnter the ParentsName. : ";
                cout<<"\nEnter the First Name of Player's Parent: ";
                cin>>strFname;
                cout<<"\nEnter the Last Name of Player's Parent: ";
                cin>>strLname;
                searchString = "pn"+strFname+"_"+strLname;  //ex. Aastha_Anand  here Fname= Aastha Lname= Anand
                cout<<"Search String: "<<searchString<<endl;
                break;

        case 6: cout<<"Enter Player's Games Basketball, TableTennis, Cricket, Volleyball, Hockey, Taekwondo , Badminton : ";
                cin>>searchString;
                break;

        case 7: cout<<"Wrong Input..! Enter your choice again...\n";
                break;

    }//end of switch


    ifstream readFile;
    readFile.open("PlayersRecord.txt");
    char charsInLine[1024]; int count=0;  //string strLine; // 1024 since a line contains 1024 chars
    searchString = searchString+" ";

    while(!readFile.eof()){
        //cout<<"Im inside while\n";
        readFile.getline(charsInLine, 1024);
     //   cout<<"Records row: "<<charsInLine<<endl;

        string strLine(charsInLine);  //Casting a char array to string

        size_t found = strLine.find(searchString); //stores the info whether a particular string(Game) form in the line of not

        if(found!= string::npos){
            count++;
            cout<<"Records "<<count<<" : "<<charsInLine<<endl;  //printing the Record for the game
        }
    }

    readFile.close();
    if( count == 0 )
        cout<<"NO records found...!\n";
    else{
        cout<<"No. of Records found for "<<searchString<<": "<<count<<endl;
    }
}



/*
    * This function is used to completely delete the Player Database
    * It asks for re-confirmation from user
    * If the user press "Y" it completely clears the Player's record
*/

void clearCompleteDatabase(){
    int x;
    cout<<"Do you want to delete the complete database of Players...?\n Press 1 to confirm: ";
    cin>>x;

    if(x==1){
        remove("PlayersRecord.txt"); //Delete the file(Database) PlayersRecord.txt
        ofstream writeFile("PlayersRecord.txt");  //Creates a new empty file named as "PlayersRecord.txt"
        writeFile.close(); //closes the file "PlayersRecord.txt"
    }
}





int main(){

    file fileobj;   //Instance(object) of file class

    while(1){
        int choice;
        cout<<"\n\n=============== PLAYER MANAGEMENT MENU ===================";
        cout<<"\n1: Add a new Player Record";
        cout<<"\n2: Show all Players Record";
        cout<<"\n3: Delete Player by its Player Id from Record";
        cout<<"\n4: Delete Player by its Player Name from Record";
        cout<<"\n5: Search Player using Player Name";
        cout<<"\n6: Search Player using Player Id";
        cout<<"\n7: Search Player using Dorm No.";
        cout<<"\n8: Search Player using Bed No.";
        cout<<"\n9: Search Players using Parents Name";
        cout<<"\n10: Search Players in a specific Game";
        cout<<"\n11: Count the No. of Player in all Games";
        cout<<"\n12: Count the Total no. of Player's Record";
        cout<<"\n13: Clear the complete database of Player's Record";
        cout<<"\n14: Exit from Program";

        cout<<"\n\nEnter your choice...!\n";
        cin>>choice;
        system("cls");//clearing the console

        switch(choice){

            case 1: fileobj.input();  //Takes input for the instance
                    writeInFile(fileobj); //1: Writes the Input Record in File
                    break;
            case 2: showAllRecords();
                    break;
            case 3: deleteInRecords(1);//3: Delete Player by its Player Id from Record
                    break;
            case 4: deleteInRecords(2);//4: Delete Player by its Player Name from Record
                    break;
            case 5: searchInRecords(1);//5: Search Player using "Player Name"
                    break;
            case 6: searchInRecords(2);//6: Search Player using "Player Id"
                    break;
            case 7: searchInRecords(3);//7: Search Player using "Dorm No."
                    break;
            case 8: searchInRecords(4);//8: Search Player using "Bed No."
                    break;
            case 9: searchInRecords(5);//9: Search for Player's details of particular "Parents Name"
                    break;
            case 10: searchInRecords(6);//10: Search Player's details of particular "Game"
                    break;
            case 11: countPlayersInGame();//11: Count Players in all Games;
                    break;
            case 12: countPlayers();//12: Count Players in individual game
                    break;
            case 13: clearCompleteDatabase();
                    break;
            case 14: cout<<"Exiting from program...\n";
                     exit(0);  //closes the program
            default: cout<<"Wrong Choice..! enter your choice again...\n";
                     break;

        }//end of switch;
   }//end of while

}//end of main

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream  (input and output stream), fstream (For File Handling), string (For String Manipulation), stdlib.h (memory management), conio.h (console input/output).
  • We will create a class called file and create private data members which will store information about the player – playerid, playerName, age, add, phno (we are not taking int because Mob No. is 10 digit which is outside the range of int), parentName, game, coachn, coachName, dorm (dorm number), bed (bed number).
  • We will also create public member functions – 
  • The input() function will let the user input the information/details of the user. The details which user will put are – player id number, age, first and kast name, address (city), phone no., parent’s name, game they play, coach name, dorm number, and bed number.
  • The output() function will output/print the player details which we entered above using the input() function.
  • The retplayername() function will return the player name.
  • The retage() function will return the player’s age.
  • The radd() will return address.
  • The rphno() function will return the phone number of the player. We will take long long as data type as int cannot store the mobile since it is out of range.
  • The rparentn() function will return parent name.
  • The rgame() function will return game name.
  • The rcoachn() function will return coachName.
  • The rdorm() function will return the dorm number.
  • The rbed() function will return the bed number allotted to the player.
  • Now outside the class we will create a function called writeInFile(). This function is used to write the details in the file. It takes details of class instance and append that into the file. It writes the player’s details to a file named “PlayersRecord.txt” in a specific format. For this code snippet – fout<<‘D’; – D will be starting character of Dorm no. in file for unique identification. All Dorm No. will start with D.
  • For fout<<‘B’; – B will be the Starting char of every Bed No. for its unique identification.
  • Now we will create a function called showAllRecords(). This functions shows all the Players record which we have stored in file. It will read from the file “PlayersRecord.txt”. After reading it will also close the file.
  • The deleteInRecords() function is function which deletes the Record of Player from “Playersrecord.txt file” and it can delete the Record of Player using Player Id and Player Name both. If the choice is 1: it asks for Player’s Id else the choice is not 1 here it is 2 as passed by main as 2(deleInRecords(2)): it asks for Player’s Name. It searches the Player’s id and if found it deletes that record.
  • The readFile.close(); will be closing the PlayersRecord,txt file.
  • The writeFile.close(); will be closing sample.txt file.
  • The remove(“PlayersRecord.txt”) function will be deleting Playersrecord.txt file since unmatched(contents which doesn’t need to be deleted) content is copied to sample.txt file.2
  • The rename(“sample.txt”, “PlayersRecord.txt”) function will rename sample.txt as PlayersRecord.txt since Sample.txt contains the contents which were not needed to be deleted.
  • The countPlayers() function counts the Total number Players available in our Record File (“PlayersRecord.txt”). It uses the string “id” to uniquely identify a record(line) since there is id for every Player. So, No. of Player available in Record is equal to no.of times “id” is found in Record File(“PlayersRecord.txt”). Most of the code will be explained in the source code itself.
  • The countPlayersInGame() function counts Total number of Players in all Games here “Basketball”,”TableTennis”,”Cricket”,”Volleyball”,”Hockey”, “Taekwondo”, “Badminton”;. It contains the list of games available for enrollment. We will use for loop and while loop.
  • The searchInRecords() function finds the Player record by using the PlayerId, PlayerName,Parents Name, Game, Dorm No. Bed No. It takes the Player Name as from user & returns the details of the player. It searches the Entered Player name in File “PlayersRecord.txt” and returns the record if it is there. If the Player is not found in the file it Returns “Player not found…!!”. We will use a switch case control statement to enter details.
  • The clearCompleteDatabase() function is used to completely delete the Player Database. It asks for re-confirmation from user. If the user press “Y” it completely clears the Player’s record.
  • The main() function. We will create an object called ‘fileobj’ for the class file. Using the hile loop we will first display the list of menu options on the console. The user will enter the choice. We will use switch case statements to select between choices.
  • This sums up our project of Player Management System using C++ (With Source Code).

Simple RPG Monster Game using C++ (With Source Code)

Final Output:-

Here is an example to show how this project works.

Player Management System Using
C++

Selecting option 1:

Player Management System Using
C++

Player Management System Using
C++

Player Management System Using
C++

Selecting option 2:

Player Management System Using
C++

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

ShutDown and Restart Computer system in C++ (With source code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodewithRandom

ADVERTISEMENT



Leave a Reply