Demo entry 6314825

Project

   

Submitted by Devil on Oct 30, 2016 at 08:57
Language: C++. Code size: 19.9 kB.

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<ctype.h>
#include<iomanip.h>
struct dob
{
 int day;
 int month;
 int year;
};
struct teamrate
{
 float rte;
 int no;
 char tname[20];
};

class player
{
 private: int batrank; //Out of 100
          int bowlrank;//Out of 100
          dob d;
          char name[50];
          int age;
          char team[20];
 public:  void read();
          void show();
          int rate();
          char* getname()
          {
            return name;
          }
          int getbat()
          {
            return batrank;
          }
          int getbowl()
          {
            return bowlrank;
          }
          char* retteam()
          {
            return team;
          }

};
void player::read()
{
 start1:
 cout<<"\n\nEnter the name of player:";
 gets(name);
 cout<<"\nEnter the age:";
 cin>>age;
  if(cin.fail())
 {
     cout<<"\nData entered is not of an integer";
     cout<<"\nYou have to enter the player's details again..";
     goto start1;
 }
 cout<<"\nEnter the team:";
 gets(team);
  if(cin.fail())
 {
     cout<<"\nData entered is not of a string";
     cout<<"\nYou have to enter the player's details again..";
     goto start1;
 }
 do
 {
 cout<<"\nEnter the batting rating(Out of 100):";
 cin>>batrank;
 cout<<"\nEnter the bowling rating(Out of 100):";
 cin>>bowlrank;
 }while(batrank>=100&&bowlrank>=100);
 cout<<"\nEnter the Date of Birth:";
 cout<<"\nEnter the day:";
 cin>>d.day;
 cout<<"\nEnter the month:";
 cin>>d.month;
 cout<<"\nEnter the Year:";
 cin>>d.year;
}
int player::rate()
{
 int r;
 r=(batrank+bowlrank)/2;
 return r;
}
void player::show()
{
 cout<<"\nName:"<<name;
 cout<<"\nAge:"<<age;
 cout<<"\nTeam:"<<team;
 cout<<"\nBatting:"<<batrank;
 cout<<"\nBowling:"<<bowlrank;
 cout<<"\nDate of Birth:"<<d.day<<"-"<<d.month<<"-"<<d.year;
 cout<<"\nPlayer rate:";
 int r = rate();
 cout<<r;
}


//***************************FUNCTIONS USED********************************
 void Menu();
 void Append();
 void Search();
 void Delete(char filename[20]);
 void Modify();
 void Ranking();
 void Squad();
 void Compare();
 void CreateTeam();
//*************************************************************************
player a[120];
int no,no1; //No of Players
void player_arr(char filename[20])
{
 no=0,no1=0;
 player p;
 fstream f;
 f.open(filename,ios::in|ios::binary);
 f.seekg(0);
 while(f.read((char*)&p,sizeof(p)))
   {
    if(strcmpi(filename,"playertst.dat")==0)
    {
    a[no]=p;
    ++no;
    }
    else if(strcmpi(filename,"createteam001.dat")==0)
    {
    a[no1]=p;
    ++no1;
    }
   }
}
void Append(char filename[20])
 {
   clrscr();
   cout<<"\n\t\t--------------";
   cout<<"\n\t\tADDING PLAYERS";
   cout<<"\n\t\t--------------\n";
   char ch;
   player p;
   fstream f;
   f.open(filename,ios::app|ios::binary);
   f.seekg(0);
   do
   {
    p.read();
    f.write((char*)&p,sizeof(p));
    cout<<"\nDo you want to add more players:";
    cin>>ch;
   }while(ch=='Y');
   f.close();
   player_arr(filename);
   cout<<"\nPress any key to go back ..";
   getch();
   if(strcmpi(filename,"playertst.dat")==0)
    Menu();
   else
    CreateTeam();
 }
 void Search(char filename[20])
 {
  char srch[30];
  clrscr();
  cout<<"\n\t\t------";
  cout<<"\n\t\tSEARCH";
  cout<<"\n\t\t------\n";
  int flag=0;
  player p;
  fstream f;
  f.open(filename,ios::in|ios::binary);
  f.seekg(0);
  cout<<"\nEnter the name:";
  gets(srch);
  while(f.read((char*)&p,sizeof(p)))
   {
    if(strcmpi(srch,p.getname())==0)
    {
     p.show();
     flag=1;break;
    }
   }
   f.close();
   if(flag!=1)
   cout<<"\nSearch not found!";
   cout<<"\nPress any key to go back to the menu..";
   getch();
if(strcmpi(filename,"playertst.dat")==0)
    Menu();
   else
    CreateTeam();
 }
void Delete(char filename[20])
{
 fstream f,t;
 f.open(filename,ios::in|ios::binary);
 t.open("temp.dat",ios::out|ios::binary);
 f.seekg(0);
 player p;
 int flag=0;
 char srch[30];
 clrscr();
 cout<<"\n\t\t------";
 cout<<"\n\t\tDELETE";
 cout<<"\n\t\t------\n";
 cout<<"\nEnter the name of the player to be deleted:";
 gets(srch);
 while(f.read((char*)&p,sizeof(p)))
 {
  if(strcmpi(srch,p.getname())==0)
  {
     cout<<"\a\nPlayer: "<<p.getname()<<" is deleted!";
     flag=1;
  }
  else
     t.write((char*)&p,sizeof(p));
 }
 if(flag==0)
   cout<<"Entered player is not found!";
   f.close();
   t.close();
 remove(filename);
 rename("temp.dat",filename);
 player_arr(filename);
 cout<<"\nPress any key to continue..";
   getch();
  if(strcmpi(filename,"playertst.dat")==0)
    Menu();
   else
    CreateTeam();
 }
 void Modify(char filename[20])
 {
   fstream f,t;
   f.open(filename,ios::in|ios::binary);
   t.open("temp.dat",ios::out|ios::binary);
   f.seekg(0);
   player p;
   int flag=0;
   char srch[30];
   clrscr();
   cout<<"\n\t\t------";
   cout<<"\n\t\tMODIFY";
   cout<<"\n\t\t------\n";
   cout<<"\nEnter the name of the player to be modified:";
   gets(srch);
   while(f.read((char*)&p,sizeof(p)))
  {
   if(strcmpi(srch,p.getname())==0)
    {
     p.show();
     cout<<"\n\t\tEnter the new Details:";
     p.read();
     t.write((char*)&p,sizeof(p));
     flag=2;
    }
   else
     t.write((char*)&p,sizeof(p));
  }
  if(flag==0)
   cout<<"\nEntered player is not found!";
  f.close();
  t.close();
  remove(filename);
  rename("temp.dat",filename);
  player_arr(filename);
  cout<<"\nPress any key to go back to the menu..";
   getch();
 if(strcmpi(filename,"playertst.dat")==0)
    Menu();
   else
    CreateTeam();
}


void Ranking()
{
 int ch,l,j,k;
 player temp;
 teamrate s[10],tmp;
 fstream f;
 f.open("playertst.dat",ios::in|ios::binary);
 f.seekg(0);
 clrscr();
 cout<<"\n\t\t-------";
 cout<<"\n\t\tRANKINGS";
 cout<<"\n\t\t--------\n";
 cout<<"\n Select:\n";
 cout<<"\n 1=>Top 10 Batsmen";
 cout<<"\n 2=>Top 10 Bowlers";
 cout<<"\n 3=>Top 10 All rounders";
 cout<<"\n 4=>Team Rankings\n";
 cout<<"\n Enter the choice:";
 cin>>ch;

   int i=0;
 switch(ch)
 {
  case 1:cout<<"\n\tTOP 10 BATSMEN\n";
         for(j=0;j<no;++j)
         {for(k=0;k<no-j;++k)
         if(a[j].getbat()>a[k].getbat())
         {
         temp=a[j];
         a[j]=a[k];
         a[k]=temp;
         }
         }
         cout<<"\n\n";
         cout<<setw(2)<<"No ";
         cout<<setw(20)<<"Name";
         cout<<setw(20)<<"      Batting Rate";
         cout<<setw(20)<<"      Nationality";
         cout<<"\n\n";
         for(k=0;k<10;++k)
         {
         cout<<setw(2)<<(k+1)<<". "<<setw(20)<<a[k].getname();
         cout<<setw(13)<<a[k].getbat();
         cout<<setw(23)<<a[k].retteam();
         cout<<"\n\n";
         }
         break;
  case 2:cout<<"\n\tTOP 10 BOWLERS\n";
         for(j=0;j<no;++j)
         for(k=0;k<no-j;k++)
         if(a[k].getbowl()<a[k+1].getbowl())
         {
           temp=a[k];
           a[k]=a[k+1];
           a[k+1]=temp;
         }
         cout<<"\n\n";
         cout<<setw(2)<<"No ";
         cout<<setw(20)<<"Name";
         cout<<setw(20)<<"      Bowling Rate";
         cout<<setw(20)<<"     Nationality";
         cout<<"\n\n";
         for(l=0;l<10;++l)
         {
         cout<<setw(2)<<(l+1)<<". "<<setw(20)<<a[l].getname();
         cout<<setw(13)<<a[l].getbowl();
          cout<<setw(23)<<a[l].retteam();
         cout<<"\n\n";
         }
         break;

  case 3:cout<<"\n\tTOP 10 ALLROUNDERS\n";
         for(j=0;j<no;++j)
         for(k=0;k<no-j;k++)
         if(a[k].rate()<a[k+1].rate())
         {
          temp=a[k];
          a[k]=a[k+1];
          a[k+1]=temp;
         }
         cout<<"\n\n";
         cout<<setw(2)<<"No ";
         cout<<setw(20)<<"Name";
         cout<<setw(10)<<"Rate";
         cout<<setw(20)<<"      Nationality";
         cout<<"\n\n";
         for(l=0;l<10;++l)
         {
         cout<<setw(2)<<(l+1)<<". "<<setw(20)<<a[l].getname();
         cout<<setw(8)<<a[l].rate();
         cout<<setw(23)<<a[l].retteam();
         cout<<"\n\n";
         }
         break;

  case 4:cout<<"\n\tTOP  TEAMS\n";
  for(j=0;j<6;++j)
   {
    s[j].no=0;
    s[j].rte=0;
   }
         for(j=0;j<no;++j)
         {
          if(strcmpi(a[j].retteam(),"India")==0)
           {
             s[0].rte+=a[j].rate();
             s[0].no++;
             strcpy(s[0].tname,"India");
           }
         else if(strcmpi(a[j].retteam(),"Australia")==0)
           {
             s[1].rte+=a[j].rate();
             s[1].no++;
             strcpy(s[1].tname,a[j].retteam());
           }
         else if(strcmpi(a[j].retteam(),"England")==0)
           {
             s[2].rte+=a[j].rate();
             s[2].no++;
             strcpy(s[2].tname,a[j].retteam());
           }
         else if(strcmpi(a[j].retteam(),"West Indies")==0)
           {
             s[3].rte+=a[j].rate();
             s[3].no++;
             strcpy(s[3].tname,a[j].retteam());
           }
         else if(strcmpi(a[j].retteam(),"Sri Lanka")==0)
           {
             s[4].rte+=a[j].rate();
             s[4].no++;
             strcpy(s[4].tname,a[j].retteam());
           }
          else if(strcmpi(a[j].retteam(),"New Zealand")==0)
           {
             s[5].rte+=a[j].rate();
             s[5].no++;
             strcpy(s[5].tname,a[j].retteam());
           }
          }
          for(j=0;j<6;++j)
          s[j].rte=(s[j].rte)/(s[j].no);
          for(j=0;j<=5;++j)
          for(k=0;k<=5;++k)
          if(s[k].rte<s[j].rte)
          {
           tmp=s[k];
           s[k]=s[j];
           s[j]=tmp;
          }
          cout<<"\n";
           cout<<setw(2)<<"No ";
         cout<<setw(12)<<"Name";
         cout<<setw(11)<<"Rate";
          for(j=0;j<6;++j)
          {
           cout<<"\n\n"<<setw(2)<<(j+1)<<". "<<setw(12)<<s[j].tname;
           cout<<setw(10)<<setprecision(4)<<s[j].rte;
          }
          break;
     }
     f.close();
     cout<<"\n\nPress any key to go back to the menu..";
   getch();
   Menu();
  }

   void Squad(char filename[20])
   {
    int j;
    char sqname[30];

    fstream f;
    int flag=0,k=1;
    f.open(filename,ios::in|ios::binary);
    f.seekg(0);
    clrscr();
    cout<<"\n\t\t-----";
    cout<<"\n\t\tSQUAD";
    cout<<"\n\t\t-----\n";
    cout<<"\nEnter the team whose players are to be viewed:";
    gets(sqname);
    for(j=0;j<no;++j)
      {
        if(strcmpi(sqname,a[j].retteam())==0)
        {
          cout<<"\n"<<k<<". "<<a[j].getname();
          ++k;
          flag=1;
        }
      }
      if(flag==0)
      cout<<"\nTeam Invalid";
      cout<<"\nPress any key to go back to the menu..";
       f.close();
   getch();
  if(strcmpi(filename,"playertst.dat")==0)
    Menu();
   else
    CreateTeam();
   }

  void Compare(char filename[20])
  {
    int j,i,flag=0;
    char sch1[50],sch2[50];
    clrscr();
    cout<<"\n\t\t-------";
    cout<<"\n\t\tCOMPARE";
    cout<<"\n\t\t-------\n";
    cout<<"\nEnter the first player's name:";
    gets(sch1);
    cout<<"\nEnter the second player's name:";
    gets(sch2);
    fstream f;
    f.open(filename,ios::in|ios::binary);
    f.seekg(0);
     for(i=0;i<no;++i)
     { for(j=0;j<no;++j)
     {
       if((strcmpi(sch1,a[i].getname())==0)&&(strcmpi(sch2,a[j].getname())==0))
       {
        flag=1;
        a[i].show();
        cout<<"\n";
        a[j].show();
        cout<<"\n\nPLAYER ANALYSIS\n";
        if(a[i].rate()>a[j].rate())
         cout<<"\nBetter Player: "<<a[i].getname();
        else if(a[i].rate()<a[j].rate())
         cout<<"\nBetter Player: "<<a[j].getname();
         else
          cout<<"\nBoth are equally good players!!!";
         if(a[i].getbat()>a[j].getbat())
          cout<<"\nBetter Batsman:"<<a[i].getname();
         else if(a[i].getbat()<a[j].getbat())
          cout<<"\nBetter Batsman:"<<a[j].getname();
         else
          cout<<"\nBoth are equally good batsmen!!!";
         if(a[i].getbowl()>a[j].getbowl())
          cout<<"\nBetter Bowler:"<<a[i].getname();
         else if(a[i].getbowl()<a[j].getbowl())
          cout<<"\nBetter Bowler:"<<a[j].getname();
         else
          cout<<"\nBoth are equally good bowlers!!!";
       }
     }
     }
     if(flag==0)
     cout<<"\nEntered player does not exist!!!";
     f.close();
     cout<<"\nPress any key to go back to the menu..";
   getch();
if(strcmpi(filename,"playertst.dat")==0)
    Menu();
   else
    CreateTeam();
  }
  void Info()
  {
   int i,j,k;

   for(i=0;i<20;++i)
   for(k=0;k<150;++k)
   {
     clrscr();
    for(j=0;j<i;++j)
    {
     cout<<"\n";
    }
    cout<<"\n\t----";
    cout<<"\n\tINFO";
    cout<<"\n\t----\n";
    cout<<"\nProgram : Cricblitz";
    cout<<"\nCreators: Abhishek Ajoy, Sandeed Mahin";
    cout<<"\nCompiler: Borland C++ 5.02";
    cout<<"\nThank You for Using.";

   }
   cout<<"\nPress any key to go back to the menu..";
   getch();
   Menu();
  }
  void CreateTeam()
  {
   clrscr();
   cout<<"\n\t\t-----------";
   cout<<"\n\t\tCREATE TEAM";
   cout<<"\n\t\t-----------\n";
   fstream f,t;
   f.open("playertst.dat",ios::in|ios::binary);
   t.open("createteam001.dat",ios::app|ios::binary);
   f.seekg(0);
   player p,batsman=a[0],bowler=a[0],allrounder=a[0];
   int flag=0,j,i;
   char teamname[30],srch[20],ch,ch1,filename1[20];
   if(no1==0)
   {
   cout<<"\nEnter the Name of your team:";
   gets(teamname);
   }
   t.close();
   f.close();
   start1:
   player_arr(filename1);
   clrscr();
   cout<<"\n\t\t-----------";
   cout<<"\n\t\tCREATE TEAM";
   cout<<"\n\t\t-----------\n";
   cout<<"\n\t1.  ADD PLAYERS..........=>PRESS 1\n";
   cout<<"\n\t2.  DELETE PLAYER........=>PRESS 2\n";
   cout<<"\n\t3.  SEARCH PLAYER........=>PRESS 3\n";
   cout<<"\n\t4.  MODIFY PLAYER........=>PRESS 4\n";
   cout<<"\n\t5.  DISPLAY..............=>PRESS 5\n";
   cout<<"\n\t6.  VIEW SQUADS..........=>PRESS 6\n";
   cout<<"\n\t7.  COMPARE PLAYERS......=>PRESS 7\n";
   cout<<"\n\t8.  TEAM ANALYSIS........=>PRESS 8\n";
   cout<<"\n\t9.  GO BACK TO MAIN MENU.=>PRESS 9\n";
    strcpy(filename1,"createteam001.dat");
   int choice;
   do
   {
    player_arr("playertst.dat");

     cout<<"\n\tEnter your choice:";
     cin>>choice;

   }while(choice<1&&choice>10);
   do
   {
   switch(choice)
   {

    case 1: {
            clrscr();
            cout<<"\n\t\t--------------";
            cout<<"\n\t\tADDING PLAYERS";
            cout<<"\n\t\t--------------\n";
            char ch;
            player p;
            fstream f;
            f.open(filename1,ios::app|ios::binary);
            f.seekg(0);
            do
            {
             cout<<"\nEnter the Name of the player:";
             gets(srch);
             for(i=0;i<no;++i)
             {
               if(strcmpi(srch,a[i].getname())==0)
               {
                cout<<"\n"<<a[i].getname()<<" is added to "<<teamname;
                flag=1;
                no1++;
                p=a[i];
                f.write((char*)&p,sizeof(p));break;
               }
             }
              if(flag==0)
              cout<<"\nPlayer not found!";
              cout<<"\nDo you want to add more players:";
              cin>>ch;
            }while(ch=='Y');
            f.close();
            goto start1;
            }
    case 2: Delete(filename1);goto start1;;
    case 3: Search(filename1);goto start1;;
    case 4: Modify(filename1);goto start1;
    case 5: {
             clrscr();
             cout<<"\n\t\t------------";
             cout<<"\n\t\tDISPLAY TEAM";
             cout<<"\n\t\t------------\n";

             fstream t;
             t.open("createteam001.dat",ios::in|ios::binary);
             t.seekg(0);
             j=1;
             cout<<"\n"<<teamname<<"\n";
              while(t.read((char*)&p,sizeof(p)))
              {
                cout<<"\n"<<j<<". "<<p.getname();
                j++;
              }
              t.close();
              cout<<"\n\nPress Any Key to continue...";
              getch();
              goto start1;
            }
    case 6: Squad(filename1);goto start1;
    case 7: Compare(filename1);goto start1;
    case 8: clrscr();
            cout<<"\n\t\t------------";
            cout<<"\n\t\tTEAM ANALYSIS";
            cout<<"\n\t\t------------\n";
            t.open("createteam001.dat",ios::in|ios::binary);
            while(t.read((char*)&p,sizeof(p)))
            {
             if(batsman.getbat()<p.getbat())
                batsman=p;
             if(bowler.getbowl()<p.getbowl())
                bowler=p;
             if(allrounder.rate()<p.rate())
                allrounder=p;
            }
            cout<<"\n\nBest Batsman: "<<batsman.getname();
            cout<<"\nBest Bowler:  "<<bowler.getname();
            cout<<"\nBest All-rounder: "<<allrounder.getname();
            cout<<"\n\nPress Any Key to continue...";
              getch();
            goto start1;
    case 9: cout<<"\nPress any key to go back to menu..";
            getch();
            Menu();
   default:
            cout<<"\nInvalid option";
            cout<<"\nPress any key to go back to create team menu..";
            getch();
            goto start1;
  }
   cout<<"\nDo you want to continue(Y/N):";
   cin>>ch1;
  }while(ch1=='Y');
 cout<<"\nPress any key to go back to menu..";
 getch();
 Menu();
 }

  void Menu()
  {
   start:
   clrscr();
   char filename[20];
   strcpy(filename,"playertst.dat");
   player_arr(filename);
   cout<<"\n\t\t\t----";
   cout<<"\n\t\t\tMENU";
   cout<<"\n\t\t\t----";
   cout<<"\n\t1.  ADD PLAYERS.......=>PRESS 1\n";
   cout<<"\n\t2.  DELETE PLAYER.....=>PRESS 2\n";
   cout<<"\n\t3.  SEARCH PLAYER.....=>PRESS 3\n";
   cout<<"\n\t4.  MODIFY PLAYER.....=>PRESS 4\n";
   cout<<"\n\t5.  RANKINGS..........=>PRESS 5\n";
   cout<<"\n\t6.  VIEW SQUADS.......=>PRESS 6\n";
   cout<<"\n\t7.  COMPARE PLAYERS...=>PRESS 7\n";
   cout<<"\n\t8.  CREATE YOUR TEAM..=>PRESS 8\n";
   cout<<"\n\t9.  APP INFO..........=>PRESS 9\n";
   cout<<"\n\t10. EXIT.............=>PRESS 10\n";

   int choice;
   do
   {
   cout<<"\n\tEnter your choice:";
   cin>>choice;
   }while(choice<1&&choice>10);
   switch(choice)
   {
    case 1: Append(filename);break;
    case 2: Delete(filename);break;
    case 3: Search(filename);break;
    case 4: Modify(filename);break;
    case 5: Ranking();break;
    case 6: Squad(filename);break;
    case 7: Compare(filename);break;
    case 8: CreateTeam();break;
    case 9: Info();break;
    case 10: cout<<"\nPress any key to exit..";
             getch();
             exit(0);
    default:
             cout<<"\nInvalid option";
             cout<<"\nPress any key to go back to menu..";
             getch();
             goto start;
  }
 }
 void main()
 {
  char filename[20];
  strcpy(filename,"playertst.dat");
  cout<<"\n ****  *****   *****   ****  *****   *      *****  *****  *****";
  cout<<"\n*      *    *    *    *      *    *  *        *      *       * ";
  cout<<"\n*      *****     *    *      *****   *        *      *      *  ";
  cout<<"\n*      *    *    *    *      *    *  *        *      *     *   ";
  cout<<"\n ****  *    *  *****   ****  *****   *****  *****    *    *****";
  cout<<"\n\n\n\n\tPress Any Key to Continue.....";
  getch();
  player_arr(filename);
  Menu();
  }

This snippet took 0.05 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).