Loops

I have to write a code that has a user guess the random number generated. Then it is suppose to ask them if they want to play again. If they say no the program ends, if they say yes the program picks another random number. I am having a problem with getting the program to continue when the user picks yes.
I am assuming I have done the loop wrong.
The program operates and does what it is suppose to up until the do you want to play again part.

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
62
63
64
65
66
67
68
 #include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

/**
 * DESCRIBE_THE_PROGRAM_HERE
 * 
 * @creator YOUR_NAME_GOES_HERE
 */

int main(int, char**) {
      srand(time(NULL));   

const int MIN_NUMBER = 1;
const int MAX_NUMBER = 100;
const int EXIT_VALUE = -1;
const int MAX_GAMES = 256;   
   
int random_number = rand() % MAX_NUMBER + 1;
int guess_taken = 0 ;
int n ;
char c ;


while (guess_taken < 256 )
{
    cout << "Guess a number between 1 and 100 ( -1 to give up): " << endl ;
    cin >> n ;
            
        guess_taken = guess_taken + 1 ; 
        
    if (n == -1){
        cout << "***QUITTER***" << endl ;
        (void) system("pause");      
        return 0; }   
         
    if ( n > MAX_NUMBER || n < MIN_NUMBER && n != EXIT_VALUE)
        cout << "Your guess is not between 1 and 100. Try again." << endl ;
        
    if (n < random_number){  
        cout << "Your guess is too low." << endl ; }
        
    if (n > random_number){
        cout << "Your guess is too high." << endl ; }
        
    if ( n == random_number){
        break;}
                
    if ( n == random_number)
        cout << "*** GOT IT *** it took you " <<guess_taken<< " guesses." << endl ; 
}
cout <<"Do you want to play again (Y or N)?" << endl ;
cin >> c ;
    
    if ( c == 'N' || c == 'n'){
        cout << "***Good Bye***" << endl ;
        (void) system("pause");      
        return 0; }  
         
    if ( c == 'Y' || c =='y') {
        (THIS IS WHERE I NEED HELP)}
}

(void) system("pause");      
   return EXIT_SUCCESS;
}
You want two nested loops.
The outer loop repeats the "game" as many times as the player wants.
The inner loop does one game.
Hi there,
How about adding a bool into your while loop, and set the value in your if statement.
Example:
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>

using namespace std;

int main() {
	
	bool done = false; 
	int i;
	char end;

	cout << "Please Inpute number: ";
	cin >> i;

	while (!done && i < 250) {
		cout << "Inpute 'E' to end: ";
		cin >> end;

		if (end == 'e' || end == 'E') {
			done = true;
		}
	}
}
Topic archived. No new replies allowed.