Need help with multiple conditions in if else if statements

This programming assignment is to create a guessing game where the user guesses two integers. I have a few bugs to work out but right now I am having one hell of a time with my if else if statement. It has to test the conditions and output the 6 different results. I cannot get all of the statements to work. Using visual studio and inserting a breakpoint, I have found that it will not output "both guesses are too high" or "both guesses are too low". I know that is a confusing question. But how do I get all if else if's to work properly? Also, if there is a more elegant way to layout these conditions, please do advise.

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
  #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int random1;
int random2;


int main() {
	int guess1;
	int guess2;
	char replay;
	cout << " Welcome to The Guessing Game! " << endl;
	cout << "Guess Two Numbers Between 1 and 100. " << endl;

	while (true) {
		int random1 = rand() % 100 + 1;
		srand(time(NULL));
		int random2 = rand() % 100 + 1;
		srand(time(NULL));
		int tries = 0;

		while (true) {
			cout << "Guess #1: " << endl;
			cin >> guess1;
			cout << "Guess #2: " << endl;
			cin >> guess2;

				if (guess1 == random1 || guess1 == random2 && guess2 == random1 || guess2 == random2) {
				cout << " Congratulations! You have guessed both numbers correctly!" << endl;
				}
				else if (guess1 == random1 || guess1 == random2 || guess2 == random1 || guess2 == random2 && guess1 < random1 || guess1 < random2 || guess2 < random1 || guess2 < random2) {
					cout << "One of your guesses is correct but the other is too low. " << endl;
				}
				else if (guess1 == random1 || guess1 == random2 || guess2 == random1 || guess2 == random2 && guess1 > random1 || guess1 > random2 || guess2 > random1 || guess2 > random2) {
					cout << "One of your guesses is correct but the other is too high. " << endl;
				}
				else if (guess1 > random1 || guess1 > random2 || guess2 > random1 || guess2 > random2 && guess1 > random1 || guess1 > random2 || guess2 > random1 || guess2 > random2) {
					cout << " Sorry, both of your guesses is too high. " << endl;
				}
				else if (guess1 < random1 || guess1 < random2 || guess2 < random1 || guess2 < random2 && guess1 < random1 || guess1 < random2 || guess2 < random1 || guess2 < random2) {
					cout << "Sorry, both of your guesses is too low. " << endl;
				}
				else if (guess1 < random1 || guess1 < random2 || guess2 < random1 || guess2 < random2 && guess1 > random1 || guess1 > random2 || guess2 > random1 || guess2 > random2) {
					cout << " One of your guesses is too low and the other is too high. " << endl;
				}
				else
					cout << "I don't know what the hell you did! " << endl;
					tries++;
					
				
						if (guess1 == random1 || guess1 == random2 && guess2 == random1 || guess2 == random2) {
							cout << "Would you like to play again? " << endl;
							cin >> replay;
							if (replay == 'N' || replay == 'n') {
								cout << "Thank You for Playing! " << endl;
								break;
							}
						}
				
		}
	}
	return 0;
}
To make the logic a little easier, removing lots of conditions, i would advice that you use functions.
Pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1. Generate numbers (use rand()) just once
2. Take numbers
3. Do the following checks
if both are correct
	Say so and exit
else if one is correct
	if one is high
		say so
	else if one is low
		say so
else if both are wrong
	if both are high
		say so
	else if both are low
		say so
	
4. Increment tries
5. Ask if user want a rerun

Tip: use functions to
1. Determine a good guess for one number
2. Determine if a number is high
3. Determine if a number is low

From these you can combine them in anyway you want with the || and && operators

Eg.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool correctFirstGuess(int num1, int num2, int guess1) {
return guess1 == num1 || guess1 == num2;
}
bool correctSecondGuess(int num1, int num2, int guess2) {
return guess2 == num1 || guess2 == num2;
}

bool goodGuess(int num1, int num2, int guess1, int guess2) {
return correctFirstGuess(num1, num2, guess1) && correctSecondGuess(num1, num2, guess2);
}

bool oneGoodGuess(int num1, int num2, int guess1, int guess2) {
return correctFirstGuess(num1, num2, guess1) || correctSecondGuess(num1, num2, guess2);
}


In main
1
2
3
if (goodGuess(random1, random2, guess1, guess2)) {
cout << " Congratulations! You have guessed both numbers correctly!" << endl;
}


Hope this helps. Good luck with the number guessing game.
Call srand() just once at the beginning of main().

There are actually 9 conditions. Guess1 can be less than, equal to, or greater than random1,
and guess2 can be less than, equal to, or greater than random2. That's 3 possibilities for guess1 and 3 for guess2, giving 9 combinations.

There's some ambiguity here. For example, if the random numbers are 34 and 67, and the guesses are 40 and 70, then do you report "both are too high" because 40>34 and 70>67, or do you report one "one is high and one is low" because 70>34 and 40<67?

You might be able to simplify the conditions if you guarantee that the guess1 < guess2 and random1 < random2. You can do this by simply swapping the numbers if they aren't in the right order.

Can you post the assignment? Maybe it will clarify exactly what must be built.
COMP 1102

Project Two

Assigned: March 21, 2018

Due:

April 4, 2018

Write a program that will:

Randomly select two integers between 1 and 100, inclusive.

- Prompt the user to enter their guesses for the values of the random integers.

- Repeat the following, until the user guesses both numbers correctly:

Compare the first guess with the first random integer and the second guess with

the second random integer.

- Report the results, in one of the following ways:

1) Congratulations! You guessed both numbers!

2) One of your guesses is correct, but the other is too high.

3) One of your guesses is correct, but the other is too low.

4) Sorry, both of your guesses are too high.

5) Sorry, both of your guesses are too low.

6) One of your guesses is too high and the other is too low.

Note that in cases 2, 3, and 6, the report should NOT give any indication as to

which guess is correct or is too high or too low.

- Unless both guesses are correct, prompt the user for their next pair of guesses.

Once both numbers are found, report the number of guesses required. Then ask

the user if s/he would like to play again.

If so, start the whole process over again. If not, end the program and report

the average number of guesses required.

Your source code file should be named guessing.cpp.

Turn in your project by submitting it as an attachment to a Blackboard Course

Message). Do NOT send your executable file.

To receive a grade, you must discuss your project with me in my office.

A sample interaction is shown on the next page. User input is shown in boldface.


I also have an Example page of the output if it needed. And No need to do the work for me. Id hate to take up too much of your time. Whatever help is greatly appreciated.
The program requirements are fairly straight-forward. You were making it complicated.

Compare the first guess with the first random integer and the second guess with the second random integer.
means for
1) Congratulations! You guessed both numbers!
you will do
1
2
3
4
    if(firstGuess == random1 && secondGuess == random2){
        // you guessed correctly
   }
// You compare the user's first guess with random1 only and user's second guess with random2 only.  


Do those checks and paste your most recent work if you still have problems getting it right.
Last edited on
It a matter of taste, but I would do this by setting two new variables to represent the results of the comparisons:
1
2
3
4
5
6
7
8
// Compare a number to a guess. Return <0, 0, or >1 depending on whether
// guess is less than, equal to, or greater than number
int comp(int number, int guess) {
    return guess - number;
}
...
            int comp1 = comp(random1, guess1);
            int comp2 = comp(random2, guess2);


Then the comparison logic isn't quite so verbose:
1
2
3
4
5
6
7
            if (comp1 == 0 && comp2 == 0) {
                cout << " Congratulations! You have guessed both numbers correctly!" <<
                    endl;
            } else if (comp1 == 0 && comp2 > 0 ||
                       comp2 == 0 && comp1 > 0) {
                cout << "One of your guesses is correct but the other is too high. " <<
                    endl;


Make const strings for the messages, perhaps with newlines, so that they won't need to be recreated each playthrough. This also makes the logic part more readable.

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

using namespace std;

static mt19937 mt_rand(time(0));
const string GRATZ = "Congratulations! You guessed both numbers!\n";
const string ONE_HIGH = "One of your guesses is correct, but the other is too high.\n";
const string ONE_LOW = "One of your guesses is correct, but the other is too low.\n";
const string BOTH_HIGH = "Sorry, both of your guesses are too high.\n";
const string BOTH_LOW = "Sorry, both of your guesses are too low.\n";
const string MIXED = "One of your guesses is too high and the other is too low.\n";
const string INTRO = "The computer has chosen two random integers from 1 to 100, inclusive.\n"
    "You must guess them both in the same order as picked by the computer.\n"
    "Enter two space-separated integers:\n";

void Game()
{
    int ra = mt_rand()%100 + 1;
    int rb = mt_rand()%100 + 1;
    int guesses = 1;
    
    cout << INTRO;
    int a, b;
    while (true)
    {
        cout << "? ";
        cin >> a >> b;
        
        if (a==ra && b==rb)
        {
            cout << GRATZ;
            cout << "Indeed, the computer picked " << ra << " and " << rb << endl;
            break;
        }
        else if (a==ra)
        {
            cout << (b>rb ? ONE_HIGH : ONE_LOW);
        }
        else if (b==rb)
        {
            cout << (a>ra ? ONE_HIGH : ONE_LOW);
        }
        else if (a>ra && b>rb)
        {
            cout << BOTH_HIGH;
        }
        else if (a<ra && b<rb)
        {
            cout << BOTH_LOW;
        }
        else
        {
            cout << MIXED;
        }
        guesses++;
    }
    cout << "It took you " << guesses << " guesses." << endl;
}

int main() 
{
    string input;
    while (true)
    {
        Game();
        cout << "Would you like to play again? (y/n)\n";
        cin >> input;
        if (toupper(input[0])!='Y')
            break;
    }
    
    return 0;
}


Running at https://repl.it/repls/LongKnownDividend
Topic archived. No new replies allowed.