Nested loop in War game

I'm trying to get the game to loop and play again, while also having a loop inside that one to validate the users input when it asks them if they want to play again. I'm most likely doing this wrong lol, but my professor is very little help and I only got a "figure it out" response from her.

the code compiles and runs, but when it prompts the user if they want to play again, the program quits no matter what is entered.

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;

int main ()
{

  // Seeds random number generator
  srand(time(0));

  //Decleration of variables
  int humanCard;
  int computerCard;
  string humanCardName;
  string computerCardName;

  int humanSuitNumber;
  int computerSuitNumber;
  string humanSuit;
  string computerSuit;
  char keepPlaying;


  while (true)
  {
  cout << endl;

  // Generates a random number from 2 to 14
  humanCard =  2 + (rand() % 13);
  computerCard =  2 + (rand() % 13);

  // Assigns human card value
  if (humanCard < 11)
    humanCardName = humanCard;
  if (humanCard == 11)
    humanCardName = "Jack";
  if (humanCard == 12)
    humanCardName = "Queen";
  if (humanCard == 13)
    humanCardName = "King";
  if (humanCard == 14)
    humanCardName = "Ace";

  // Assigns computer card value
  if (computerCard < 11)
    computerCardName = computerCard;
  if (computerCard == 11)
    computerCardName = "Jack";
  if (computerCard == 12)
    computerCardName = "Queen";
  if (computerCard == 13)
    computerCardName = "King";
  if (computerCard == 14)
    computerCardName = "Ace";

  // Generates a random number from 0 to 3
  humanSuitNumber = rand() % 3;
  computerSuitNumber = rand() % 3;

  // Assigns suit to human card
  if (humanSuitNumber == 0)
    humanSuit = "Spades";
  if (humanSuitNumber == 1)
    humanSuit = "Diamonds";
  if (humanSuitNumber == 2)
    humanSuit = "Hearts";
  if (humanSuitNumber == 3)
    humanSuit = "Clubs";

  // Assigns suit to computer card
  if (computerSuitNumber == 0)
    computerSuit = "Spades";
  if (computerSuitNumber == 1)
    computerSuit = "Diamonds";
  if (computerSuitNumber == 2)
    computerSuit = "Hearts";
  if (computerSuitNumber == 3)
    computerSuit = "Clubs";

  // Output statements to display card suit and value
  if (computerCard < 11)
    cout << "Computer's card is a " << computerCard << " of " << computerSuit << endl;
  else
    cout << "Computer's card is a " << computerCardName << " of " << computerSuit << endl;
  if (humanCard < 11)
    cout << "Human's card is a " << humanCard << " of " << humanSuit << endl;
  else
    cout << "Human's card is a " << humanCardName << " of " << humanSuit << endl;


  // If statements to evaluate which player wins
  if (humanCardName == computerCardName)
    cout << "--<< It's a tie. >>--" << endl;

  if (humanCardName > computerCardName)
    cout << "--<< Human wins! >>--" << endl;

  if (humanCardName < computerCardName)
    cout << "--<< Computer wins! >>--" << endl;
  cout<< endl;

    while (true)
    {
    cout << "Continue? [Y/N] ";
    cin >> keepPlaying;
    cin.ignore(1000, 10);
    if (keepPlaying != 'y' || keepPlaying != 'Y' || keepPlaying != 'n' || keepPlaying != 'N') break;
    }
      if (keepPlaying != 'n' || keepPlaying != 'N') break;
  }

}
if (keepPlaying != 'y' || keepPlaying != 'Y' || keepPlaying != 'n' || keepPlaying != 'N') break;

So no matter what the user enters, 'Y/y' or 'N/n', it will always break. I think you can see what's wrong yourself now.
Last edited on
Topic archived. No new replies allowed.