Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,244 C++ Programmers. There are 2,252 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

dunno where the problem is ?

 
Reply to this topicStart new topic

dunno where the problem is ?, its a project of X-O using c++

quaresma
post 9 May, 2008 - 12:59 AM
Post #1


New D.I.C Head

*
Joined: 5 May, 2008
Posts: 4



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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Cerolobo
post 9 May, 2008 - 01:11 AM
Post #2


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 151

After you prompt the user, you run this

CODE

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


You only have a case for Level 1. I did test it and it seems to work. Did you possibly try level 2 or 3?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Cerolobo
post 9 May, 2008 - 01:21 AM
Post #3


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 151

Sorry, I miss interpreted the question (the formatting DOES NOT HELP and tends to drive people away).

The reason why it is quiting, is because you are telling it to...

Here is your main(), with the switch stripped out

CODE
// 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 was here

system ("pause");
return 0;

} // <-- End of the for loop
}


You have return 0; at the end of the for loop. So, you are exiting your program, before you actually go through another loop.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

quaresma
post 9 May, 2008 - 02:14 AM
Post #4


New D.I.C Head

*
Joined: 5 May, 2008
Posts: 4

yea i changed but still the same problem exists

you know the program doesnt quit it just pause i mean after you write your name and click enter it pauses wich doesnt let you to write anything else

i think the problem not in the main maybe at the functions but dont know where

thnxxx 4 ur help
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

quaresma
post 10 May, 2008 - 04:16 PM
Post #5


New D.I.C Head

*
Joined: 5 May, 2008
Posts: 4

plz any body help me here
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/16/08 08:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month