Why does this happen?

In the class selection If I type something other than y/n it repeats the loop 5 times. I can't seem to figure out why, all I know is that it does the loop 5 times skipping all the choices and repeating the class options.

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

    void classes(){
        for(int x2 = 0; x2<5;){

        cout << "\n";
        cout << "[1] Heavy Warrior\n" << "Stats: \n" << "Health: 150 \n" << "Attack: 5 \n" << "Defence: 15 \n" << "Dodge Chance: 5%"<< endl;
        cout << "\n";
        cout << "[2] Light Warrior\n" << "Stats: \n" << "Health: 125 \n" << "Attack: 10 \n" << "Defence: 10 \n" << "Dodge Chance: 10%" << endl;
        cout << "\n";
        cout << "[3] Archer\n" << "Stats: \n" << "Health: 100\n" << "Attack: 15 \n" << "Defence: 5 \n" << "Dodge Chance: 15%"<< endl;
        cout << "\n";
        cout << "Choose a class: ";
        cin >> choice;
    switch (choice){
        case '1':
            system("cls");
            cout << "Are you sure you want to be a Heavy Warrior? y/n" << endl;
            cin >> choice;
    switch (choice){
        case 'Y':
        case 'y':
            system("cls");
            cout << "Your class is now set to Heavy Warrior" << endl;
            playerHP = 150;
            attack = 5;
            defence = 15;
            dodgechance = 5;
            level = 1;
            exp = 0;
            x2 = 51;
            cout << "\n";
            cout << "Name: \n" << name << "Class: Heavy Warrior \n" << "Attack: " << attack << "\nDefence: " << defence << "\nDodge Chance: " << dodgechance << "%" << endl;
            break;
        case 'N':
        case 'n':
            x2 = 0;
            break;
        default:
            x2 = 0;
            break;
} //nested switch
            break;
        case '2':
            system("cls");
            cout << "Are you sure you want to be a Light Warrior? y/n" << endl;
            cin >> choice;
    switch (choice){
        case 'Y':
        case 'y':
            system("cls");
            cout << "Your class is now set to Light Warrior" << endl;
            playerHP = 125;
            attack = 10;
            defence = 10;
            dodgechance = 10;
            level = 1;
            exp = 0;
            x2 = 51;
            cout << "\n";
            cout << "Name: \n" << name << "Class: Light Warrior \n" << "Attack: " << attack << "\nDefence: " << defence << "\nDodge Chance: " << dodgechance << "%" << endl;
            break;
        case 'N':
        case 'n':
            x2 = 0;
            break;
        default:
            x2 = 0;
            break;
} // nested switch
            break;
        case '3':
            system("cls");
            cout << "Are you sure you want to be a Archer? y/n" << endl;
            cin >> choice;
    switch (choice){
        case 'Y':
        case 'y':
            system("cls");
            cout << "Your class is now set to Archer" << endl;
            playerHP = 100;
            attack = 15;
            defence = 5;
            dodgechance = 15;
            level = 1;
            exp = 0;
            x2 = 51;
            cout << "\n";
            cout << "Name: \n" << name << "Class: Archer \n" << "Attack: " << attack << "\nDefence: " << defence << "\nDodge Chance: " << dodgechance << "%" << endl;
            break;
        case 'N':
        case 'n':
            x2 = 0;
            break;
        default:
            x2 = 0;
            break;
}
        default:
            x2 = 0;
            break;

} //main switch
} //loop
} // function classes
that is a really ill-formed for loop and the program should have proper indentation.
1
2
3
4
5
6
7
8
9
10
int main()
{
    for()
    {
        if()
        {
            stuff
        }
    }
}


It is pointless to use a for loop like that where it is always equal to zero except one other case where you set it equal to 51 , why 51 anyways???

You should instead use a while statement. while( x2 == 0 )
Topic archived. No new replies allowed.