Problem with my loop?

Hello everyone. I'm making a small dice rolling game. It's very basic, all text. The problem I'm having with the program right now is: The program doesn't end when either player's health reaches 0. Can someone help me understand why?
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
  srand(time(NULL));
  int mydice; //The user's dice. 
  int yourdice;//The computer's dice.
  int myhealth = 100;//The player's health. 
  int yourhealth = 100;//Opponent's health.
  string name;//Player's name.
  string answer;//Used for questions.
  cout << "Please enter your name pilot.\n";
  getline(cin,name);
  cout << "Is " << name << " correct?\n";
  getline(cin,answer);
  if (answer == "no") {
    cout << "Please enter your name pilot.\n";
    getline(cin,name);
    cout << "Is " << name << " correct?\n";
    getline(cin,answer);
    }
  else if (answer == "yes") {
    cout << "Welcome " << name << "\n";
    cout << "Let's begin.\n";
    }
  else {
    cout << "Please enter a valid answer.\n";
    } 
  cout << "\n";
  cout << "You encounter an enemy Mech. Type 'roll' to attack.\n";
  do {
    getline(cin,answer);
    if (answer == "roll") {
      cout << "HP: " << myhealth << "\n";
      cout << "Enemy HP: " << yourhealth << "\n";
      mydice = rand() % 6;
      yourdice = rand() % 6;
      cout << mydice << " " << yourdice << "\n";
        if (mydice > yourdice) {
        yourhealth -= 20;
        cout << "HP: " << myhealth << "\n";
        cout << "Enemy HP: " << yourhealth << "\n";
        }
        else if (mydice < yourdice) {
        myhealth -= 20;
        cout << "HP: " << myhealth << "\n";
        cout << "Enemy HP: " << yourhealth << "\n";
        }
        else if (mydice == yourdice) {
        cout << "HP: " << myhealth << "\n";
        cout << "Enemy HP: " << yourhealth << "\n";
        }
      }
    }
    while (myhealth > 0 || yourhealth > 0);/* Pretty sure my problem is here.*/
    system("PAUSE");	
  return 0;
}
Since you have a logical OR, it will keep looping while either myhealth OR yourhealth is greater than 0.
Well, the only thing that made sense to me was to change

(myhealth > 0 || yourhealth > 0);

to

while (myhealth > 0 | yourhealth > 0);

and it still isn't working. Do I need to avoid an OR statement entirely?
try &&
make it >= 0
and it should work

Last edited on
*sigh* Thanks for the help everyone. Here is the how it finally ended up working:

while (myhealth > 0 && yourhealth > 0);

Now, I've got a whole new problem. I changed the if statements in the beginning(for entering the correct name) to switch cases, and I was wondering if loops could be embedded in individual cases. Here's what I have so far:
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
  srand(time(NULL));
  int choice; //Used for switch cases.
  int mydice; //The user's dice. 
  int yourdice;//The computer's dice.
  int myhealth = 100;//The player's health. 
  int yourhealth = 100;//Opponent's health.
  string name;//Player's name.
  string answer;//Used for questions.
  
  cout << "Please enter your name pilot.\n";
  getline(cin,name);
  cout << "Is " << name << " correct?\n";
  cout << "1) Yes\n";
  cout << "2) No\n"; 
  cin >> choice;
  cout << "\n";
  switch(choice) {
  case 1:
  cout << "\n";
  cout << "You encounter an enemy Mech. Type 'roll' to attack.\n";
  do {
    getline(cin,answer);
    if (answer == "roll") {
      mydice = rand() % 6;
      yourdice = rand() % 6;
      cout << mydice << " " << yourdice << "\n";
        if (mydice > yourdice) {
        yourhealth -= 20;
        cout << "HP: " << myhealth << "\n";
        cout << "Enemy HP: " << yourhealth << "\n";
        }
        else if (mydice < yourdice) {
        myhealth -= 20;
        cout << "HP: " << myhealth << "\n";
        cout << "Enemy HP: " << yourhealth << "\n";
        }
        else if (mydice == yourdice) {
        cout << "HP: " << myhealth << "\n";
        cout << "Enemy HP: " << yourhealth << "\n";
        }
      }
    }
    while (myhealth > 0 && yourhealth > 0);
   break;
   case 2:
   cout << "Too bad.\n";
   break;
  default:
   cout << "Invalid choice\n";
   break;
  }
  system("PAUSE");	
  return 0;
}


I think I need loops in case 2 and default so that the programs asks for a name again if these options are selected. Am I going in the right direction, or am I way off? Thanks in advance all.
Last edited on
i compiled your code and see that it is not working, but not just in that it is not accepting 0 health for either player
the dice rolls are consistantly the same and have no variation on the amount of health taken from either player...
checking to see if i can find a way to randomize better for you.
Actually, it does stop when a player's health reaches 0, but I see what you mean about the health. It seems to whittle down both players' health equally; i.e. I lose health, you lose health, draw, repeat. It's a little more random than that, but you get the gist of it.
Sorry to double post(can I call bump?) but, could my problem be that I'm reseeding the random number generator every time I type roll? Correct me if I'm wrong, but the "random" number is actually time-based isn't it? I think I'm gonna move srand to the beginning of the program.

EDIT: Crap, never mind. That definitely did NOT work...
Last edited on
Topic archived. No new replies allowed.