want to play again? true or false?


i have just started learning and im trying to make a system that will ask the player if he wants tot play again but whenh the player inputs Y or N, it always just repaets whats in the while loop again even if the bool statment is false?


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


using namespace std;

int main()
{
   int x,y;
   char YN;
   bool PA;

   while (PA = 1) {

       cout << "enter first number: ";
       cin >> x;

       cout << "please enter a number larger then the first: ";
       cin >> y;

       while (x <= y) {
         cout << x << "\n";
         x = x+1;
       }

       cout << "Would you like to play again? (y/n)";
       cin >> YN;

       if (YN == 'y') {
         PA = 1;
       } else {
         PA = 0;
       }
   }

    return 0;
}
Last edited on
29
30
31
32
33
       if (YN == 'y') {
         PA = true;
       } else {
         PA = false;
       }

You can condense that to something simpler:

PA = (YN == 'y');

Hope this helps.
its still not working =(
Last edited on
Notice the operator in while (PA = 1).
yes, what do i do?
Just while (PA == 1) instead.
ohh, wow sorry, thanks for the help, i was assigning instead of checking to see if they were equal, right?
Yes. Also since the it's of type bool you should use true and false. See Duoas reply for example.
No problem, Daniel. I still sometimes accidentally type the equality operator when I meant to use the assignment operator, though luckily not as often the other way around.
Topic archived. No new replies allowed.