Answer to Question #210050 in C++ for hjm

Question #210050

Sport management project in main()


1
Expert's answer
2021-06-23T17:06:16-0400
#include<iostream>
#include<fstream>  
#include<string>  
#include<stdlib.h>
#include<conio.h>
using namespace std;

class file{
    private:
        int playerid;
        string playerName;
        int age;
        char add[30];
        long long phno; 
        string parentName;
        char game[30];
        string coachName;
        int dorm;
        int bed;
    public:
        void input(){
            cout<<"\nEnter player id no: ";
            cin>>playerid;
            cout<<"\nEnter player age: ";
            cin>>age;
            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;
            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;


            cout<<"\nEnter the First Name of Coach: ";
            cin>>strFname;
            cout<<"\nEnter the Last Name of Coach: ";
            cin>>strLname;
            coachName = strFname +" "+ strLname;
            cout<<"\nEnter dorm no.: ";
            cin>>dorm;
            cout<<"\nEnter bed no.: ";
            cin>>bed;
        }
        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;


        }
        string retplayername(){
            return playerName;
        }
        int retplayerid(){
            return playerid;
        }
        int retage(){
            return age;
        }
        char* radd(){
            return add;
        }
        long long  rphno(){ 
            return phno;
        }


       string rparentn(){
            return parentName;
       }
        char* rgame(){
            return game;
        }
        string rcoachn(){
            return coachName;
        }
        int rdorm(){
            return dorm;
        }
        int rbed(){
            return bed;
        }
};
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';   
    fout<<fobj.rdorm();fout<<arr;
    fout<<'B'; 
    fout<<fobj.rbed();fout<<arr;
    fout<<'\n';
    fout.close();
}
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();


}
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{             
        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");
    int count=0;
    bool found = false; //checks wheather Player Record is there or not
    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;
        }
        else{
            writeFile<<charsInLine;
            writeFile<<"\n";
        }
        count++;
    }
    readFile.close();  //closing the PlayersRecord,txt file
    writeFile.close(); //closing sample.txt file


    remove("PlayersRecord.txt");    
    rename("sample.txt", "PlayersRecord.txt");
    if(found == false){
       cout<<"No records available...!\n";
    }
}
void countPlayers(){
    ifstream readFile;
    char charsInLine[1024];
    int countPlayer=0;


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


        readFile.getline(charsInLine,1024);
        string strLine(charsInLine); 
        size_t found = strLine.find("id"); 
        if(found!= string::npos){
            countPlayer++;
            cout<<charsInLine<<endl;  
        }
        else{
            break; 
        }
    }


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


    ifstream readFile;
    readFile.open("PlayersRecord.txt");
    char charsInLine[1024];
    int count=0;  //string strLine; 
    string games[7]={"Basketball","TableTennis","Cricket","Volleyball","Hockey", "Taekwondo", "Badminton"};
    string gameName="";
    for(int i=0; i<7 ; i++){    
        gameName = games[i]; 
        int countGame = 0;
        ifstream readFile;
        readFile.open("PlayersRecord.txt");
        while(!readFile.eof()){
            readFile.getline(charsInLine, 1024);
            string strLine(charsInLine);  
            size_t found = strLine.find(gameName); 
            if(found!= string::npos){
                countGame++;
            }
        }
        readFile.close();
        cout<<"No. of Players enrolled in "<<gameName<<": "<<countGame<<endl;
        countGame = 0;
   }
}
void searchInRecords(int choice){
    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;  
                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;


    }




    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;
    }
}
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;  


    while(1){
        int choice;
        cout<<"\n\n=============== SPORTS MANAGEMENT SYSTEM ===================";
        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");


        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);  
            default: cout<<"Wrong Choice..! enter your choice again...\n";
                     break;
        }
   }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS