@hoogo,
What do you mean it will not compile. It worked fine for me. Although I tend to use
srand(static_cast<size_t>(time(NULL)));
. Look it up "srand" takes an "unsigned int";
void srand (unsigned int seed);.
http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand
Hello MJnoob,
hoogo failed to mention that "srand" only need to be done once before the do/while loop.
about the guess checking, I did try a couple of things but I am pretty sure that I am missing something I tried :
if(myArray[i]=userinput) |
Right idea, but put it in a for loop to be done after the input of guess.
Your code will work, it is just that you are using the wrong order of things.
before the while loop all you need is:
1 2 3 4 5 6
|
char response{}; // <--- Needs to be before the do/while loop to be defined before being used in the while condition of the do/while loop.
std::string playerName;
std::cout << "\nwelcome " << playerName << "!" << std::endl;
std::cout << "Please enter a number between 0 and 100 please: ";
|
You only need to input the player's name once.
Inside the while loop first get the input for guess then use your if statement to validate the number entered. Based on checking two different thing in the if condition [(MIN and MAX) and (!std::cin)] I set up what you had with:
1 2 3 4 5 6 7 8 9 10 11 12
|
if (guess > MAX || guess < MIN || !std::cin)
{
std::cout << "\nsorry " << playerName << " your guess is outside the range or invalid,\nplease enter an integer between " << MIN << " and " << MAX << ": ";
if (!std::cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
continue;
}
|
The added if statement is to keep the "std::cin.ignore" statement from waiting for input if the "std::cin >> guess;" contains only an out of range number.
The "!std::cin" will catch any problem with a failed "std::cin" whereas "std::cin.fail()" may not be the bit that was set and you are not checking the others that might have been set.
What I am use to seeing is something more like:
1 2 3 4 5 6 7 8 9 10 11 12
|
std::cin >> guess;
while (!std::cin)
{
std::cout << "\n Entry is not a number. Try again." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "Please enter a number between 0 and 100 please: ";
std::cin >> guess;
}
|
This way you stay in the while loop until you get a valid number to check and then follow this while loop with the if statement checking for out of range then the for loop to check for duplicate numbers.
With the changes I made one possible put put is:
Welcome to The Guessing game
input your name please: Andy
welcome Andy!
Please enter a number between 0 and 100 please: 50
The number guessed is too high, Please enter your guess again: 25
The number guessed is too high, Please enter your guess again: 24
Your guess is correct! congratulation Andy you got it in 3 guesses
you have guessed :
50
25
24
would you like to play again?
type Y to play again or press any key to exit: y
Welcome back Andy! Let's play a game.
Please enter a number between 0 and 100 please: 50
The number guessed is too high, Please enter your guess again: 25
The number guessed is too high, Please enter your guess again: 12
The number guessed is too low, Please enter your guess again: 18
The number guessed is too low, Please enter your guess again: 21
The number guessed is too low, Please enter your guess again: 24
Your guess is correct! congratulation Andy you got it in 6 guesses
you have guessed :
50
25
12
18
21
24
would you like to play again?
type Y to play again or press any key to exit:
|
Just for fun I added this to the code:
1 2 3 4
|
if (firstTime)
std::cout << "\nWelcome " << playerName << "!\n" << std::endl;
else
std::cout << "\nWelcome back " << playerName << "! Let's play a game.\n" << std::endl;
|
What you have can work although it may not be the best way to code it unless you want to rewrite the whole program.
Hope that help,
Andy