How to limit the number of user input

I want to make the user can only guess 10 times after that they win without guessing the number 5. What should I add?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
int main(){
int num;
char rerun;
    do {
    std::cout << "Please guess a number: ";
    std::cin >> num;
    if (num == 5)
       std::cout << "Hey! You weren't supposed to enter 5!";
    else{
       do{
         std::cout << "Please guess again: ";
         std::cin >> num;
       }
       while (num != 5);
       std::cout << "\n Hey! You weren't supposed to enter 5!";
       std::cout << "\n Do you want to play again? (y/n)";
       std::cin >> rerun;
         }
    }
    while (rerun == 'y' || rerun == 'Y');
}
Last edited on
Hello BeginnerOnProgram,

There is nothing wrong with your program except that it is hard to read. Consider this

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
#include <iostream>

int main()
{
	int num;
	char rerun;

	do
	{
		std::cout << "Please guess a number: ";
		std::cin >> num;

		if (num == 5)
			std::cout << "Hey! You weren't supposed to enter 5!";
		else
		{
			do
			{
				std::cout << "Please guess again: ";
				std::cin >> num;
			} while (num != 5);

			std::cout << "\n Hey! You weren't supposed to enter 5!";
			std::cout << "\n Do you want to play again? (y/n)";
			std::cin >> rerun;
		}
	} while (rerun == 'y' || rerun == 'Y');
}


With a few blank lines and having the {}s line up in the same column makes the code much easier to read.

I do question the second do/while in the else block. That just does not seem like it should be there, but realize that I have not tested the program yet.

For a limit I would define a variable std::size_t count{}; where "size_t" is another name for "unsigned int" and the empty {}s will initialize the variable to zero.

In the second do/while loop I would add one to count and change the while condition to:
while (num != 5 && count < 6); and see if that works. I see more when I test the code.

Hope that helps,

Andy
Hello BeginnerOnProgram,

After running the program I made some changes.

Above main I put the line constexpr std::size_t MAXTRIES{ 5 };.

In main I defined the variable "count" and set it to 1. Because the first guess is after the outer do/while loop.

Looking at my code above I moved lines 24 and 25 outside the else block so that the if statement would also see these line and I added
if (std::toupper(rerun) == 'Y') count = 1; // <--- Reset "count" for next play. . Otherwise it would just keep adding to "count" and become unusable.

In the else block I would consider changing the message to reflect that the maximum number of tries has been reached. Maybe even an if/else one part for reaching the max tries and the other for entering 5.

Hope that helps,

Andy
Thank you for answering me, but I still can't limit it to 10. Do you know where did I do wrong? Thx btw.
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
#include <iostream>
#define constexpr std::size_t MAXTRIES{10};
int main()
{
              int num;
              char rerun;
              std::size_t count{1};
              do {
                          std::cout << "Please guess a number: ";
                          std::cin >> num;

                          if (num == 5)
                              std::cout << "Hey! You weren't supposed to enter 5!";
                          else{
                                  do{
                                     std::cout << "Please guess again: ";
                                     std::cin >> num;
                                    } while (num != 5 && count < 10);

                                std::cout << "\n Hey! You weren't supposed to enter 5!";
                              }
                                std::cout << "\n Do you want to play again? (y/n)";
                                std::cin >> rerun;
                                if (std::toupper(rerun) == 'y') count = 1;

                }while (rerun == 'y' || rerun == 'Y');
}
I would use 2 nested while loops. The outer for running until the user hits 'n', the inner until the number of guesses is 10.
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
#include <iostream>
#include <cctype>

const int MAX_GUESSES = 10;

using namespace std;

int main()
{
  bool running = true;
  char choice;

  while (running)
  {
    int numGuesses = 0;
    int number;

    while (true)
    {
      cout << "Guess a number: ";
      cin >> number;
      numGuesses++;
      if (numGuesses == MAX_GUESSES)
        break;
      if (number == 5)
      {
        cout << "\nHey! You weren't supposed to enter 5!" << "\n";
      }
    }
    std::cout << "\n Do you want to play again? (y/n)";
    std::cin >> choice;
    choice = char(toupper(choice));
    if (choice == 'N')
      running = false;
  }
}

What's not really clear is if there should be way to stop before 10 guesses.
Hello BeginnerOnProgram,

Line 18 is set up correctly, but "count" is always 1. Inside the do/while loop you need to change the value of count with count++; otherwise the while condition will always be true unless you enter 5 for "num"

When you find a problem like this I usually put a break point in the program so I can check the value of "count" . Or you could put a cout statement as the last line of the do/while loop to print the value of "count.

Hope that helps,

Andy

Edit:

P.S. the way to stop before 10 guessed is when you enter 5.

And Thomas1965's example is a very good alternative.
Last edited on
Topic archived. No new replies allowed.