how to call a boolean function into main

I don't know how to call the Boolean function back into main. I'm trying to make a tic tac toe board. The program works but doesn't stop when someone wins because i need this function.

# include <iostream>
using namespace std;
void Printarray(char array1[]);
bool check (char board[][3], bool &win);
int main ()
{

char array2 [9];

char player1, player2;
int num, turn=0;
bool check = false, win = false, winner;


array2[0] = '1';
array2[1] = '2';
array2[2] = '3';
array2[3] = '4';
array2[4] = '5';
array2[5] = '6';
array2[6] = '7';
array2[7] = '8';
array2[8] = '9';


while (check == false)
{
cout << endl;


if (turn % 2 == 0){
cout << "Enter the number that corresponds to the spot you wish to pick player 1 ( X )" << '\n'<< '\n';
Printarray(array2);
cout << '\n'<< '\n';
cin >> num;
array2 [num -1] = 'X';
Printarray(array2);
cout << '\n'<< '\n';
}
if (turn % 2 != 0){
cout << "Enter the number that corresponds to the spot you wish to pick player 2 ( O )" << '\n'<< '\n';
Printarray(array2);
cout << '\n'<< '\n';
cin >> num;
array2 [num -1] = 'O';
Printarray(array2);
cout << '\n'<< '\n';
}

turn++;

}


return 0;
}


bool check (char check[], bool& win) {

if ((check[0] == 'X') && (check[3] == 'X') && (check[6] == 'X')){
return true;}
if ((check[1] == 'X') && (check[4] == 'X') && (check[7] == 'X')){
return true;}
if ((check[2] == 'X') && (check[5] == 'X') && (check[8] == 'X')){
return true;}
if ((check[0] == 'X') && (check[1] == 'X') && (check[2] == 'X')){
return true;}
if ((check[3] == 'X') && (check[4] == 'X') && (check[5] == 'X')){
return true;}
if ((check[6] == 'X') && (check[7] == 'X') && (check[8] == 'X')){
return true;}
if ((check[0] == 'X') && (check[4] == 'X') && (check[8] == 'X')){
return true;}
if ((check[2] == 'X') && (check[4] == 'X') && (check[6] == 'X')){
return true;}

else
return false;





if ((check[0] == 'O') && (check[3] == 'O') && (check[6] == 'O')){
return true;}
if ((check[1] == 'O') && (check[4] == 'O') && (check[7] == 'O')){
return true;}
if ((check[2] == 'O') && (check[5] == 'O') && (check[8] == 'O')){
return true;}
if ((check[0] == 'O') && (check[1] == 'O') && (check[2] == 'O')){
return true;}
if ((check[3] == 'O') && (check[4] == 'O') && (check[5] == 'O')){
return true;}
if ((check[6] == 'O') && (check[7] == 'O') && (check[8] == 'O')){
(win == true);}
if ((check[0] == 'O') && (check[4] == 'O') && (check[8] == 'O')){
return true;}
if ((check[2] == 'O') && (check[4] == 'O') && (check[6] == 'O')){
return true;}
else
return false;

}

void Printarray(char array1[]){


for (int j=0; j<=8; j++){
cout<<array1[j]<<" ";
if (j ==2 || j == 5)
cout<<endl;
}

}
Ok, first off, please indent your code. It makes it a lot easier to read and pilfer through. I don't know why you did some of the things you did, but I corrected what needed to be corrected and the comments explain what and why. If you have any questions, ask. Also, if you're posting code please use the code format.

(EDIT) If you really want to pass the variables to be changed to a function, try passing them by reference (&) like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <stdlib.h>

using namespace std;

void somefunc(int *pointer);

int main() {
   int vartochange = 15;
   somefunc(&vartochange);//this passes the address of vartochange
   cout << "The new value is: " << vartochange << endl;
   system("pause");
   return 0;
}

void somefunc(int *pointer) {
   *pointer+=5;
}


The output would be:


The new value is: 20
press any key to continue. . .


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# include <iostream>
# include <stdlib.h> //This is for the "system()" command

 using namespace std;
 
 void Printarray();
 
 bool check(); // your function here didn't match the one below and it doesn't need parameters
 
 char array[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};// why not make your array global?
 int  iplayer = 1; //to keep track of what player is playing, 1=player 1, and 2=player 2 

 int main ()
 {
 
 char player1, player2; // what are you using these for?
 int num, turn=0;
 //bool win = false, winner; these are unnecessary
 char* winner;

 


while (check() == false) //This should just be the function to simplify
 {
 cout << endl;


 if (turn % 2 == 0){
 iplayer = 1;
 cout << "Enter the number that corresponds to the spot you wish to pick player 1 ( X )" << '\n'<< '\n';
 Printarray(); 
 cout << '\n'<< '\n';
 cin >> num; 
 array[num -1] = 'X';
 Printarray();
 cout << '\n'<< '\n';
 }else {
 iplayer = 2;
 cout << "Enter the number that corresponds to the spot you wish to pick player 2 ( O )" << '\n'<< '\n';
 Printarray(); 
 cout << '\n'<< '\n';
 cin >> num; 
 array[num -1] = 'O';
 Printarray();
 cout << '\n'<< '\n';
 }

 turn++;

 }
 if(iplayer == 1) winner = "Player 1";
 else winner = "Player 2";
 cout << "The winner is: " << winner << "!!!" << endl;
 system("pause");
 

return 0;
 }
 

bool check() { //the variable 'win' is in main's scope, not here.

//This initailly didn't even check Os because it returned false from the Xs!
//And indentations make your code MUCH EASIER to read.
   if(iplayer == 1) {// now it checks which player to check
       if ((array[0] == 'X') && (array[3] == 'X') && (array[6] == 'X')){
       return true;}
       if ((array[1] == 'X') && (array[4] == 'X') && (array[7] == 'X')){
       return true;}
       if ((array[2] == 'X') && (array[5] == 'X') && (array[8] == 'X')){
       return true;}
       if ((array[0] == 'X') && (array[1] == 'X') && (array[2] == 'X')){
       return true;}
       if ((array[3] == 'X') && (array[4] == 'X') && (array[5] == 'X')){
       return true;}
       if ((array[6] == 'X') && (array[7] == 'X') && (array[8] == 'X')){
       return true;}
       if ((array[0] == 'X') && (array[4] == 'X') && (array[8] == 'X')){
       return true;}
       if ((array[2] == 'X') && (array[4] == 'X') && (array[6] == 'X')){
       return true;}
      
       else return false;
   }else {
       if ((array[0] == 'O') && (array[3] == 'O') && (array[6] == 'O')){
       return true;}
       if ((array[1] == 'O') && (array[4] == 'O') && (array[7] == 'O')){
       return true;}
       if ((array[2] == 'O') && (array[5] == 'O') && (array[8] == 'O')){
       return true;}
       if ((array[0] == 'O') && (array[1] == 'O') && (array[2] == 'O')){
       return true;}
       if ((array[3] == 'O') && (array[4] == 'O') && (array[5] == 'O')){
       return true;}
       if ((array[6] == 'O') && (array[7] == 'O') && (array[8] == 'O')){
       return true;}
       if ((array[0] == 'O') && (array[4] == 'O') && (array[8] == 'O')){
       return true;}
       if ((array[2] == 'O') && (array[4] == 'O') && (array[6] == 'O')){
       return true;}
       else return false;
   }
 
}

 void Printarray(){ // you don't need to pass a parameter to this when the variable being checked is global
 
 for (int j=0; j<=8; j++){
 cout<<array[j]<<" ";
 if (j ==2 || j == 5) {
 cout<<endl;
 }

 } //You didn't have enough curly brackets in here, indent your code to make it easier to read!
 }
Last edited on
drognisep, I have essentially the same problem to code, but I did it in a different way. here is my thread http://www.cplusplus.com/forum/beginner/84101/. Could you help me with it?

mjr5253, were you told to use bool data types for some of your functions or is that what you thought was the best method? I'm doing the same problem, but I'm not sure if I'm taking the best approach.
mjr5253, were you able to find your problem?
Last edited on
Topic archived. No new replies allowed.