This is all the code i dont know where the problem is
there is no errors but the program stops at certain point which is after asking the player to enter his name after this the program stops completly i hope any 1 could give me the answer of this....
Note: if you are trying to run this problem choose the first level only cause i havent completed the rest of cases yet
and thxxx
QUOTE
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
struct highscore // structure to have the name and score of each player
{
string name;
int score;
};
// End of the Structure
// Function to add the name and score for player in the level played
void append (string file, string target , int s)
{
highscore y;
y.name=target;
y.score=s;
ofstream x;
x.open((file.c_str()),ios::app);
x<<y.name<<y.score;
x.close();
}
// End of the function
// Function to check the player's name wheter found before or not
// If found update his score
// If not found create a new score for him
void check_player_name(string file, string target , int s)
{
int i=0;
ofstream q;
ifstream r;
r.open((file.c_str()));
highscore b[10];
while (!r.eof())
{
r>> b[i].name >> b[i].score;
i++;
}
r.close();
for(i=0;i<10;i++)
{
if (target.compare(b[i].name) == 0)
break;
}
if ( i == 10)
append (file , target , s);
else
{
q.open((file.c_str()));
b[i].score<<s;
for (int j=0 ; j<10 ; j++)
{
q << b[j].name <<b[j].score;
}
q.close();
}
}
// End of the function
void game_level_2(int &check2)
{
string High_scores_level_2;
string name1;
string name2;
int score1=0;
int score2=0;
int check =0;
int r , c,w,z;
cout<<" Player 1 will sign ' x ' while player 2 will sign ' o '"<<endl<<endl;
int x[4][4];
for (int j=0 ; j<4; j++)
{
cout<< setw(6)<<"l"<<setw(6)<<"l"<<setw(6)<<"l";
cout<<endl;
for (int i=0; i<4; i++)
cout<<setw(5)<<" ____";
cout<<endl;
}
for (z=0; z<16 ; z++)
{
cout<<" Player 1: enter first the number of the row then number of cloumb"<<endl;
cout<<" to mark your sign";
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=1;
if (x[0][0] ==1 && x[0][1] ==1 && x[0][2] == 1 && x[0][3]== 1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[1][0] ==1 && x[1][1] ==1 && x[1][2] == 1 && x[1][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[2][0] ==1 && x[2][1] ==1 && x[2][2] == 1 && x[2][3]==1 )
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[3][0] ==1 && x[3][1] ==1 && x[3][2] == 1 && x[3][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][1] ==1 && x[2][2] == 1 && x[3][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][3] ==1 && x[1][2] ==1 && x[2][1] == 1 && x[3][0]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][0] ==1 && x[2][0] == 1 && x[3][0]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][1] ==1 && x[1][1] ==1 && x[2][1] == 1 && x[3][1]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][2] ==1 && x[2][2] == 1 && x[3][2]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][3] ==1 && x[1][3] ==1 && x[2][3] == 1 && x[3][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
}
cout<< " Now player 2 enter the number of row then coloumb"<<endl;
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=2;
if (x[0][0] ==2 && x[0][1] ==2 && x[0][2] == 2 && x[0][3] ==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[1][0] ==2 && x[1][1] ==2 && x[1][2] == 2 && x[1][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[2][0] ==2 && x[2][1] ==2 && x[2][2] == 2 && x[2][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[3][0] ==2 && x[3][1] ==2 && x[3][2] == 2 && x[3][3]== 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][1] ==2 && x[2][2] == 2 && x[3][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][3] ==2 && x[1][2] ==2 && x[2][1] == 2 && x[3][0]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][0] ==2 && x[2][0] == 2 && x[3][0]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][1] ==2 && x[1][1] ==2 && x[2][1] ==2 && x[3][1]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][2] ==2 && x[1][2] ==2 && x[2][2] ==2 && x[3][2]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][3] ==2 && x[1][3] ==2 && x[2][3] ==2 && x[3][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
}
}
if (check == 0)
{
cout<<" drow !!!"<<endl;
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==1)
{
cout<<" Player 1 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name1;
check_player_name(High_scores_level_2 , name1 , score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==2)
{
cout<<" Player 2 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name2;
check_player_name(High_scores_level_2 , name2 , score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
}
void game_level_1(int &check2)
{
string high_scores_level_1 = "high_scores_level_1";
string name1;
string name2;
int score1=0;
int score2=0;
int check =0;
int r , c,w,z;
cout<<" Player 1 will sign ' x ' while player 2 will sign ' o '"<<endl<<endl;
int x[3][3];
for (int j=0 ; j<3; j++)
{
cout<< setw(5)<<"l"<<setw(5)<<"l"<<setw(5)<<"l";
cout<<endl;
for (int i=0; i<3; i++)
cout<<setw(4)<<" ____";
cout<<endl;
}
for (z=0; z<9 ; z++)
{
cout<<" Player 1: enter first the number of the row then number of cloumb"<<endl;
cout<<" to mark your sign";
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=1;
if (x[0][0] ==1 && x[0][1] ==1 && x[0][2] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[1][0] ==1 && x[1][1] ==1 && x[1][2] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[2][0] ==1 && x[2][1] ==1 && x[2][2] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][1] ==1 && x[2][2] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][1] ==1 && x[2][0] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][0] ==1 && x[2][0] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][1] ==1 && x[1][1] ==1 && x[2][1] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][2] ==1 && x[2][2] == 1)
{
cout<<"player 1 wins"<<endl;
score1++;
check = 1;
break;
}
}
cout<< " Now player 2 enter the number of row then coloumb"<<endl;
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=2;
if (x[0][0] ==2 && x[0][1] ==2 && x[0][2] == 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[1][0] ==2 && x[1][1] ==2 && x[1][2] == 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[2][0] ==2 && x[2][1] ==2 && x[2][2] == 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][1] ==2 && x[2][2] == 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][2] ==2 && x[1][1] ==2 && x[2][0] == 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][0] ==2 && x[2][0] == 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][1] ==2 && x[1][1] ==2 && x[2][1] ==2)
{
cout<<"player 2 wins"<<endl;
score2++;
check = 2;
break;
}
else if (x[0][2] ==2 && x[1][2] ==2 && x[2][2] ==2)
{
cout<<"player 2 wins"<<endl;
score2++;
check = 2;
break;
}
}
}
if (check == 0)
{
cout<<" drow !!!"<<endl;
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==1)
{
cout<<" Player 1 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name1;
check_player_name(high_scores_level_1 ,name1 ,score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==2)
{
cout<<" Player 2 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name2;
check_player_name(high_scores_level_1 , name2 , score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
}
// The begining of the main
int main ()
{
string y;
int j;
ifstream in;
int check2;
int level;
for (int i=0 ; i<100 ; i++)
{
cout<<" Hello user "<<endl;
cout<<" Welcome in the X-O game"<<endl<<endl;
cout<<" Please choose the level you want (1 or 2 or 3)"<<endl;
cout<<" As level 1 equal board (3 X 3)"<<endl;
cout<<" While level 2 equal board (4 X 4)"<<endl;
cout<<" And level 3 equal board (5 X 5)" <<endl;
cin>>level;
switch (level)
{
case 1: {
game_level_1(check2);
if (check2 == 2)
{
in.open("High_scores_level_1");
while (!in.eof())
{
in>>y;
cout<<y<<endl;
}
in.close();
}
else if (check2 == 3)
system ("pause");
break;
}
}
system ("pause");
return 0;
}
}
This post has been edited by quaresma: 9 May, 2008 - 01:04 AM