How to fix the displaying a board in a game

I am creating a board with 'X' and 'O' which prints 'X' when the number is even and prints 'O' when the number is odd. But when I display the board, I am getting some random characters.


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
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <iomanip>

using namespace std;

//declaring the global variables that holds 'X' and 'O'
char p1,  p2,  p3,  p4,  p5,  p6,
     p7,  p8,  p9,  p10, p11, p12,
     p13, p14, p15, p16, p17, p18,
     p19, p20, p21, p22, p23, p24,
     p25, p26, p27, p28, p29, p30,
     p31, p32, p33, p34, p35, p36;

//this function generates the random values and sets "X" or "O" on the board
void random()
{
	srand(time(NULL));
	p1 = rand() % 100; p2 = rand() % 100; p3 = rand() % 100; p4 = rand() % 100; p5 = rand() % 100; p6 = rand() % 100;
	p7 = rand() % 100; p8 = rand() % 100; p9 = rand() % 100; p10 = rand() % 100; p11 = rand() % 100; p12 = rand() % 100;
	p13 = rand() % 100; p14 = rand() % 100; p15 = rand() % 100; p16 = rand() % 100; p17 = rand() % 100; p18 = rand() % 100;
	p19 = rand() % 100; p20 = rand() % 100; p21 = rand() % 100; p22 = rand() % 100; p23 = rand() % 100; p24 = rand() % 100;
	p25 = rand() % 100; p26 = rand() % 100; p27 = rand() % 100; p28 = rand() % 100; p29 = rand() % 100; p30 = rand() % 100;
	p31 = rand() % 100; p32 = rand() % 100; p33 = rand() % 100; p34 = rand() % 100; p35 = rand() % 100; p36 = rand() % 100;
	/*
	p1 = rand() ; p2 = rand() ; p3 = rand(); p4 = rand() ; p5 = rand(); p6 = rand() ;
	p7 = rand() ; p8 = rand() ; p9 = rand(); p10 = rand() ; p11 = rand(); p12 = rand();
	p13 = rand(); p14 = rand(); p15 = rand(); p16 = rand(); p17 = rand(); p18 = rand();
	p19 = rand(); p20 = rand(); p21 = rand(); p22 = rand(); p23 = rand(); p24 = rand();
	p25 = rand(); p26 = rand(); p27 = rand(); p28 = rand(); p29 = rand(); p30 = rand();
	p31 = rand(); p32 = rand(); p33 = rand(); p34 = rand(); p35 = rand(); p36 = rand();*/

	if (p1 % 2 == 0)  { p1 == 'X'; }  	else { p1 = 'O'; }
	if (p2 % 2 == 0)  { p2 == 'X'; }	else { p2 = 'O'; }
	if (p3 % 2 == 0)  { p3 == 'X'; }	else { p3 = 'O'; }
	if (p4 % 2 == 0)  { p4 == 'X'; }	else { p4 = 'O'; }
	if (p5 % 2 == 0)  { p5 == 'X'; }	else { p5 = 'O'; }
	if (p6 % 2 == 0)  { p6 == 'X'; }	else { p6 = 'O'; }
	if (p7 % 2 == 0)  { p7 == 'X'; }	else { p7 = 'O'; }
	if (p8 % 2 == 0)  { p8 == 'X'; }	else { p8 = 'O'; }
	if (p9 % 2 == 0)  { p9 == 'X'; }	else { p9 = 'O'; }
	if (p10 % 2 == 0) { p10 == 'X'; }	else { p10 = 'O'; }
	if (p11 % 2 == 0) { p11 == 'X'; }	else { p11 = 'O'; }
	if (p12 % 2 == 0) { p12 == 'X'; }	else { p12 = 'O'; }
	if (p13 % 2 == 0) { p13 == 'X'; }	else { p13 = 'O'; }
	if (p14 % 2 == 0) { p14 == 'X'; }	else { p14 = 'O'; }
	if (p15 % 2 == 0) { p15 == 'X'; }	else { p15 = 'O'; }
	if (p16 % 2 == 0) { p16 == 'X'; }	else { p16 = 'O'; }
	if (p17 % 2 == 0) { p17 == 'X'; }	else { p17 = 'O'; }
	if (p18 % 2 == 0) { p18 == 'X'; }	else { p18 = 'O'; }
	if (p19 % 2 == 0) { p19 == 'X'; }	else { p19 = 'O'; }
	if (p20 % 2 == 0) { p20 == 'X'; }	else { p20 = 'O'; }
	if (p21 % 2 == 0) { p21 == 'X'; }	else { p21 = 'O'; }
	if (p22 % 2 == 0) { p22 == 'X'; }	else { p22 = 'O'; }
	if (p23 % 2 == 0) { p23 == 'X'; }	else { p23 = 'O'; }
	if (p24 % 2 == 0) { p24 == 'X'; }	else { p24 = 'O'; }
	if (p25 % 2 == 0) { p25 == 'X'; }	else { p25 = 'O'; }
	if (p26 % 2 == 0) { p26 == 'X'; }	else { p26 = 'O'; }
	if (p27 % 2 == 0) { p27 == 'X'; }	else { p27 = 'O'; }
	if (p28 % 2 == 0) { p28 == 'X'; }	else { p28 = 'O'; }
	if (p29 % 2 == 0) { p29 == 'X'; }	else { p29 = 'O'; }
	if (p30 % 2 == 0) { p30 == 'X'; }	else { p30 = 'O'; }
	if (p31 % 2 == 0) { p31 == 'X'; }	else { p31 = 'O'; }
	if (p32 % 2 == 0) { p32 == 'X'; }	else { p32 = 'O'; }
	if (p33 % 2 == 0) { p33 == 'X'; }   else { p33 = 'O'; }
	if (p34 % 2 == 0) { p34 == 'X'; }	else { p34 = 'O'; }
	if (p35 % 2 == 0) { p35 == 'X'; }	else { p35 = 'O'; }
	if (p36 % 2 == 0) { p36 == 'X'; }	else { p36 = 'O'; }
}

//displaying the board
void display()
{
	cout << setw(3) << p1  << setw(3) << p2  << setw(3) << p3  << setw(3) << p4  << setw(3) << p5  << setw(3) << p6  << endl;
	cout << setw(3) << p7  << setw(3) << p8  << setw(3) << p9  << setw(3) << p10 << setw(3) << p11 << setw(3) << p12 << endl;
	cout << setw(3) << p13 << setw(3) << p14 << setw(3) << p15 << setw(3) << p16 << setw(3) << p17 << setw(3) << p18 << endl;
	cout << setw(3) << p19 << setw(3) << p20 << setw(3) << p21 << setw(3) << p22 << setw(3) << p23 << setw(3) << p24 << endl;
	cout << setw(3) << p25 << setw(3) << p26 << setw(3) << p27 << setw(3) << p28 << setw(3) << p29 << setw(3) << p30 << endl;
	cout << setw(3) << p31 << setw(3) << p32 << setw(3) << p33 << setw(3) << p34 << setw(3) << p35 << setw(3) << p36 << endl;

}


int main()
{
	srand(time(NULL));
	random();
	display();
	system("pause");
	return 0;
}[\code][code]
Last edited on
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
#include <iostream>
#include <cstdlib>
#include <ctime>

// arrays: http://www.cplusplus.com/doc/tutorial/arrays/
const int N = 6 ;
// type alias: http://www.cplusplus.com/doc/tutorial/other_data_types/
using board = char[N][N] ; // another name for type NxN array of char

// generate a random 'X' or 'O'
char rand_X_or_O() { return std::rand() % 100 < 50 ? 'X' : 'O' ; }

// fill board with random 'X' or 'O'
void fill( board& brd )
{
    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( auto& row : brd ) // for each row in the board
        for( char& c : row ) c = rand_X_or_O() ; // set each char to 'X' or 'O'
}

// print the board
void print( const board& brd )
{
    for( const auto& row : brd ) // for each row in the board
    {
        for( char c : row ) std::cout << c << ' ' ; // print each char in the row
        std::cout << '\n' ; // and then a new line at the end of the row
    }
}

int main()
{
    std::srand( std::time(nullptr) ) ; // seed the legacy rng, once at the start of the program
    
    board my_board ;

    for( int i = 0 ; i < 5 ; ++i ) // print 5 generated layouts of the board
    {
        fill(my_board) ;
        print(my_board) ;
        std::cout << "\n\n" ;
    }
}

http://coliru.stacked-crooked.com/a/4728879fd514a395
@JL Borges, Thanks for the reply, Can you please tell where my mistake was.
Look at line 34 carefully: if (p1 % 2 == 0) { p1 == 'X'; } else { p1 = 'O'; }
There is a typo: p1 == 'X'; should have been p1 = 'X';

Likewise for all lines from 34 to 69.

With warnings enabled, my compiler (clang++) tells me:
warning: equality comparison result unused [-Wunused-comparison]
        if (p25 % 2 == 0) { p25 == 'X'; }       else { p25 = 'O'; }
                            ~~~~^~~~~~
note: use '=' to turn this equality comparison into an assignment
        if (p25 % 2 == 0) { p25 == 'X'; }       else { p25 = 'O'; }
                                ^~
                                =


Topic archived. No new replies allowed.