recursive do while loop

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

using namespace std;

int main()
{
    int start ;
    int finish;
    bool PlayAgain = 0;
    char PlayAgainAnswer;

    do {

      do {
        cout << "what number would you like the computer to count to?: ";
        cin >> finish;
        cout << "what number would you like the computer to start at?: ";
        cin >> start;
        if (start >= finish)
        cout << "The number you start at has to be smaller then the number your counting to. \n";
      } while (start >= finish);

      while (start <= finish){
        cout << start << endl;
        start = start + 1;
      }

      do {
        cout << "Do you wanna play again? (y/n): ";
        cin >> PlayAgainAnswer;
      } while (PlayAgainAnswer != 'y' || 'n' || 'Y ' || 'N');

      if (PlayAgainAnswer == 'y' || 'Y')
        bool PlayAgain = 1;

      if (PlayAgainAnswer == 'n' || 'N')
        bool PlayAgain =  0;

    } while (PlayAgain == 1);

    return 0;
}


never stops asking "Do you wanna play again? (y/n): "
This is what you need on line 33.

} while (PlayAgainAnswer != 'y' && PlayAgainAnswer != 'n' && PlayAgainAnswer != 'Y ' && PlayAgainAnswer != 'N');
Also lines 35:

if (PlayAgainAnswer == 'y' || PlayAgainAnswer == 'Y')

Line 37:

if (PlayAgainAnswer == 'n' || PlayAgainAnswer == 'N')

Remember, you have to check conditional operator (i.e ==) requires two inputs.
Last edited on
Topic archived. No new replies allowed.