Why does my int function execute before my main function?

The function I am specifically talking about is:

 
int showFight(variables);

On line 104 and 172.

And here is my full code:

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class Player
{
public:
    double currentHP = 30;
    double totalHP = getValues();
    double money = 0;
    int level = 1;
    int weapon = 0; // 0 unarmed
    double atk = getValues();
    double def = getValues();
    double exp = 0;
    string name;

    void showHUD()
    {
        system("cls");
        cout << name << endl;
        cout << "Level: " << level << endl;
        cout << "HP: " << currentHP << " / " << totalHP << endl;
        cout << "Money: $" << money << endl << endl;
    }

    int getValues()
    {
        if(level==1) {totalHP=30; atk=5; def=6;}
        if(level==2) {totalHP=45; atk=10; def=12;}
        if(level==3) {totalHP=75; atk=20; def=22;}
        if(level==4) {totalHP=100; atk=40; def=45;}
    }

    int lvlUp()
    {
        if(exp>=20) {level = 2;}
        if(exp>=50) {level = 3;}
        if(exp>=100) {level = 4;}
    }

    void showStats()
    {
        system("cls");
        cout << name << endl << endl;
        cout << "Level: " << level << endl;
        cout << "HP: " << currentHP << "/" << totalHP << endl;
        cout << "Money: $" << money << endl << endl;
        cout << "Attack: " << atk << endl;
        cout << "Defense: " << def << endl;
        cout << "Experience Points: " << exp << endl << endl;
        system("pause");
    }
};

class Items
{
public:
    double potion = 20;
    double superPotion = 40;
};

class Enemys
{
public:
    double slime = 15;
    double slimeATK = 2;
};



void showMain()
{
    cout << "1. Fight" << endl;
    cout << "2. Move Area" << endl;
    cout << "3. View Stats" << endl;
    cout << "4. Quit" << endl;
    cout << "5. DEBUG ADD MONEY" << endl;
    cout << "6. DEBUG -10 HP" << endl;
    cout << "7. DEBUG LEVEL UP" << endl;
    cout << "8. DEBUG GIVE POTION" << endl;
    cout << "Choice: ";
}

void showError()
{
    system("cls");
    cout << "ERROR ERROR YOU FUCKED UP ERRRO ERROR" << endl << endl;
    system("pause");
}

int healPlayer(int input, Player& user, Items& item)
{
    double heal;

    if(input==2) {heal=item.potion;}
    if(input==3) {heal=item.superPotion;}

    user.currentHP = user.currentHP + heal;
    if (user.currentHP>user.totalHP) {user.currentHP = user.totalHP;}
}

int showFight(int input, Player& user);

int main()
{
    Player user;
    Items item;
    bool quit = false;
    int menuInput;
    int healInput = 2; // 1 is spell | 2 potion | 3 Super potion
    bool fightWin = showFight(1, user);

    cout << "Enter Player Name: ";
    cin >> user.name;

    while(quit==false)
    {
        user.getValues();
        user.showHUD();
        showMain();
        cin >> menuInput;

        switch(menuInput)
        {
        case 1:
            // fight
            showFight(1, user);
            if(fightWin==true)
            {
                user.money = user.money + 5;
            }
            break;
        case 2:
            // move area
            break;
        case 3:
            // view stats
            user.showStats();
            break;
        case 4:
            system("cls");
            cout << "Quitting Game!" << endl;
            cout << "Thank you for playing!" << endl << endl;
            quit = true;
            break;
        case 5:
            user.money = user.money + 200;
            break;
        case 6:
            user.currentHP = user.currentHP - 10;
            break;
        case 7:
            // level up
            user.exp = user.exp + 10;
            user.lvlUp();
            break;
        case 8:
            // give potion
            healPlayer(healInput, user, item);
            break;
        default:
            showError();
            break;
        }
    }

    return 0;
}

int showFight(int input, Player& user)
{
    Items item;
    Enemys enemy;
    double badguy;
    double badguyATK;
    string badguyName;
    int menuInput;
    double userDamage = (user.def/2) - badguyATK;
    bool output;

    // get enemy
    if(input==1) {badguy = enemy.slime; badguyATK = enemy.slimeATK; badguyName = "Slime";}

    while(user.currentHP>=1 && badguy>=1)
    {
        system("cls");
        cout << user.name << "'s HP: " << user.currentHP << "/" << user.totalHP << endl;
        cout << badguyName << "'s HP: " << badguy << endl << endl;
        cout << "1. Fight" << endl;
        cout << "2. Use Item" << endl;
        cout << "Choice: ";
        cin >> menuInput;

        switch(menuInput)
        {
        case 1:
            // fight
            user.currentHP = user.currentHP - userDamage;
            badguy = badguy - user.atk;
            break;
        case 2:
            // item
            healPlayer(2, user, item);
            break;
        default:
            showError();
            break;
        }

        if(user.currentHP<=0)
        {
            system("cls");
            cout << "You Lost!" << endl << endl;
            system("pause");
            output = false;

            return output;
        }

        if(badguy<=0)
        {
            system("cls");
            cout << "You won!" << endl << endl;
            system("pause");
            output = true;

            return output;
        }
    }
}
Last edited on
Your question doesn't make any sense. It's impossible for any function to run before main. main is called automatically when the program starts. One of the first thing your main does is call showFight, so that's when showFight executes. If you want it to run later, then call it later.
When you say main function do you mean showMain?

You are calling showFight for the first time on line 113. showMain is not called until line 122.
tpb: When I build and run it does execute before my main function. It's not supposed to do that until the user selects "1" at the main menu in my main function.

Peter87: No I'm talking about "int main(){}"
Last edited on
tpb wrote:
It's impossible for any function to run before main.

Not if it's used to initialize a global variable.
When I build and run it does execute before my main function


NO IT DOESN'T. Did you hear that?

MAIN ALWAYS RUNS FIRST!!!!!!!!!!!!!!!!!!!!!!

IN YOUR MAIN YOU ARE CALLING showFight. THAT'S WHY IT RUNS!!!!!!!!!!!!!!!!!!!!!!!!!
IF YOU WANT IT TO RUN AT A LATER POINT, THEN CALL IT AT A LATER POINT!!!!!!!!!!!!!!!!
I don't understand your question because main() is the starting point for every program.

You call showfight() on line 113, perhaps this call is in the wrong location.

Also you appear to have several warnings that you should strive to fix.

main.cpp||In member function ‘int Player::getValues()’:|
main.cpp|34|warning: no return statement in function returning non-void [-Wreturn-type]|
main.cpp||In member function ‘int Player::lvlUp()’:|
main.cpp|41|warning: no return statement in function returning non-void [-Wreturn-type]|
main.cpp||In function ‘int healPlayer(int, Player&, Items&)’:|
main.cpp|102|warning: no return statement in function returning non-void [-Wreturn-type]|
main.cpp||In function ‘int showFight(int, Player&)’:|
main.cpp|232|warning: control reaches end of non-void function [-Wreturn-type]|
main.cpp|180|warning: ‘badguyATK’ is used uninitialized in this function [-Wuninitialized]|


Clearly I'm not getting it. Where am I calling showFight? And how can I fix this?

jlb: I didn't realize when declaring that variable I was also calling on showFight.
Last edited on
Also you appear to have several warnings that you should strive to fix.

If you are using GCC/MinGW or Clang I strongly recommend that you at least use the -Wall compiler flag. It gives you a lot of useful warning messages when making simple mistakes like this.

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall
Last edited on
Where am I calling showFight?

On lines 113 and 129.

how can I fix this?

If you don't want to call the function on line 113, then don't.

Note that you store the return value of line 113 call in variable fightWin.
You don't store the return value of line 129 call at all.
On line 130 you do use the value of variable fightWin.

Note that your showFight returns an int. However, all exit paths of the function do not return a value and the paths that do, do return a bool value. Why the function does not return a bool?
Topic archived. No new replies allowed.