how to exit loop

HI i want to exit this loop whenif(comphealth <= 0 || yourhealth <= 0) that if statement is true.how do i do that.like can i put something after the if statement?

1
2
3
4
5
6
7
while(comphealth > 0 || yourhealth > 0)
    {
        yourchoiceagain();
        compattack();

        if(comphealth <= 0 || yourhealth <= 0) 
    }
The keyword is break:
1
2
if(comphealth <= 0 || yourhealth <= 0)
  break;
it still doesnt exit?

1
2
3
4
5
6
7
8
9
while(comphealth > 0 || yourhealth > 0)
    {
        yourchoiceagain();
        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
        break;
        }
    }
actually this while(comphealth > 0 || yourhealth > 0) and that
1
2
3
if(comphealth <= 0 || yourhealth <= 0){
        break;
        }
are doing the same

your if clause is redundant

if your while loop doesn't exit then the conditions are not met
1
2
3
4
5
while (comphealth > 0 && yourhealth > 0)
{
   yourchoiceagain();
   compattack();
}


You want the loop to end when one of the healths have depleted. Therefore, make the while condition so that both need to be above zero in order for it to execute.
ok why in this its not working....when the computer attacks its like totally diferent numbers then when i attack?
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void yourchoice()
{
    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    char yourname [20];

    srand(time(0));
    attack = rand() %21;
    shoot = rand() %23;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl;

}


void compattack()
{
    int comphealth;
    int yourhealth;
    int whatattack;
    int attack;


    srand(time(0));
    attack = rand() %21;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest!" << endl;

    yourhealth = yourhealth - attack;

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl;
}

void yourchoiceagain()
{
    int comphealth;
    int yourhealth;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    char yourname [20];

    srand(time(0));
    attack = rand() %21;
    shoot = rand() %23;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - 20;
        break;

        case 2:
        comphealth = comphealth - 15;
        break;

        case 3:
        yourhealth = yourhealth + 5;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl;

}

int main()
{
    char yourname [20];
    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;

    cout << "Enter your name: ";
    cin >> yourname;

    cout << yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    yourchoice();
    compattack();
    while(comphealth > 0 && yourhealth > 0)
    {
        yourchoiceagain();
        compattack();
    }

    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }
    cin.get();
    return 0;
}
The problem here is scope.

Try printing your health variables in each iteration of the loop. You'll probably find that they both remain at 175.
what do you mean?
you just have local variables. a local variable from one function is not visible in the other.
How do I change it to a non local variable
read into global varables

read into global varables


Or not. Global variables are best avoided. They may seem pretty harmless in smaller programs but can wreak havoc in larger scale ones. I would recommend looking at function return types and function parameters (both pass-by-value and pass-by-reference).
You re define new varibles in every function. The varibles you have only run the life of the function or block of code it runs. You declare health 4 times, which creates 4 SEPERATE, completely different varibles, becuase they were created in function, so they will run only through the life of the function. in c++ you can have 400 different varibles with the same name in different function, it might be complete hell to find errors, but c++ allows it. Try decalaring your varibles one in main, and passing them into functions like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Playerfight(int& x, int& y, int dmg);
int main()
{
    int health;
    int comphealth;
    int damage;
    int compdamage;
    char * name;
    Playerfight(health,comphealth,damage)
}

void Playerfight(int& x, int& y, int dmg)
{
    std::cout << "Your current health: " << x;
    std::cout << "Enemies Current health: " << y;
    y -= dmg;
    std::cout << "You did " << dmg << " Damage! Enemy now has " << y << " health left!\n";
}
Topic archived. No new replies allowed.