c++ saved input

I'm having trouble with my no_mana function i want i to stop the player from enetering the skill menu becuase he is out of mana. Even tho it does what i wanted if i input 2 after the MP reaches 0 and then i input 1,3,4. It still lets the player enter the skill menu. I think this has to do with me returning combatMenu() in the no mana function but i'm not sure.
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
class BattleMenu {
public:
    int HeroMaxHP=100;
    int HeroMaxMP = 10;
    int EnemyMaxHP=100;
    int Attack = 15;
    int EAttack = 10;
    int user_input;
    string current_turn="Hero";
    void combatMenu()
    {
        while (true) {
            if (current_turn == "Hero") {
                cout << "Your turn!" << endl;
                cout << "Enemy HP: " << EnemyMaxHP << endl << endl << endl;
                cout << "Hero HP:" << HeroMaxHP << "      ";
                cout << "Hero MP:" << HeroMaxMP << endl;
                cout << "1. Attack" << " " << Attack << endl;
                cout << "2. Skill" << /*Skill <<*/endl;
                cout << "3. Deffend" << /*Defend<<*/endl;
                cout << "4. Item" <</*Item<<*/endl;
                cin >> user_input;
                switch (user_input) {
                case 1:
                    EnemyMaxHP = EnemyMaxHP - Attack;
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl << endl;
                    break;
                case 2:
                {
                    no_mana();
                    SkillMenu();
                    system("cls");
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl;
                }
                    break;
                case 3:
                    //HeroMaxHP - (EnemyAttack - Armor) = HeroMaxHp;
                    break;
                case 4:
                    ItemMenu();
                    system("cls");
                    break;
                }
                break;

            }
            else if(current_turn == "Enemy");
            {
                cout << endl;
                cout << "Enemy turn " << endl;
                HeroMaxHP = HeroMaxHP - EAttack;
                cout << "You lost: " << HeroMaxHP << "HP" << endl << endl;
                break;
            }
        }
    }
   
    void SkillMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your skills"<<endl;
            cout << "1.Skill Name"<<endl;
            cout << "2.Skill Name"<<endl;
            cout << "3.Skill Name"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Skill_Name";
                EnemyMaxHP = EnemyMaxHP - 30;
                HeroMaxMP = HeroMaxMP - 5;
                break;
            case 2:
                cout << "2.Skill_Name";
                break;
            case 3:
                cout << "1.Skill_Name";
                break;
            }
            break;
        }
    }
    void ItemMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your itmes"<<endl;
            cout << "1.Health Potion"<<endl;
            cout << "2.Mana Potion"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Item_Name";
                break;
            case 2:
                cout << "2.Item_Name";
                break;
            case 3:
                cout << "1.Item_Name";
                break;
            }
            break;
        }
    }
    void change()
    {
        if (current_turn == "Hero") current_turn = "Enemy";
        else current_turn = "Hero";
    }
    void check_for_winner()
    {
        if (HeroMaxHP <= 0)
        {
            cout << "Hero lost" << endl;
            exit(0);
        }
        if (EnemyMaxHP <= 0)
        {
            cout << "Enemy lost" << endl;
            

        }
    }
    void no_mana() {
        if (HeroMaxMP <= 0) {
            cout << "You are out of mana"<<endl;
            return combatMenu();
            system("cls");
        }
    }
    BattleMenu()
    {
        while (true)
        {
            combatMenu();
            check_for_winner();
            change();
           
        }
        
    }
};

int main()
{
    BattleMenu Game;
    return 0;
}
you have some weirdness... nomana returns a value but is a void function so that does nothing and should warn you about it.

what I would do is maybe something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool no_mana()
{
     if (HeroMaxMP <= 0) 
    {
      //other code
      return true; //out of mana
     }
    return false; //not out of mana
}

...

 case 2:
                {
                    if(!no_mana())
                    SkillMenu();


but you may need to adjust the idea since you wanted some result from the other menu in no-mana... tinker with the idea until it all works together
Last edited on
I did something close to this one bu the values such as hp of the hero and enemy change what i don't want
Last edited on
I can only image what went wrong with it. If you have some new code, post it, and we can look again... its no trouble... just start a new post (in this thread) with the updated code.
Last edited on
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
class BattleMenu {
public:
    int HeroMaxHP=100;
    int HeroMaxMP = 10;
    int EnemyMaxHP=100;
    int Attack = 15;
    int EAttack = 10;
    int user_input;
    string current_turn="Hero";
    void combatMenu()
    {
        while (true) {
            if (current_turn == "Hero") {
                cout << "Your turn!" << endl;
                cout << "Enemy HP: " << EnemyMaxHP << endl << endl << endl;
                cout << "Hero HP:" << HeroMaxHP << "      ";
                cout << "Hero MP:" << HeroMaxMP << endl;
                cout << "1. Attack" << " " << Attack << endl;
                cout << "2. Skill" << /*Skill <<*/endl;
                cout << "3. Deffend" << /*Defend<<*/endl;
                cout << "4. Item" <</*Item<<*/endl;
                cin >> user_input;
                switch (user_input) {
                case 1: {
                    EnemyMaxHP = EnemyMaxHP - Attack;
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl << endl;
                }
                    break;
                case 2:
                {
                    no_mana();
                    if (!no_mana()) {
                        SkillMenu();
                    }
                    else {
                        cout << "You are out of mana" << endl;
                    }
                   
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl;
                }
                    break;
                case 3:{}
                    //HeroMaxHP - (EnemyAttack - Armor) = HeroMaxHp;
                    break;
                case 4: {
                    ItemMenu();
                    system("cls");
                }
                    break;
                }
                break;

            }
            else if(current_turn == "Enemy");
            {
                cout << endl;
                cout << "Enemy turn " << endl;
                HeroMaxHP = HeroMaxHP - EAttack;
                cout << "You lost: " << HeroMaxHP << "HP" << endl << endl;
                break;
            }
        }
    }
   
    void SkillMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your skills"<<endl;
            cout << "1.Skill Name"<<endl;
            cout << "2.Skill Name"<<endl;
            cout << "3.Skill Name"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Skill_Name";
                EnemyMaxHP = EnemyMaxHP - 30;
                HeroMaxMP = HeroMaxMP - 5;
                break;
            case 2:
                cout << "2.Skill_Name";
                break;
            case 3:
                cout << "1.Skill_Name";
                break;
            }
            break;
        }
    }
    void ItemMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your itmes"<<endl;
            cout << "1.Health Potion"<<endl;
            cout << "2.Mana Potion"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Item_Name";
                break;
            case 2:
                cout << "2.Item_Name";
                break;
            case 3:
                cout << "1.Item_Name";
                break;
            }
            break;
        }
    }
    void change()
    {
        if (current_turn == "Hero") current_turn = "Enemy";
        else current_turn = "Hero";
    }
    void check_for_winner()
    {
        if (HeroMaxHP <= 0)
        {
            cout << "Hero lost" << endl;
            exit(0);
        }
        if (EnemyMaxHP <= 0)
        {
            cout << "Enemy lost" << endl;
            

        }
    }
    bool no_mana() {
        if (HeroMaxMP <= 0) {

            return true;
        }
        else
            return false;
    }
    BattleMenu()
    {
        while (true)
        {
            combatMenu();
            check_for_winner();
            change();
           
        }
        
    }
};

int main()
{
    BattleMenu Game;
    return 0;
}

Pretty much the same code added the bool works well , but the values of the heros hp keep changing if i keep pressing 2 what i want to avoid
remove line 36. Then see if you understand why.
Also a small problem on line 59.

Compiler told me!

The compiler is your friend, turn on all the warnings, so it tells you where you went wrong.

Also a good idea to have a default case in your switches. A quit option would be good too.

Some of your functions are too long.

I didn't notic line 36 but added continue on line 41 and now all works fine
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
class BattleMenu {
public:
    int HeroMaxHP=100;
    int HeroMaxMP = 10;
    int EnemyMaxHP=100;
    int Attack = 15;
    int EAttack = 10;
    int user_input;
    string current_turn="Hero";
    void combatMenu()
    {
        while (true) {
            if (current_turn == "Hero") {
                cout << "Your turn!" << endl;
                cout << "Enemy HP: " << EnemyMaxHP << endl << endl << endl;
                cout << "Hero HP:" << HeroMaxHP << "      ";
                cout << "Hero MP:" << HeroMaxMP << endl;
                cout << "1. Attack" << " " << Attack << endl;
                cout << "2. Skill" << /*Skill <<*/endl;
                cout << "3. Deffend" << /*Defend<<*/endl;
                cout << "4. Item" <</*Item<<*/endl;
                cin >> user_input;
                switch (user_input) {
                case 1: {
                    EnemyMaxHP = EnemyMaxHP - Attack;
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl << endl;
                }
                    break;
                case 2:
                {
                    if (!no_mana()) {
                        SkillMenu();
                    }
                    else {
                        cout << "You are out of mana" << endl;
                        continue;
                    }
                   
                    cout << "Enemy lost: " << EnemyMaxHP << "HP" << endl;
                }
                    break;
                case 3:{}
                    //HeroMaxHP - (EnemyAttack - Armor) = HeroMaxHp;
                    break;
                case 4: {
                    ItemMenu();
                    system("cls");
                }
                    break;
                }
                break;
            }
            else if(current_turn == "Enemy");
            {
                cout << endl;
                cout << "Enemy turn " << endl;
                HeroMaxHP = HeroMaxHP - EAttack;
                cout << "You lost: " << HeroMaxHP << "HP" << endl << endl;
                break;
            }
        }
    }
   
    void SkillMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your skills"<<endl;
            cout << "1.Skill Name"<<endl;
            cout << "2.Skill Name"<<endl;
            cout << "3.Skill Name"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Skill_Name";
                EnemyMaxHP = EnemyMaxHP - 30;
                HeroMaxMP = HeroMaxMP - 5;
                break;
            case 2:
                cout << "2.Skill_Name";
                break;
            case 3:
                cout << "1.Skill_Name";
                break;
            }
            break;
        }
    }
    void ItemMenu()
    {
        system("cls");
        while (true) {
            cout << "Pick on of your itmes"<<endl;
            cout << "1.Health Potion"<<endl;
            cout << "2.Mana Potion"<<endl;
            cin >> user_input;
            switch (user_input)
            {
            case 1:
                cout << "1.Item_Name";
                break;
            case 2:
                cout << "2.Item_Name";
                break;
            case 3:
                cout << "1.Item_Name";
                break;
            }
            break;
        }
    }
    void change()
    {
        if (current_turn == "Hero") current_turn = "Enemy";
        else current_turn = "Hero";
    }
    void check_for_winner()
    {
        if (HeroMaxHP <= 0)
        {
            cout << "Hero lost" << endl;
            exit(0);
        }
        if (EnemyMaxHP <= 0)
        {
            cout << "Enemy lost" << endl;
            

        }
    }
    bool no_mana() {
        if (HeroMaxMP <= 0) {

            return true;
        }
        else
            return false;
    }
    BattleMenu()
    {
        while (true)
        {
            combatMenu();
            check_for_winner();
            change();
           
        }
        
    }
};

int main()
{
    BattleMenu Game;
    return 0;
}
And what was the warning for line 58 that I mentioned?

One can compile on this site by using the Edit/Run button next to the code, turn all the warnings on.
Last edited on
Small mistake on my side on the if statment. After using python i got used to add if(statment):
avoid exit(0). It is not hurting anything here, but it is bad practice to abuse it.

exit is meant to end the program with an error code in case of unrecoverable error detected. You should try to wire the program so that it gets back to main and returns (0) from there as a 'normal program exit' approach.
later, you will encounter terminate() which is like exit except it attempts to clean up first. Like exit, terminate is for errors, not normal operations.
the function of ItemMenu does not have branch to deal, when you intpu 4 .
Topic archived. No new replies allowed.