If statements

I am not able to get my if statements to output the correct number. Here is the code I have so far

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
 #include <iostream>
    #include <sstream>
    #include <string>
    #include <ctime>
    #include <cmath>
    #include <math.h>
    #include <algorithm>

    using namespace std;

    void Welcome_Screen()
    {
		cout << "You are playing the text version of Mastermind." << endl << endl;
		cout << "The rules are to guess the 5 digit number before your number of guesses run out. " << endl << endl;
		cout << "The numbers run from 1 - 9 and there can be multiple numbers of the same number." << endl << endl;
		cout << "1 implies the right number in the right place." << endl;
		cout << "2 implies the right number but in the wrong place." << endl;
		cout << "0 implies wrong number." << endl << endl;
		cout << "Ready?? Here we go." << endl << endl;

		system("pause");
		system("cls");
    }

    string convert_int_to_string(int computer_number[5])
    {
		stringstream computer_numbers[5];
		computer_numbers[0] << computer_number[0];
		computer_numbers[1] << computer_number[1];
		computer_numbers[2] << computer_number[2];
		computer_numbers[3] << computer_number[3];
		computer_numbers[4] << computer_number[4];
		return computer_numbers[5].str();
    };

    string convert_player_guess_int_to_string(int player_guess[5])
    {
		stringstream player_numbers[5];
		player_numbers[0] << player_guess[0];
		player_numbers[1] << player_guess[1];
		player_numbers[2] << player_guess[2];
		player_numbers[3] << player_guess[3];
		player_numbers[4] << player_guess[4];
		return player_numbers[5].str();
    };

    int main()
    {
		Welcome_Screen();

		srand(static_cast<unsigned int>(time(0)));

		int computer_number[5], player_guess[5], guess_code[5];
		int random_number1 = rand() % 6 + 1;
		int random_number2 = rand() % 6 + 1;
		int random_number3 = rand() % 6 + 1;
		int random_number4 = rand() % 6 + 1;
		int random_number5 = rand() % 6 + 1;

		computer_number[0] = random_number1;
		computer_number[1] = random_number2;
		computer_number[2] = random_number3;
		computer_number[3] = random_number4;
		computer_number[4] = random_number5;

		int number_of_guesses = 12;

		cout << "The computer has made it's choices and set it's 5 numbers down. " << endl << endl;
		cout << "What are your number guesses? (press enter after each number)" << endl << endl;

		for(int a = 0; a < number_of_guesses; ++a)
		{

			cout << "Guess 1: ";
			cin >> player_guess[0];
			cout << endl;

			cout << "Guess 2: ";
			cin >> player_guess[1];
			cout << endl;

			cout << "Guess 3: ";
			cin >> player_guess[2];
			cout << endl;

			cout << "Guess 4: ";
			cin >> player_guess[3];
			cout << endl;

			cout << "Guess 5: ";
			cin >> player_guess[4];
			cout << endl << endl;

			//Checks first guess
			if( computer_number[0] == player_guess[0] )
			{
				guess_code[0] = 1;
			}

			else if(computer_number[0] != player_guess[0]|| player_guess[1] || player_guess[2] || player_guess[3] || player_guess[4])
			{
				guess_code[0] = 0;
			}

			else(computer_number[0] == player_guess[1] || player_guess[2] || player_guess[3] || player_guess[4]);
			{
				guess_code[0] = 2;
			}

			//Checks second guess
			if( computer_number[1] == player_guess[1] )
			{
				guess_code[1] = 1;
			}

			if(computer_number[1] != player_guess[0]|| player_guess[1] || player_guess[2] || player_guess[3] || player_guess[4])
			{
				guess_code[1] = 0;
			}

			if(computer_number[1] == player_guess[0] || player_guess[2] || player_guess[3] || player_guess[4])
			{
				guess_code[1] = 2;
			}

			//Checks third guess
			if( computer_number[2] == player_guess[2] )
			{
				guess_code[2] = 1;
			}

			if(computer_number[2] != player_guess[0]|| player_guess[1] || player_guess[2] || player_guess[3] || player_guess[4])
			{
				guess_code[2] = 0;
			}

			if(computer_number[2] == player_guess[0] || player_guess[1] || player_guess[3] || player_guess[4])
			{
				guess_code[2] = 2;
			}

			//Checks forth guess
			if( computer_number[3] == player_guess[3] )
			{
				guess_code[3] = 1;
			}

			if(computer_number[3] != player_guess[0]|| player_guess[1] || player_guess[2] || player_guess[3] || player_guess[4])
			{
				guess_code[3] = 0;
			}

			if(computer_number[3] == player_guess[0] || player_guess[1] || player_guess[2] || player_guess[4])
			{
				guess_code[3] = 2;
			}

			//Checks fifth guess
			if( computer_number[4] == player_guess[4] )
			{
				guess_code[4] = 1;
			}

			if(computer_number[4] != player_guess[0]|| player_guess[1] || player_guess[2] || player_guess[3] || player_guess[4])
			{
				guess_code[4] = 0;
			}

			if(computer_number[4] == player_guess[0] || player_guess[1] || player_guess[2] || player_guess[3])
			{
				guess_code[4] = 2;
			}

			cout <<"Here is the outcome of guess number "<< a <<":\n";

			for (unsigned int i=0; i < 5; i++)
			{
				cout << computer_number[i]<<"   ";
			}

			cout <<endl;


			for (unsigned int i=0; i < 5; i++)
			{
				cout << player_guess[i]<<"   ";
			}

			cout << endl;

			for (unsigned int i=0; i < 5; i++)
			{
				cout << guess_code[i] <<"   ";
			}

			cout <<endl;
			
			

		}

   
		system("pause");
		return 0;
    }
if(computer_number[2] == player_guess[0] || player_guess[1] || player_guess[3] || player_guess[4])

|| does not work like you think it does. For some reason this is a common mistake with beginners.

|| looks at both the left and ride side of it. If either side is nonzero (true), the result is nonzero (true). Otherwise the result is zero (false). The || operator is unaware that the left side of it is a == operation, and therefore does not give you multiple right-hand values for the == operator. It merely combines the output of the == operator with another expression.

EXAMPLE:

1
2
3
4
5
int foo = 5;
if(foo == 1 || 2)
{
  // this if statement will ALWAYS run
}


Notice the if block will run, even though foo does not equal 1 or 2. That's because the compiler interprets this as follows:

(foo == 1 || 2) // start with this

-) look at foo==1. foo contains 5, so this results in false.

(false || 2) // evaluates to this

-) look at '2'. This is nonzero, so it gets interpretted as 'true'

(false || true) // evaluates to this

-) The || operator compares what's on each side of it. Since the right side of it is true, it evaluates to true

(true) // finally evaluates to this

-) Since it gets evaluated to true, the if block executes.



What you probably wanted to do was this:

 
if(foo == 1 || foo == 2)


But I don't doing that because it makes you code get ugly and out of control really fast. A better solution would be to move some of these checks inside a loop... or inside separate functions.
Last edited on
So would this work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 //Checks first guess
if( computer_number[0] == player_guess[0] )
{
    guess_code[0] = 1;
}

for (int i = 0; i<5; i++)
{
    if(computer_number[0] == player_guess[i])
    {
        guess_code[0] = 0;
    }

    else  guess_code[0] = 2;
}
Close. The problem there is that player_guess[0] through [3] will be effectively ignored because player_guess[4] will always determine the final value of guess_code[0]. The else clause in the for loop is the culprit there.

Something like this might work:

1
2
3
4
5
6
7
8
9
10
guess_code[0] = 0; // by default, assume they did not guess correctly

if( computer_number[0] == player_guess[0] )
  guess_code[0] = 1;  // if they guessed correctly, change that to give them the '1' code

for(int i = 1; i < 5; ++i)  // note I start at 1, not zero
{
  if( computer_number[0] == player_guess[i] )  // if they guessed it on any non-first guess
    guess_code[0] = 2;  // revise to give them the '2' code
}


Of course, I would also throw all of this in a function so you don't have to copy/paste it a million times.
I haven't done a lot of work with functions but to make this a function I'm thinking I would have to do something like this

1
2
3
4
5
6
7
8
9
10
11
12
void Check_Guess()

guess_code[0] = 0; // by default, assume they did not guess correctly

if( computer_number[0] == player_guess[0] )
  guess_code[0] = 1;  // if they guessed correctly, change that to give them the '1' code

for(int i = 1; i < 5; ++i)  // note I start at 1, not zero
{
  if( computer_number[0] == player_guess[i] )  // if they guessed it on any non-first guess
    guess_code[0] = 2;  // revise to give them the '2' code
}


Then once I have this built, would I call it after every time the player makes a guess and what would I need to do so the array position, guess_code[0] change to guess_code[1]?
You only want to keep the logic in the function. The actual variables will be passed to/from the function. This makes it more reusable.

Here, you have 2 things that you want to input to the function:
1) The computer's number
2) The players guesses

And you have one thing you want to output:
1) The guess code.

Typically when there's only one output, you 'return' that value. So the function would look something like this:

1
2
3
4
int Check_Guess( int correct_answer, int player_guess[], int guess_count )
{
  //...
}


Here, you would check each entry in the 'player_guess' array (of which you know you have 'guess_count' entries) against the 'correct_answer'. Depending on if/when they guessed the number correctly, you will return the appropriate guess code.

Usage would be like this:

1
2
3
4
5
guess_code[0] = Check_Guess( computer_number[0], player_guess, 5 );

// or... if you want to loop it:
for(int i = 0; i < 5; ++i)
  guess_code[i] = Check_Guess( computer_number[i], player_guess, 5 );
I think I understand what you are trying to do here. I will play with it and see if I can make it work. The only question I do have is do I need a return statement somewhere?
If your function is going to be outputting data via it's return value, then yes you will need a return statement in the function.

Topic archived. No new replies allowed.