Need help adding a Yes continue/No end program loop

I don't know how/what/where to add a loop to continue if you type Y or end the program if you type N to the first question in my code.

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

int main()
{
	int randNumber;
	int userNumber;
	int answer;

	char again = 'Y';
	bool win = false;
	unsigned seed = time(0);

		srand(seed);
		randNumber = 0 + rand() % 501;

		cout << "I'm thinking of a number between 0 and 501. Do you want to guess what it is? (Y/N)" <<endl;
		cin >> userNumber;

		{
			while (!win)
			{				
				if (userNumber < randNumber)
				{
					cout << "Sorry, bud. You're number was out of range. You guessed too low, try again." << endl;
					cin >> userNumber;
				}
				else if (userNumber > randNumber)
				{
					cout << "Sorry, bud. You're number was out of range. You guess too high, try again." << endl;
					cin >> userNumber;
					cin.ignore();
				}
				else //winner
				{
					cout << "Yup, that was my number! We're done, you can leave now." << endl;
					win = true;
				}
			}



			

		}
		
cin.get();
return 0;
}
I'm assuming it goes on line 21, but I don't know what to put there.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    // consider using the facilities in the C++ random number library
    // https://en.cppreference.com/w/cpp/numeric/random
    std::srand( std::time(nullptr) ) ;

    char answer ; // 'Y' or 'N'
    while( std::cout << "\nI'm thinking of a number in [0,501)."
                        " Do you want to guess what it is? (Y/N): " &&
                        std::cin >> answer &&
                        ( answer == 'Y' || answer == 'y' ) )
    {
         const int rand_number = std::rand() % 501 ;

         int user_number ;
         std::cout << "your guess? " ;
         std::cin >> user_number ;

         if( user_number < rand_number ) std::cout << "too low\n" ;
         else if( user_number > rand_number ) std::cout << "too high\n" ;
         else std::cout << "that was my number. well done!\n" ;

         std::cout << "the random number was " << rand_number << '\n' ;
    }
}
Topic archived. No new replies allowed.