### If statements

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205`` `````` #include #include #include #include #include #include #include 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(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 <
`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:

 ``12345`` ``````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
 ``123456789101112131415`` `````` //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:

 ``12345678910`` ``````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

 ``123456789101112`` ``````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:

 ``1234`` ``````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:

 ``12345`` ``````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.