How to pass a char array in a function(s)?

Hello All, I need help with function paramenters. How to I get my board to update after a user enters a move? I've narrowed it down to passing the board through functions, but I'm unsure of how to set up the parameters correctly. I left comments where I feel I need help. I use XCode 6.1 to code.
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
///Tic-Tac-Toe

#include <iostream>
using namespace std;
///DECLARATION////
void printBoard();//prints the board.
void pturn();//player turn detection.
bool endgame();//supposed to detect winning configurations. Need help with draw configuration.
void resetBoard();//if to be played again, this function clears the board.

////MAIN///
int main(int argc, const char * argv[])
{
    int choice, again = 0; //choice is for move selection, again is to replay game.
    int winA, winB, loseA, loseB, draw = 0;//Scoreboard counters: A is to increment scores for p1, B is to increment score for p2.
    char turn;//determines whether it is turn 'x' or 'o'.
    string p1, p2;//stores players names.
    bool drawMove = true;//supposed to determine during each move if there is a draw.
    
    cout << "Tic-Tac-Toe Game V1" << endl;
    cout << "Enter Player 1's name:\n";//player 1 name
    getline(cin, p1);
    p1[0] = toupper(p1[0]);
    cout << p1 << " you are 'X'. \n";
    
    cout << "Enter Player 2's name:\n";//player 2 name
    getline(cin, p2);
    p2[0] = toupper(p2[0]);
    cout << p2 << " you are 'O'\n";
    cout << endl;
    
    do{
        cout << "Who will go first?\n";//players choose turn pattern.
        cout << "Press 1 for " << p1 << endl;
        cout << "Press 2 for " << p2 << endl;
        cin >> choice;
        cout << endl;
        
        if (choice < 1 || choice > 2)//checks to make sure turn choice is 1 or 2.
        {
            cin.clear();
            cin.ignore();
            cout << "Please use 1 or 2 to choose who goes first." <<endl;
            cout << "Press 1 for " << p1 << endl;
            cout << "Press 2 for " << p2 << endl;
            cin >> choice;
        }
        
        if(choice == 1)
        {
            turn = 'x';
        }
        
        else if (choice == 2)
        {
            turn = 'o';
        }
        
        
        while (!endgame())//while there is no winning configuration, continue the game
        {
            cout << "Tic Tac Toe Game\n";
            cout << "Player 1[X] --- Player 2[O]\n";
            cout << p1 << "[X] --- " << p2 << "[O]\n";
            printBoard();
            pturn();
            endgame();
        }
        
        if (turn == 'o' && !drawMove)
        {
            printBoard();
            cout << endl << endl << "Player 1 [X] Wins! Game Over!\n";
            winA++;
            loseB++;
        }
        else if (turn == 'x' && !drawMove)
        {
            printBoard();
            cout << endl << endl << "Player 2 [O] Wins! Game Over!\n";
            winB++;
            loseA++;
        }
        else if (turn == 'o' && drawMove)
        {
            printBoard();
            cout << endl << endl << "It's a draw! Game Over!\n";
            draw++;
        }
        else if (turn == 'x' && drawMove)
        {
            printBoard();
            cout << endl << endl << "It's a draw! Game Over!\n";
            draw++;
        }
        //scoreboard is supposed to print with updated score after each game.
        cout << "-----------------------------------------------------------" << endl;
        cout << "|              Tic X Tac O Toe Scoreboard                 |" << endl;
        cout << "-----------------------------------------------------------" << endl;
        cout << "|" << "Player 1: " << p1 << "  |Wins:  " << winA << "   |Losses:  " << loseA <<  "     |Draws: " << draw << "    |" << endl;
        cout << "-----------------------------------------------------------" << endl;
        cout << "|" << "Player 2: " << p2 << "  |Wins:  " << winB << "   |Losses:  " << loseB <<  "     |Draws: " << draw << "    |" << endl;
        cout << "-----------------------------------------------------------" << endl;
        
        cout << "Play again? 1 = Yes | 2 = No \n";
        cin >> again;
        if(again <= 1 || again >= 2)//checks to make sure user choice is 1 or 2.
        {
            cin.clear();
            cin.ignore();
            cout << "Please enter 1 = Yes to continue the game or 2 = No to end the game." << endl;
            cin >> again;
        }
        cout << endl;
        
        if(again == 1)
        {
            resetBoard();//clears board if the user chooses to play again
        }
        
    }while(again != 2);//exits game
    
    system("pause");
    return 0;

}
////FUNCTIONS////
void printBoard()
{
    char board[9] = {'1', '2', '3',
                     '4', '5', '6',
                     '7', '8', '9'};
    cout << "-------------------" << endl << endl;
    
    cout << "-------------------" << endl;
    cout << "|"<< "     |     |     " << "|" << endl;
    cout << "|"<< "  " << board[0] << "  |  " << board[1] << "  |  " << board[2] << "  |" << endl;
    cout << "|"<< "_____|_____|_____" << "|" << endl;
    cout << "|"<< "     |     |     " << "|" << endl;
    cout << "|"<< "  " << board[3] << "  |  " << board[4] << "  |  " << board[5] << "  |" << endl;
    cout << "|"<< "_____|_____|_____" << "|" << endl;
    cout << "|"<< "     |     |     " << "|" << endl;
    cout << "|"<< "  " << board[5] << "  |  " << board[6] << "  |  " << board[8] << "  |" << endl;
    cout << "|"<< "     |     |     " << "|" << endl;
    cout << "-------------------" << endl;
    cout << endl;
}

void pturn()
{
    char board[9] = {'1', '2', '3',
                     '4', '5', '6',
                     '7', '8', '9'};

    char turn;
    int choice;
    int place = 0;
    //when the game begins, this does not cout
    if (turn == 'x')
    {
        cout << "Player 1 turn [X]: ";
    }
    else if (turn == 'o')
    {
        cout << "Player 2 turn [O]: ";
    }
    cin >> choice;
    
    switch (choice)
    {
        case 1: place = 0; break;
        case 2: place = 1; break;
        case 3: place = 2; break;
        case 4: place = 3; break;
        case 5: place = 4; break;
        case 6: place = 5; break;
        case 7: place = 6; break;
        case 8: place = 7; break;
        case 9: place = 8; break;
        default:
            while(cin.fail())
            {
                cout << "Invalid Entry\nEnter 1-9:" << endl;
                cin.clear();
                cin.ignore();
                pturn();
            }
            cout << "Invalid Entry\nEnter 1-9:";
            pturn();
    }
    
    //this block of code goes straight to the last else statement and says that the cell is used when no 'x' or 'o' has taken its place.
    
    if (turn == 'x' && board[place] != 'x' && board[place] != 'o')
    {
        board[place] = 'x';
        turn = 'o';
    }
    else if (turn == 'o' && board[place] != 'x' && board[place] != 'o')
    {
        board[place] = 'o';
        turn = 'x';
    }
    else
    {
        cout << "The cell you chose is used! Try again\n";
        pturn();
    }
}

bool endgame()
{
    char board[9] = {'1', '2', '3',
                     '4', '5', '6',
                     '7', '8', '9'};
    
    for (int i = 0; i < 3; i++)//Check for a win
    {
        if ((board[0] == board[1] && board[1] == board[2]) ||
            (board[0] == board[3] && board[3] == board[6]) ||
            (board[0] == board[4] && board[4] == board[8]) ||
            (board[2] == board[4] && board[4] == board[6]))
        {
            return true;
        }
    }
    
    for (int i = 0; i < 3; i++)//Check for draw
    {
        for (int j = 0; j < 3; j++)
        {
            if (board[i] != 'x' && board[j] != 'o')
            {
                return false;
            }
        }
    }
    
    return true;
}

void resetBoard()
{
    char board[9] = {'1', '2', '3',
                     '4', '5', '6',
                     '7', '8', '9'};
    board[0] = '1';
    board[1] = '2';
    board[2] = '3';
    board[3] = '4';
    board[4] = '5';
    board[5] = '6';
    board[6] = '7';
    board[7] = '8';
    board[8] = '9';
}
Also, char turn; comes up as warning as not being initialized...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

char* func()
{
	char c[] = "Hello World!";
	char *pc = c;
	return pc;
}

int main()
{
	char *ch;
	ch = func();	
	cout << ch;
	
return 0;
}


EDIT: oops. it was incautious(see below comment).
give *pc dynamically allocated memory and delete it in main.
Last edited on
@anup30: That is a terrible example. The array c is a local automatic variable of function func that ceases to exist the moment the scope of func ends. Handing out the address of already deallocated (stack memory block) is an error.


Function pturn() has local variable:
1
2
char turn; // uninitialized (155)
if (turn == 'x') // the warning should mention the line number (159) of this 


Passing an array (you could read the Tutorial on this site too):
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
#include <iostream>

void foo( char arr[] )
{
  // assume that arr has at least 5 elements
  arr[4] = 'X'; // change one
}

void bar( const char brr[], size_t N ) // will not change brr
{
  // crude, ugly, non-generic, but hopefully gives ideas
  for ( size_t x = 0; x < N; ++x ) {
    if ( 0 == x % 3 ) {
      std::cout << '\n';
    }
    std::cout << brr[ x ] << ' ';
  }
  std::cout << '\n';
}

int main()
{
  // The initializer list forces the board to have 9 elements
  char board[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  foo( board );
  bar( board, 9 );
  return 0;
}
Topic archived. No new replies allowed.