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.
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
elseif one is correct
if one is high
say so
elseif one is low
say so
elseif both are wrong
if both are high
say so
elseif 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.
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.
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;
} elseif (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.
#include <iostream>
#include <random>
#include <string>
usingnamespace 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;
}
elseif (a==ra)
{
cout << (b>rb ? ONE_HIGH : ONE_LOW);
}
elseif (b==rb)
{
cout << (a>ra ? ONE_HIGH : ONE_LOW);
}
elseif (a>ra && b>rb)
{
cout << BOTH_HIGH;
}
elseif (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;
}