Infinite Loop

Below is a function to a program I am writing. The main program and everything else is not included due to the size of it, so I know some functions there will not make sense.
For some unknown reason to me, the program goes into an infinite loop when it reaches "Boss does not have enough mana." Why is that? I assumed that the switch statement will receive a new parameter(x) when the program falls under the else statement. Thank you.
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
#include <time.h>
using namespace std;
void boss::bossAttack(info & in)
{
    srand(time(NULL));
    bool move = true;
    int x = rand() % 3 + 1;
    while (move)
    {
        switch(x)
        {
            case 1:
            {
                bossDamage = 20;
                cout << "Boss has Attacked! - " << bossDamage << endl;
                int i = in.getHealth() - bossDamage;
                in.setHealth(i);
                move = false;
                break;
            }
            case 2:
            {
                if(bossMana >= 10)
                {
                    bossDamage = 40;
                    cout << "Boss used magic! - " << bossDamage << endl;
                    int i = in.getHealth() - bossDamage;
                    in.setHealth(i);
                    bossMana = bossMana - 10;
                    move = false;
                }
                else
                {
                    cout << "Boss does not have enough mana!" << endl; //Loops
                    int x = 1;
                }
                break;
            }
            case 3:
            {
                if(bossMana >= 20)
                {
                    cout << "Boss used heal!" << endl;
                    int i = in.getEnemyHealth() + 30;
                    in.setEnemyHealth(i);
                    bossMana = bossMana - 20;
                    move = false;
                }
                else
                {
                    cout << "Boss does not have enough mana!" << endl; //Loops
                    int x = rand() % 2 + 1;
                }
                break;
                    
            }
        }
    }
}

On line 52, take out the word int. What's happening is line 52 is defining a new variable called x within the scope of that else block, and it "covers up" the x you're using in the rest of the program. Same thing is happening in the else block of case 2:
Last edited on
On line 35 you are creating a whole new variable, named x, and setting it to 1. This whole new variable ceases to exist when the else block is finished.

The original x variable is untouched. This is called shadowing.

If you change line 35 to x = 1; so that you are not creating a whole new variable, then the x in question will be the original x and it will work how you expect.
-_- that is such a beginner error.
Thanks guys.
I think that x on line 35 should be a random int because if x = 1 then switch(x) will be case 1 forever. Am I wrong?
I think that x on line 35 should be a random int because if x = 1 then switch(x) will be case 1 forever. Am I wrong?

Under the code block for case 1, move is set to false, so the while loop exits.
Last edited on
Topic archived. No new replies allowed.