Error in Display with If Statements inside While Loop

Hello,

I am writing code that mimics the game Mastermind. The computer randomly comes up with a code based on the "colors" RGBOY. The user has ten guesses to guess what the code is. Right now my code displays the secret code at the beginning just so I can test it out, but it will not display the secret code in the final until the end if the user has not guessed it.

The game gives feedback based on the user's guesses. For example, if the secret code is GBYO and the user enters GBOY, the feedback will be 1100. The first two colors are in the correct place and order, while the last two are the correct colors, but NOT in the correct order. I have written code that does this.

BUT for some reason, if the first code entered is not automatically the right one, the first time I loop through and encounter my displayFeedback function, it is missing a character in the feedback! But if I enter the SAME guess on the second guess, the feedback is correct!!! I am attaching an image so you can see what I mean. I am also posting my code. Does ANYONE have any idea why it would be doing this? The feedback works correctly from plays 2-10, but for some reason that first time through it does not! Any suggestions would be great.

http://i631.photobucket.com/albums/uu33/lupwned/mastermind.png < Print Screen

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
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

// Prototypes

void setSecretCode(char[]);
void displaySecretCode(char[]);
void removeColor(char[], string &, int);
void getGuess(char[][4], int, int);
void displayFeedback(char[][4], char [], int, int, char [][4]);
bool isItWin(char [][4], char [], int);

// Main
int main()
{
	// Define Variables
	char code[4];
	const int SIZE = 10;
	char guess[SIZE][4];
	char feedback[SIZE][4] = {0};
	bool winner;
	int i = 0;
	int j = 1;
	// Call Set Secret Code
	setSecretCode(code);
	displaySecretCode(code);
	// While less than 10 turns and not a winner
	while (i < 10 && winner == false)
		{
		// Call Guess Function
		getGuess(guess, i, j);
		// Call Display Function
		displayFeedback(guess, code, i, j, feedback);
		// Call Is It Win Function
		winner = isItWin(feedback, code, i);
			// If winning before the 10th turn
			if (winner == true)
			{
				cout << "Yay, you won!";
				cout << endl;
			}
			// If lost
			if (winner == false && i == 9)
			{
				cout << "Sorry, you lose";
				cout << endl;
				cout << "Code was: " ;
				displaySecretCode(code);
			}
		i++;
		j++;
		}
	system ("pause");
	return 0;
}

// Set Secret Code Function
void setSecretCode(char code[])
{
	// Declare Variables
	string colors = "RGBYO";
	int pos;
	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 5;
	code[0] = colors[pos]; 
	// Call Remove Color Function
	removeColor(code, colors, pos);

	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 4; 
	code[1] = colors[pos];
	// Call Remove Color Function
	removeColor(code, colors, pos);

	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 3; 
	code[2] = colors[pos]; 
	// Call Remove Color Function
	removeColor(code, colors, pos);

	// Randomly Grab
	srand(time(NULL));
	pos = rand() % 2; 
	code[3] = colors[pos];
	// Call Remove Color Function
	removeColor(code, colors, pos);
}

// Remove Color Function
void removeColor(char code[], string & colors, int pos)
{
	// Declare Variables
	string temp;
	int j = 0;

	// Create New Color String
	while (j<colors.length())
	{
		if (j!=pos)
		{
			temp = temp + colors[j];
		}
		j++;
	}
	colors = temp;
}

// Get Guess from User Function
void getGuess(char guess[][4], int i, int j)
{	
	cout << endl;
	cout << "Guess: ";
	cin >> guess[i][0] >> guess[i][1] >> guess[i][2] >> guess[i][3];
}

// Display Feedback Function
void displayFeedback(char guess[][4], char code[], int i, int j, char feedback[][4])
{
	// Display User's Guess
	cout << j << ". "<< "[" << " " << guess[i][0] << " " << guess[i][1] << " " << guess[i][2] << " " << guess[i][3] << "]";
	cout << endl;

	// Display Feedback if in wrong place
	if (guess[i][0] == code[1] || guess[i][0] == code[2] || guess[i][0] == code[3])
	{
		feedback[i][0] = '0';
		cout << "     " << feedback[i][0];
	}

	
	if (guess[i][1] == code[0] || guess[i][1] == code[2] || guess[i][1] == code[3])
	{
		feedback[i][1] = '0';
		cout << " " << feedback[i][1];
	}

	if (guess[i][2] == code[0] || guess[i][2] == code[1] || guess[i][2] == code[3])
	{
		feedback[i][2] = '0';
		cout << " " << feedback[i][2];
	}

	if (guess[i][3] == code[0] || guess[i][3] == code[1] || guess[i][3] == code[2])
	{
		feedback[i][3] = '0';
		cout << " " << feedback[1][3];
	}

	// Display Feedback if in Right Place
	if (guess[i][0] == code[0])
	{
		feedback[i][0] = '1';
		cout << "     " << feedback[i][0];
	}

	if (guess[i][1] == code[1])
	{
		feedback[i][1] = '1';
		cout << " " << feedback[i][1];
	}

	if (guess[i][2] == code[2])
	{
		feedback[i][2] = '1';
		cout << " " << feedback[i][2];
	}

	if (guess[i][3] == code[3])
	{
		feedback[i][3] = '1';
		cout << " " << feedback[i][3];
	}
	
	cout << endl;
}

// Display the Secret Code
void displaySecretCode(char code[])
{
	cout << code[0] << code[1] << code[2] << code[3];
	cout << endl;
}

// Determine if it is a Win
bool isItWin(char feedback[][4], char code[], int i)
{
	bool win;
	if (feedback[i][0] == '1' && feedback[i][1] == '1' && feedback[i][2] == '1' && feedback[i][3] == '1')
	{
		win = true;
	}
	else
	{
		win = false;
	}
	return win;
}
Typo in line 152. You have 1 instead of i.

cout << " " << feedback[1][3];

Also you should check right place after each wrong place check.

1
2
3
4
5
6
7
8
9
10
11
12
13
	// Display Feedback if in wrong place
	if (guess[i][0] == code[1] || guess[i][0] == code[2] || guess[i][0] == code[3])
	{
		feedback[i][0] = '0';
		cout << "     " << feedback[i][0];
	}

	// Display Feedback if in Right Place
	if (guess[i][0] == code[0])
	{
		feedback[i][0] = '1';
		cout << "     " << feedback[i][0];
	}
Topic archived. No new replies allowed.