Help Calculating # of Guesses and Making Program Run Again

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//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=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;
        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?
    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;
}


Thank you!
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.

Hi,
Firstly :
int numberOfGuesses = 1;

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');

return 0;
}
[code]

Last edited on
Line 47: What do you think this line is doing? Hint: It's not doing anything.
 
(tryAgain == 'Y' || tryAgain == 'y');


Please use code tags correctly. A closing code tag is:
[/code]

Please edit your post and correct your closing code tag.
Last edited on
> Ask the user if they would like to play again?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;
int main()
{
srand(time(0));

int number = rand() % 15 + 5;
int guess;
int guessesCount = 0;

do
{
cout << "Guess a number between 5 and 15: ";
cin >> guess;
guessesCount++;

if (guess < number)
cout << "Too low, try again." << endl << endl;
else if (guess > number)
cout << "Too high, try again." << endl << endl;
else
cout << "Congratulations. You figured out my number." << endl << endl;
}while (guess != number);


cout << "Total number of guess attempts : ";
cout << guessesCount << endl << endl;

char tryAgain;

cout << "Again? (Y) : ";
if((cin >> tryAgain), (toupper(tryAgain) == 'Y')) return main(); 

cout << "\nThanks for playing!";
return 0;
}
Last edited on
Does that help you? :)
@closed account:
Line 34: Recursive calls to main are illegal.
@AbstractionAnon
main() actually is just no more than that of an ordinary function. It's nothing special.

> Line 34: Recursive calls to main are illegal.

Not true though.
https://ideone.com/sHE3Ek

At least I am not near my computer this time.
It's nothing special.

Yes, it is. It is the entry into your program.

Not true though.

I didn't say it would not compile.
However, the C++ spec expressly forbids doing so.
See item #1 under Explanation:
http://en.cppreference.com/w/cpp/language/main_function


Last edited on
So you are trying to win me. Good for you :)

> Ask the user if they would like to play again?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;
int mymain()
{
srand(time(0));

int number = rand() % 15 + 5;
int guess;
int guessesCount = 0;

do
{
cout << "Guess a number between 5 and 15: ";
cin >> guess;
guessesCount++;

if (guess < number)
cout << "Too low, try again." << endl << endl;
else if (guess > number)
cout << "Too high, try again." << endl << endl;
else
cout << "Congratulations. You figured out my number." << endl << endl;
}while (guess != number);


cout << "Total number of guess attempts : ";
cout << guessesCount << endl << endl;

char tryAgain;

cout << "Again? (Y) : ";
if((cin >> tryAgain), (toupper(tryAgain) == 'Y')) return mymain(); 

cout << "\nThanks for playing!";
return 0;
}
int main() {return mymain();}


The changes are so subtle I expect you would not really notice it haha... :)
> I didn't say it would not compile.
Then who cares? As long as someone can get his/her program to work why bother?
closed account (48T7M4Gy)
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.

http://stackoverflow.com/questions/5177167/why-does-my-c-compiler-allow-recursive-calls-to-main
Topic archived. No new replies allowed.