#include <iostream>
#include <string>
usingnamespace std;
void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char* argv[])
{
int high = 10;
int low = 1;
int numberToGuess;
bool stillPlaying = true;
bool winner = false;
int guess;
while (stillPlaying)
{
// generate a random number between low and high value
numberToGuess = rand() % (high - low + 1) + low;
//tell the user about the game
introduction(high, low);
while (!winner)
{
guess = getGuess();
int NumberofGuesses = 1;
winner = testGuess(guess, numberToGuess);
if (winner)
{
cout << "Congatulations, You WON!!!" << endl;
}
else
{
cout << "You have guessed " << NumberofGuesses << " times" << endl;
++NumberofGuesses;
}
//output the number of guesses they've made so far
cout << "Would you like to play again? (Yes or No)" << endl;
string replay;
cin >> replay;
bool questionAnswered = false;
while (!questionAnswered) //From here to...
{
if (replay == "Yes" || replay == "yes")
{
stillPlaying = true;
bool questionAnswered = true;
}
elseif (replay == "No" || replay == "no")
{
stillPlaying = false;
bool questionAnswered = true;
return 0;
}
} // Here
}
}
cout << "Thanks for playing!" << endl;
cin.ignore();
cin.get();
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
cout << "I'm thinking of a number between 1 and 10..." << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
string Guess;
cout << "What is your guess?" << endl;
cin >> Guess;
return 1;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
if (guess != numberToGuess)
{
cout << "I am sorry, that is not correct." << endl;
returnfalse;
}
if (guess = numberToGuess)
{
cout << "That is correct!" << endl;
returntrue;
}
return (main);
}
Line 106. = is assignment, not equality test. This could be replaced with else
Delete line 111, it is nonsense.
Explain to us the logic behinds lines 53 to 66. Doing this might help you see the problem.
With yes no answers, restrict them to Y or N (don't get involved in all the permutations of their spelling) use std::toupper to convert y or n to uppercase. Then your tests will be easier.
while (!questionAnswered) //While the question is not answered, it will:
{
if (replay == "Yes" || replay == "yes") // if the answer to the question is yes then:
{
stillPlaying = true; // You are still playing
bool questionAnswered = true; // and the question has been answered so it should get out of the loop
}
elseif (replay == "No" || replay == "no") // if the answer to the question is no then:
{
stillPlaying = false; // You are not still playing
bool questionAnswered = true; // and the question has been answered so it should get out of the loop
return 0;
}
}
The one warning,
incorrect operator: assignment of constant in Boolean context. Consider using '==' instead.
Has been fixed and it is doing the same thing as before.
#include <iostream>
#include <string>
usingnamespace std;
void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);
int main(int argc, char* argv[])
{
int high = 10;
int low = 1;
int numberToGuess;
bool winner = false;
int guess;
char answer{'Y'}; // <---
while (toupper(answer) == 'Y' && winner == false)
{
numberToGuess = rand() % (high - low + 1) + low;
//tell the user about the game
introduction(high, low);
guess = getGuess();
int NumberofGuesses = 1;
winner = testGuess(guess, numberToGuess);
if (winner)
{
cout << "Congatulations, You WON!!!" << endl;
break; // <--
}
else
{
cout << "You have guessed " << NumberofGuesses << " times" << endl;
++NumberofGuesses;
// TODO: NEED TO TEST AGAINST NO. GUESSES
}
cout << "Would you like to play again? (Y or N): ";
cin >> answer;
}
cout << "Thanks for playing!" << endl;
return 0; // <--
}
//Tells the user the rules of the game
void introduction(int high, int low)
{
cout << "I'm thinking of a number between 1 and 10..." << endl;
}
//Prompts for, inputs, and returns the next guess
int getGuess()
{
string Guess;
cout << "What is your guess? ";
cin >> Guess;
return 1;
}
// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
if (guess != numberToGuess)
{
cout << "I am sorry, that is not correct." << endl;
returnfalse;
}
if (guess == numberToGuess)// <--
{
cout << "That is correct!" << endl;
returntrue;
}
return 0; // <--
}
I'm thinking of a number between 1 and 10...
What is your guess? 5
I am sorry, that is not correct.
You have guessed 1 times
Would you like to play again? (Y or N): y
I'm thinking of a number between 1 and 10...
What is your guess? 7
I am sorry, that is not correct.
You have guessed 1 times
Would you like to play again? (Y or N): n
Thanks for playing!
Program ended with exit code: 0