I have a code which creates a random number, the user has to guess it in 10 tries. The first cout is the option to "Cheat? (Y/N)" which will display the secret number (the random number), mostly just for me to see whats happening.
If the user selects Y the number is displayed. But also it will be displayed if they select N. This is one point of confusion.
After this they are prompted to enter a guess. If the guess is wrong they will be asked if they want to try again. Whether they say Y or N, the program just quits and outputs nothing.
Here is the code:
Code:
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
srand ( time(NULL) );
int number = (rand() % 15) + 1;
int guess;
int trycount = 0;
char choice;
cout << "Cheat? (Y/N) \n";
cin >> choice;
if (choice == 'Y' || 'y')
cout << "The secret number is " << number << ".\n";
else if (choice == 'N')
cout << "Good for you.";
cout << "Please enter a guess: ";
cin >> guess;
while (guess != number && trycount < 10)
{
int choice;
cout << "Wrong, guess again? (Y/N): ";
cin >> choice;
if (choice == 'Y')
{
cout << "Guess again: ";
cin >> guess;
}
else if (choice != 'Y')
break;
{
if (guess < number)
cout << "Too low \n";
else if (guess > number)
cout << "Too high \n";
else (guess == number);
cout << "You guessed the number!";
}
trycount++;
}
return 0;
}
Here are some outputs/inputs:
Cheat? (Y/N)
Y
The secret number is 13.
Please enter a guess: 6
Wrong, guess again? (Y/N): Y
The Debugger has exited with status 0.
----------
Cheat? (Y/N)
N
The secret number is 11.
Please enter a guess: 7
Wrong, guess again? (Y/N): N
The Debugger has exited with status 0.
Can anyone give me a hint about what I am missing? I feel like it is a simple solution but I've tried it for a bit and I can't seem to figure out what is going wrong. Thank you!