Reset Scope with pointers

sup does, how did I reset my switch...case in the classChoose scope? Like, I need to return the scope to the beginnin' in case of type an invalid class.

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
  #include <iostream>
#include <string>

class character {
    public:
        std::string name;
        std::string plclass;
        int level;
        int health;
};

class attributes {
    public:
        int vita;
        int endu;
        int stre;
        int dext;
        int resi;
        int inte;
        int fait;
};

void nameChoose(character *pname){
    std::cout << "Type your name brave: ";
    std::cin >> pname->name;
}

void classChoose(character *pclass){
    
    int nclass;
    
    std::cout << "Between this calsses:" << std::endl;
    std::cout << "[1]Human;\n[2]Orc;\n[3]Elf;\n[4]Hobbit;\n[5]Dwarf;\n[6]Pandaren;\n[7]Half-Demon;" << std::endl;
    std::cout << "Which you'll choose? ";
    
    std::cin >> nclass;
    
    switch (nclass){
        case 1:
            pclass->plclass = "Human";
            break;
        
        case 2:
            pclass->plclass = "Orc";
            break;
            
        case 3:
            pclass->plclass = "Elf";
            break;
            
        case 4:
            pclass->plclass = "Hobbit";
            break;
            
        case 5:
            pclass->plclass = "Dwarf";
            break;
            
        case 6:
            pclass->plclass = "Pandaren";
            break;
            
        case 7:
            pclass->plclass = "Half-Demon";
            break;
            
        default:
            pclass->plclass = "Invalid class.";
            break;
    }

}

int main(){
    
    character *player = new character;
    player->level = 1;
    player->health = 100;
    
    nameChoose(player);
    classChoose(player);
    
    std::cout << "\n\n\n--------Player Infos--------\n"; //Can be deleted
    std::cout << "Name: " << player->name << std::endl;
    std::cout << "Class: " <<player->plclass << std::endl;
    std::cout << "Level: " << player->level << std::endl;
    std::cout << "HP: " << player->health << std::endl;
    
    return 0;
}
closed account (E0p9LyTq)
Create a "correct input" bool variable set to false, wrap your switch block in a while/do-while loop block checking the status of the input bool variable.

When you have a valid input, change the input bool variable to true and the loop terminates.

You could change the default: case terminator from break; to continue to make the overall logic more apparent, but not needed.
You could use a while-loop in function classChoose() which will repeat until a valid input is received, then the loop will end.

http://www.cplusplus.com/doc/tutorial/control/
Thanks guys, really helped, and worked perfectly, but I'm not certainly about the structure, is that right?

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
#include <iostream>
#include <string>
#include <cstdlib>

class character {
    public:
        std::string name;
        std::string plclass;
        int level;
        int health;
};

class attributes {
    public:
        int vita;
        int endu;
        int stre;
        int dext;
        int resi;
        int inte;
        int fait;
};

void nameChoose(character *pname){
    std::cout << "Type your name brave: ";
    std::cin >> pname->name;
}

void classChoose(character *pclass){
    
    int nclass;
    bool verify = false;
    
    do {
        
        system("cls");
        
        std::cout << "Between this calsses:" << std::endl;
        std::cout << "[1]Human;\n[2]Orc;\n[3]Elf;\n[4]Hobbit;\n[5]Dwarf;\n[6]Pandaren;\n[7]Half-Demon;" << std::endl;
        std::cout << "Which you'll choose? ";
        
        std::cin >> nclass;
            
            
        switch (nclass){
            case 1:
                pclass->plclass = "Human";
                verify = true;
                break;
            
            case 2:
                pclass->plclass = "Orc";
                verify = true;
                break;
                
            case 3:
                pclass->plclass = "Elf";
                verify = true;
                break;
                
            case 4:
                pclass->plclass = "Hobbit";
                verify = true;
                break;
                
            case 5:
                pclass->plclass = "Dwarf";
                verify = true;
                break;
                
            case 6:
                pclass->plclass = "Pandaren";
                verify = true;
                break;
                
            case 7:
                pclass->plclass = "Half-Demon";
                verify = true;
                break;
                
            default:
                continue;
        }
    } while (verify == false);
    
}

int main(){
    
    character *player = new character;
    player->level = 1;
    player->health = 100;
    
    nameChoose(player);
    classChoose(player);
    
    std::cout << "\n\n\n--------Player Infos--------\n"; //Can be deleted
    std::cout << "Name: " << player->name << std::endl;
    std::cout << "Class: " <<player->plclass << std::endl;
    std::cout << "Level: " << player->level << std::endl;
    std::cout << "HP: " << player->health << std::endl;
    
    system("pause");    
    
    return 0;
}
The structure looks ok, so long as it does what is intended, it does look reasonable. Pretty good if you are not used to loops like this.

One very minor comment, at line 84, rather than while (verify == false) it is perhaps more usual to write while (!verify) (note the ! which means not).

Similarly the opposite condition, if appropriate would be simply while (verify) rather than while (verify == true). At least you should be aware of this style, in order to read and understand other code you come across.
Last edited on
Cool, that seems to work now =D, thanks for the help guys, helps a lot =)
Topic archived. No new replies allowed.