Hello! I am almost finished with my program, I just need help with two last things. First, I don't know how to make it calculate the number of guesses the user made to get to the right answer. Second, it asks the user if they would like to play again, but when the user enters "Y" for yes, the program just stops and doesn't re-run itself.
//This program plays a random number guessing game with the user.
#include <cstdlib>
#include <time.h>
#include <iostream>
usingnamespace std;
int main()
{
//Create a random number.
srand(time(0));
int number;
number = rand() % 15 + 5;
//Variables.
int guess;
int numberOfGuesses=1; //HELP?
char tryAgain;
do
{
//Have the user guess a number.
cout << "Guess a number between 5 and 15: ";
cin >> guess;
//Loop if the number is too low or too high until the user guesses correct number.
if (guess < number)
cout << "Too low, try again." << endl;
elseif (guess > number)
cout << "Too high, try again." << endl;
//Let the user know they got the correct number.
else
cout << "Congratulations. You figured out my number." << endl;
}while (guess != number);
//Calculate and display the total number of guesses made.
cout << "Total number of guesses: "; //HELP?
cin >> numberOfGuesses + 1; //HELP?
system ("PAUSE");
//Ask the user if they would like to play again. //HELP?
cout << "Would you like the play again. Type y for yes: ";
cin >> tryAgain;
while (tryAgain == 'Y' || tryAgain == 'y');
return 0;
}
Line 17: You want to initialize numberOfGuesses to 0.
Line 25: When the user enters a guess, you want to increment numberOfGuesses.
numberOfGuesses++;
Line 39: You don't want to input numberOfGuesses. You want to output it.
cout << numberOfGuesses << endl;
Line 46: This while loop doesn't do anything. This would do what you want if you start another do loop at line 19. Don;t forget to reset numberOfGuesses each time through the loop.
I think you are a little bit too selfish you are not willing to give someone at least one or two more guesses. So if I were you, for example, I would make it something more generous : int numberOfGuesses = 3;
@closed account - I think you're misinterpreting numberOfGuesses . The OP is using that to count how many attempts have been made, not to limit the number of guesses.
Here is my latest code. I still need help understanding how to make the program start again if the user enters "Y". Right now, it's just stopping and not allowing the user to enter Y.
[code]
//This program plays a random number guessing game with the user.
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
//Create a random number.
srand(time(0));
int number;
number = rand() % 15 + 5;
//Variables.
int guess;
int numberOfGuesses=0;
char tryAgain;
do
{
//Have the user guess a number.
cout << "Guess a number between 5 and 15: ";
cin >> guess;
numberOfGuesses++;
//Loop if the number is too low or too high until the user guesses correct number.
if (guess < number)
cout << "Too low, try again." << endl;
else if (guess > number)
cout << "Too high, try again." << endl;
//Let the user know they got the correct number.
else
cout << "Congratulations. You figured out my number." << endl;
}while (guess != number);
//Calculate and display the total number of guesses made.
cout << "Total number of guesses: "; //HELP?
cout << numberOfGuesses << endl;
system ("PAUSE");
//Ask the user if they would like to play again. //HELP?
cout << "Would you like the play again. Type y for yes: ";
cout << tryAgain << endl;
(tryAgain == 'Y' || tryAgain == 'y');
It says it cannot be done when in fact closed a/c has shown it can.
This article throws some light on why it works. It seems the standard and some compilers allow a great deal of flexibility or simply ignore it because the writers have confused a good idea/convention with an inviolable condition of use of the language.
So, from 'forbid' we go to 'can' and on to 'depends'. Somebody should tell Cubbi because 'cannot' is obviously the wrong choice of word.