Not sure what I am doing wrong. This is a best of seven guessing game. The problem I am facing is under the hint section. The hints are neither accurate nor do they come up when incorrect guesses is equal to two, which I thought I did correctly. Any input?
#include <iostream>
#include <cstdlib> // For rand and srand
#include <ctime> // For the time function
usingnamespace std;
int main()
{
srand(time(0)); // Confused about this
int Guess1;
int RanNum;
int NumberCorrect = 0, NumberIncorrect = 0;
cout << "Welcome to the guessing game! " << endl;
cout << "Choose Wisely: This is a best of seven game. " << endl;
// Guess Section
do
{
cout << "" << endl; // Space
cout << "I am thinking of a number from 1 to 4. " << endl;
cout << "What is your guess? ";
cin >> Guess1;
if (Guess1 > 4 || Guess1 < 1)
{
cout << "Error: Please guess a number between 1 and 4. " << endl;
}
RanNum = rand() % 4 + 1; // Makes RanNum between 1 and 4
if (Guess1 != RanNum)
{
cout << "" << endl;
cout << "Sorry! I was thinking of " << RanNum << endl;
++NumberIncorrect;
// Hint Section
if (NumberIncorrect == 2 && RanNum == 2 || RanNum == 4)
{
cout << "" << endl;
cout << "Here is a hint. The number is even. " << endl; }
if (NumberIncorrect == 2 && RanNum == 1 || RanNum == 3)
{
cout << "" << endl;
cout << "Here is a hint. The number is odd. " << endl; }
}
if (Guess1 == RanNum)
{
cout << "" << endl;
cout << "Correct! I was thinking of " << RanNum << endl;
++NumberCorrect;
}
// Win or Lose Section
} while(NumberCorrect != 4 && NumberIncorrect != 4);
cout << "Correct - " << NumberCorrect << "\t Incorrect - " << NumberIncorrect << endl;
{
if (NumberCorrect >= 4) { cout << "You Win!" ; // Not sure why the program didn't build when I wrote if (NumberCorrect == 4)
cout << "" << endl;}
if (NumberIncorrect >= 4) { cout << "You Lose!" ; // Same as above with (NumberIncorrect == 4)
cout << "" << endl;} // Greater than worked
}
system("pause");
return 0;
}
if (NumberIncorrect == 2 && RanNum == 2 || RanNum == 4)
&& has a higher precedence than ||, thus your code is equivalent to: if ((NumberIncorrect == 2 && RanNum == 2) || RanNum == 4)
So if RanNum is 4, the result will be true.
// Not sure why the program didn't build when I wrote if (NumberCorrect == 4)