String array keeps resetting

When debugging, I found out that once setLetter() (class Game) sets the value to letter[x] array, it gets reset once it's out of the function checkPlay(). So showLetter() prints nothing every time. I cannot figure out why the array keeps resetting to " ". Any help would be great!

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
/************
* Game.h    *
************/
#ifndef GAME_H
#define GAME_H

#include <iostream>
#include <ctime>
#include <string>
#include <sstream>

class Game
{
protected:
	std::string letter[9];

public:
	Game() 
	{
		for(unsigned int i = 0; i < 9; i++)
		{
			letter[i] = " ";
		}
	}
	~Game() {}

	std::string showLetter(int x) const { return letter[x]; }
	void setLetter(int x, std::string i) { letter[x] = i; }
};

#endif

/************
* Player.h  *
************/
#ifndef PLAYER_H
#define PLAYER_H

#include "Game.h"

class Player : public Game
{
private:
	bool reset;

public:
	std::string convertInt(int);
	void ranLet(Game);

	void checkPlay(int, std::string, Game);
};

#endif

/***************
* Player.cpp   *
***************/
#include "Player.h"

std::string Player::convertInt(int number)
{
	std::stringstream ss;
	ss << number;

	return ss.str();
}

void Player::ranLet(Game board)
{
	int number;
	char letter;
	std::string choice;

	this->reset = false;

	while(!this->reset)
	{
		number = rand() % 3;
		letter = 'A' + static_cast<char>(number);
		choice = letter + convertInt(1 + rand() % 3);

		checkPlay(2, choice, board);
	}
}

void Player::checkPlay(int player, std::string choice, Game board)   
{
	switch(choice[0])
	{
	case 'A':
		if(choice[1] == '1' && letter[0] == " ")
		{
			if(player == 1)
				board.setLetter(0, "**");
			else 
				board.setLetter(0, "^^");

			this->reset = true;
			break;
		}
        //Left out rest of the switch statement as it's not important.   
	}
}

/*************
* Main.cpp   *
*************/
#include "Player.h"

void drawBoard(Game);

int main()
{
	std::string playerChoice;
	srand(static_cast<unsigned int>(time(NULL)));

	Game board;
	Player pOne, pTwo;

	std::cin >> playerChoice;

	pOne.checkPlay(1, playerChoice, board);
	drawBoard(board);

	pTwo.ranLet(board);
	drawBoard(board);

	return 0;
}

void drawBoard(Game board)
{
	for(unsigned int i = 0; i < 9; i++)
	{
		std::cout << board.showLetter(i) << " ";
	}
}
You are passing the object 'board' by value and not by reference. Passing by value means it creates a separate copy of that data type for the function parameter and all of that data is lost once it leaves the function. Passing by reference will use the address of the original object so it keeps all of the changes.

You will want to change your function parameters to
void Player::checkPlay(int player, std::string choice, Game &board)

I also suggest looking up Passing by Value vs Passing by Reference to clarify things.
Last edited on
Thank you so much! I'll look it up to get a better understanding.
closed account (Dy7SLyTq)
i would also make the string pass by reference because since your not editing it it will save memory (no point in copying an object of a class if you dont have to)
Sorry, didn't see your reply. I pretty much did exactly what you said after reading up on Passing by Value and Passing by Reference.

Thanks anyways!
Topic archived. No new replies allowed.