number guessing game no more than 5 tries

QUESTION:
I'm trying to write a program to plays a number guessing game. The program currently gives the user as many tries need to guess the correct number. However I want the program to give the user no more than five tries to guess the number. Also, I want want my program to print a message, such as "You win!" or "You lose!" when the number is guess correctly or incorrectly.

I posted my sample code below with comment lines on the code lines I am having trouble with.

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
50
51
52
53
54
55
56
57
58
59
60
61
 #include <iostream>
#include <cstdlib>
#include <ctime>
#include <istream>


using namespace std;

int main()
{
    int num,guess,noOfGuesses;
    bool isGuessed;

    srand(time(0));
    num = rand () % 100;
    isGuessed = false;

    while (!isGuessed)
    {
        cout << "Enter an integer greater"
        << " than or equal to 0 and "
        << "less than 100: ";

    cin >> guess;

    cout << endl;
{
//No more than 5 tries and "You Lose!"
      while ((noOfGuesses < 5) && (!isGuessed))

          if (noOfGuesses > 5)
        cout<< "\n You Lose!";
// No more than 5 tries and "You Lose!"
}

   if (guess == num)

    {


   cout << "You guessed the correct "
        << "number."<< endl;
        
// You Win!
cout<< "\nYou win!";
    isGuessed = true;
// You Win!
    }
   else if (guess < num)
    cout << "Your guess is lower than the "
        << "number.\n Guess again!"
        <<endl;
   else
        cout<< "Your guess is higher than "
        << "the number. \n Guess again!"
        << endl;
}

    return 0;
}
You should add a variable int guess_times = 0; and ++ it when the user makes a guess. In the while loop where the user enters input, put in the arguements
times<=5.
can you please show me an example as to what you are suggesting?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main() {
	
	int numberOfGuesses = 0;
	int guess;
	while (numberOfGuesses != 5) // Limits the number of guesses to 5
	{
		cin >> guess; // user guesses
		numberOfGuesses++; // add 1 to the guess variable. After 5 guesses
		// it will be equal to 5 and quit the loop
	}
	return 0;
}
I'll use your problem to show you how to do it.

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
#include<iostream>
#include<random>
#include<ctime>
using namespace std;

int main(){
srand(time(0));
int num = rand() % 100;
int guess_times = 0;
int guess;
bool found  = false;
while(guess_times<5){
cin>>guess;
if (guess>num){
cout<<"Your guess is higher, try again\n";
}
else if (guess<num){
cout<<"Your guess is lower, try again\n";
}
else if(guess==num){
found = true;
break; //stop the while loop
}
guess_times++;
}
if (found==false){
    cout<<"You are out of guesses so you're out of the game, the number was "<<num;
}
else if(found==true){
    cout<<"You found the number!!!\n";
}
}


If you have any questions as to how the code works just asks
The program will never end unless you type in the number. I guess he is suggesting to put a variable int to keep track of the number of guesses the person types, otherwise it will just keep accepting more tries until the correct number is typed. Just make an int or short variable to keep track of the amounts of tries and put a loop that allows you to terminate the program if the number of guesses are equal to 5. you can do a for loop or while loop for that or add it to an existing loop.
@Psalazar2016

Exactly what the code I wrote does.
Thank you for all your tips! I solved it thank you!
Topic archived. No new replies allowed.