- Back to Home »
- PROJECTS IN C »
- cricket team formation project
Posted by : ANIMESH SHAW
Thursday, 27 October 2011
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
#include<stdlib.h>
class batsman
{
public:
char name[20];
float avg,sr;
friend team;
};
class bowler
{
public:
char name[20],style[5];
float avg;
friend team;
};
class team
{
public:
char tname[20];
batsman bat[11];
bowler bowl[11];
void load(char *);
void teame();
void pedit();
void save(char*,char*);
};
void team::load(char *fn)
{
ifstream fin(fn);
fin>>tname;
for(int i = 0; i < 11; i++)
{
fin>>bat[i].name;
strcpy(bowl[i].name,bat[i].name);
fin>>bat[i].avg;
fin>>bowl[i].avg;
fin>>bowl[i].style;
fin>>bat[i].sr;
}
fin.close();
}
void team::teame()
{
cout<<"Enter new values for the attribute you want to change, else enter previous values";
cout<<"\n\nOld Team Name : ";
cout<<tname;
cout<<"\nEnter the new name for team : ";
cin>>tname;
}
void team::pedit()
{
cout<<"\t\t\t\tPLAYER LIST\n";
cout<<"-------------------------------------------------------------------------------";
cout<<setiosflags(ios::right);
cout<<"\n\n"<<setw(2)<<"No"<<setw(20)<<"Name"<<endl;
for(i = 0; i < 11; i++)
{
cout<<"\n"<<setw(2)<<i+1<<setw(20)<<bat[i].name;
}
cout<<"\n\nEnter the sr.no of player you want to edit : ";
cin>>i;
cout<<"\t\t\t\tPLAYER EDITOR\n";
for(int j = 0; j < 80; j++)
cout<<"-";
cout<<"\n\nPrevious values : ";
cout<<"\nPlayer name : "<<bat[i-1].name;
cout<<"\nBatting avg : "<<bat[i-1].avg;
cout<<"\nBowling avg : "<<bowl[i-1].avg;
cout<<"\nBowling style : "<<bowl[i-1].style;
cout<<"\nBatting strike rate : "<<bat[i-1].sr;
cout<<"\n\nEnter new values for the attribute you want to change, else enter previous values\n";
cout<<"Name : ";
cin>>bat[i-1].name;
strcpy(bowl[i-1].name,bat[i-1].name);
cout<<"Batting avg : ";
cin>>bat[i-1].avg;
cout<<"Bowling avg : ";
cin>>bowl[i-1].avg;
bostyle:
cout<<"Bowling style : ";
cin>>bowl[i-1].style;
if(strcmp(bowl[i-1].style,"RF") && strcmp(bowl[i-1].style,"LF") && strcmp(bowl[i-1].style,"RLS") && strcmp(bowl[i-1].style,"LLS") && strcmp(bowl[i-1].style,"ROS") && strcmp(bowl[i-1].style,"LOS"))
goto bostyle;
cout<<"Batting strike rate : ";
cin>>bat[i-1].sr;
}
void team::save(char nf[7],char of[7] = "\0")
{
ofstream fout(nf);
fout<<tname<<endl;
for(int i = 0; i < 11; i++)
{
fout<<bat[i].name<<endl;
fout<<bat[i].avg<<endl;
fout<<bowl[i].avg<<endl;
fout<<bowl[i].style<<endl;
fout<<bat[i].sr<<endl;
}
fout.close();
remove(of);
}
char teams[20][20],teamc[20][7];
void agree();
void create();
void edit();
void del();
void addplay(char*);
void list();
void blist();
void view();
int main()
{
char choice;
agree();
start:
list();
cout<<"\t\t\t\t\tTEAM EDITOR\n";
for(int i = 0; i < 80; i++)
cout<<"=";
cout<<"\n\n\t\t\t\t1.Create team\n\t\t\t\t2.View Teams\n\t\t\t\t3.Edit Team\n\t\t\t\t4.Delete Team\n\t\t\t\t5.Bolwing style codes\n\t\t\t\t6.Exit\n\t\t\t\tYour choice : ";
choice = getch();
switch(choice)
{
case '1':
create();
goto start;
case '2':
view();
goto start;
case '3':
edit();
goto start;
case '4':
del();
goto start;
case '5':
blist();
goto start;
case '6':
break;
default:
goto start;
}
return 0;
}
void agree()
{
for(int i = 0; i < 80; i++)
cout<<"Í";
cout<<"\n\n \t\t\t\tNOTE\n\n";
cout<<"ÍIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
cout<<"\n\n\n\t This program is meant for editing teams and players. \nYou will be required to input values for the same. It is recommended that you take the back up of \nthe folder in which this game is before editing any team or player because any invalid\n data can lead to incorrect working of the game. \nHowever, if you are confident that data given by you is valid, you need not take the back up. \nPlease note down the bowling style codes before creating or editing player.\n\n Enjoy the game!\n\n";
for(i = 0; i < 80; i++)
cout<<"Í";
cout<<"(Press any key to continue)";
getch();
}
void list()
{
int i = 0;
not = 0;
ifstream fin("teams.log");
while(fin)
{
fin>>teamc[i];
fin>>teams[i];
not++;
i++;
}
fin.close();
}
void create()
{
char fn[7];
ofstream fout;
cout<<"\t\t\t\t\tCREATE TEAM\n";
for(int i = 0; i < 80; i++)
cout<<"-";
cout<<"\n\nEnter the name of new team : ";
cin>>t.tname;
cout<<"Enter the 3 letter code(filename) for the team : ";
again:
cin>>fn;
if(strlen(fn) != 3)
{
cout<<"Invalid code. Re-enter it : ";
goto again;
}
else
{
strcpy(teams[not],t.tname);
strcpy(teamc[not],fn);
fout.open("teams.log");
for(i = 0; i <= not; i++)
{
fout<<teamc[i]<<endl<<teams[i]<<endl;
}
fout.close();
strcat(fn,".tem");
addplay(fn);
}
list();
getch();
}
void edit()
{
char fn[7],fn1[7],tn[20],tn1[20];
char tname[20],code[7];
ifstream fin;
ofstream fout;
cout<<"\t\t\t\t\tTEAM EDIT\n";
for(int i = 0; i < 80; i++)
cout<<"-";
cout<<setiosflags(ios::left);
cout<<"\t\t\t"<<setw(20)<<"Team Name"<<setw(5)<<"Code"<<endl<<endl<<endl;
for(i = 0; i < not; i++)
cout<<"\t\t\t"<<setw(20)<<teams[i]<<setw(5)<<teamc[i]<<endl;
cout<<"\n\n\nEnter the team code(type 'back'to go back to main menu) : ";
repeat:
cin>>fn;
if(strcmp(fn,"back"))
{
strcpy(tn,fn);
strcat(tn,".tem");
fin.open(tn);
if(fin.good() == 0)
{
cout<<"Team does not exist";
cout<<"\nRe-enter code : ";
fin.close();
goto repeat;
}
else
{
fin.close();
cout<<"Loading, please wait...";
clrscr();
t.load(tn);
cout<<"\t\t\t\t\t"<<t.tname<<endl;
for(i = 0; i < 80; i++)
cout<<"-";
cout<<"\n\n\t\t\t\t1.Edit team\n\t\t\t\t2.Edit Player";
switch(getch())
{
case '1':
t.teame();
cout<<"\n\nOld Team Code : ";
cout<<fn;
cout<<"\nEnter the new code for team : ";
rename:
cin>>fn1;
if(strlen(fn1) != 3)
{
cout<<"Invalid code. Re - enter code : ";
goto rename;
}
else
{
strcpy(tn1,fn1);
strcat(tn1,".tem");
if(strcmp(fn,fn1))
t.save(tn1,tn);
else
t.save(tn);
fin.open("teams.log");
fout.open("temp.log");
while(fin)
{
fin>>tn;
fin>>tn1;
if(strcmp(fn,tn))
{
fout<<tn<<endl<<tn1<<endl;
}
else
{
fout<<fn1<<endl<<t.tname<<endl;
}
}
fin.close();
fout.close();
remove("teams.log");
rename("temp.log","teams.log");
cout<<"Team successfully edited.";
list();
}
break;
case '2':
t.pedit();
t.save(tn);
cout<<"Player edited successfully";
break;
}
}
}
}
void del()
{
char fn[7],tn[20];
ofstream fout;
ifstream fin;
cout<<"\t\t\t\tDELETE TEAM\n";
for(int i = 0; i < 80; i++)
cout<<"-";
cout<<setiosflags(ios::left);
cout<<"\t\t\t"<<setw(20)<<"Team Name"<<setw(5)<<"Code"<<endl<<endl<<endl;
for(i = 0; i < not; i++)
cout<<"\t\t\t"<<setw(20)<<teams[i]<<setw(5)<<teamc[i]<<endl;
cout<<"\n\n\nEnter the team code(type 'back'to go back to main menu) : ";
repeat:
cin>>fn;
if(strcmp(fn,"back"))
{
strcpy(tn,fn);
strcat(tn,".tem");
remove(tn);
fin.open("teams.log");
fout.open("temp.log");
while(fin)
{
fin>>tn;
if(strcmp(tn,fn))
{ fout<<tn<<endl;
fin>>tn;
fout<<tn<<endl;
}
else
{
fin>>tn;
}
}
fin.close();
fout.close();
remove("teams.log");
rename("temp.log","teams.log");
}
list();
}
void addplay(char *fn)
{
ofstream fout(fn);
fout<<t.tname<<endl;
cout<<"Enter the name, batting avg ,bowling avg, bowling style and batting strike rate of 11 players : \n";
for(int i = 0; i < 11; i++)
{
cout<<"\nName : ";
cin>>t.bat[i].name;
strcpy(t.bowl[i].name,t.bat[i].name);
fout<<t.bat[i].name<<endl;
cout<<"Batting avg : ";
cin>>t.bat[i].avg;
if(!strcmp(t.bat[i].name,"D.Bradman"))
t.bat[i].avg = 90;
fout<<t.bat[i].avg<<endl;
cout<<"Bowling avg : ";
cin>>t.bowl[i].avg;
if(!strcmp(t.bowl[i].name,"G.Mcgrath"))
t.bowl[i].avg = 5;
fout<<t.bowl[i].avg<<endl;
bstyle:
cin>>t.bowl[i].style;
if(strcmp(t.bowl[i].style,"RF") && strcmp(t.bowl[i].style,"LF") && strcmp(t.bowl[i].style,"RLS") && strcmp(t.bowl[i].style,"LLS") && strcmp(t.bowl[i].style,"ROS") && strcmp(t.bowl[i].style,"LOS"))
goto bstyle;
fout<<t.bowl[i].style<<endl;
cout<<"Batting strike rate : ";
cin>>t.bat[i].sr;
fout<<t.bat[i].sr<<endl;
}
fout.close();
cout<<"\nTeam successfully created.";
}
void blist()
{
cout<<"\t\t\t\tBOWLING STYLES LIST\n";
for(int i = 0; i < 80; i++)
cout<<"-";
cout<<"\n\n\n\t\t"<<setw(25)<<"STYLE"<<setw(15)<<"CODE"<<endl<<endl;
cout<<"\t\t"<<setw(25)<<"Right arm fast"<<setw(15)<<"RF"<<endl;
cout<<"\t\t"<<setw(25)<<"Left arm fast"<<setw(15)<<"LF"<<endl;
cout<<"\t\t"<<setw(25)<<"Right arm leg spin"<<setw(15)<<"RLS"<<endl;
cout<<"\t\t"<<setw(25)<<"Left arm leg spin"<<setw(15)<<"LLS"<<endl;
cout<<"\t\t"<<setw(25)<<"Right arm off spin"<<setw(15)<<"ROS"<<endl;
cout<<"\t\t"<<setw(25)<<"Left arm off spin"<<setw(15)<<"LOS"<<endl;
cout<<"\n\n\n(Press any key to continue)";
getch();
}
void view()
{
char fn[8],tn[8];
ifstream fin;
cout<<"\t\t\t\t\tVIEW TEAMS\n";
for(int i=0;i<80;i++)
cout<<"-";
cout<<setiosflags(ios::left)<<setprecision(2);
cout<<"\t\t\t"<<setw(20)<<"Team Name"<<setw(5)<<"Code"<<endl<<endl<<endl;
for(i = 0; i < not; i++)
cout<<"\t\t\t"<<setw(20)<<teams[i]<<setw(5)<<teamc[i]<<endl;
cout<<"\n\n\nEnter the team code(type 'back'to go back to main menu) : ";
repeat10:
cin>>fn;
if(strcmp(fn,"back"))
{
strcpy(tn,fn);
strcat(tn,".tem");
fin.open(tn);
if(fin.good() == 0)
{
cout<<"Team does not exist";
cout<<"\nRe-enter code : ";
fin.close();
goto repeat10;
}
else
{
fin.close();
t.load(tn);
cout<<"\t\t\t\t"<<t.tname<<endl;
for(i = 0; i < 80; i++)
cout<<"-";
cout<<"\n\n"<<setw(20)<<"NAME"<<setw(15)<<"Bat. avg."<<setw(15)<<"bowl.avg."<<setw(15)<<"Bowl style"<<setw(15)<<"Bat. StrikeRate";
cout<<endl;
for(i = 0; i < 11; i++)
cout<<endl<<setw(20)<<t.bat[i].name<<setw(15)<<t.bat[i].avg<<setw(15)<<t.bowl[i].avg<<setw(15)<<t.bowl[i].style<<setw(15)<<t.bat[i].sr;
cout<<"\n\n\n(Press any key to continue)";
getch();
}
}
}

Post a Comment