Floating Point Exception?

So I'm still pretty new to C++ and I'm trying to make a text based RPG. I just started and have been trying to work out the fighting system to be object oriented.

I get a runtime error from it that says "Floating point exception (core dumped) Process returned 136 (0x88)" as soon as you try to attack. any ideas why?

I realize this is very long and probably not programmed very well, but I'm still just starting out. Any help would be greatly appreciated!

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

using namespace std;

void battlewin();
void newgame();

bool bw = false;

void RANDSEED()
{
    srand (time(NULL));
}

class userloc{
    private:
        int x, y, maxx, maxy;
    public:
        void set_map(int a, int b)
        {
            maxx = a;
            maxy = b;
            cout << "Map size is: " << maxx << "x" << maxy << ".\n";
        }
} map;

class mob{};


class player: public mob{
    public:
        int gold, hp, maxhp, attack;
        string name;
        void newchar()
        {
            hp = 100;
            maxhp = 100;
            attack = 10;
            gold = 0;
        }
        void deathchk()
        {
            if(hp<=0)
            {
                cout << "You have died!\n";
                cout << "Press any button to exit.";
                cin.ignore();
                exit(0);
            }
        }
        void takedmg(int atk)
        {
            int dmg;
            dmg = rand() % atk + 1;
            hp-=dmg;
            cout << "The mob has hit you for " << dmg << endl;
            cout << "You have " << hp << "/" << maxhp << " HP." << endl;
            deathchk();
        }
} user;

class enemy: public mob{
    public:
        int goldvalue, hp, attack;
        void deathchk()
        {
            if(hp<=0)
            {
                battlewin();
            }
        }
        void takedmg(int atk)
        {
            int dmg;
            dmg = rand() % atk + 1;
            hp-=dmg;
            cout << "You hit the mob for " << dmg << endl;
            cout << "Mob has " << hp << " HP." << endl;
            deathchk();
        }
        void spawn(int o, int t, int th)
        {
            hp = rand() % o + 1;
            attack = rand() % t + 1;
            goldvalue = rand() % th + 1;
        }
};

void newgame()
{
    user.newchar();
}

void battlewin()
{
    cout << "Congratulations! You won the battle.";
    cout << "You have " << user.hp << "/" << user.maxhp << " HP.\n";
    bw = true;
}


void fight()
{
    enemy i;
    i.spawn(50, 10, 10);
    while(bw==false)
    {
        int choice;
        cout << "Press 1 to attack. ";
        cin >> choice;
        if(choice==1)
        {
            i.takedmg(user.attack);
            if(bw==false)
            {
                user.takedmg(i.attack);
            }
        }
    }
    bw=false; // if you're here you won
    user.gold+=i.goldvalue;
    cout << "Mob dropped " << i.goldvalue << " gold!" << endl;
}

int main()
{
    RANDSEED();
    player user;
    user.newchar();
    cout << "Please enter your name: ";
    getline(cin,user.name);
    cout << "Welcome, " << user.name << " to RPG fight!\n";
    fight();
    cout << bw;
    return 0;
}
Last edited on
This happens because you have the user defined twice in your code. Once in line 62 in the global scope and once in line 130 in the scope of function main. When you call newchar() in line 131 it operates on the second "user" variable, while one in global scope remains uninitialized. After that everywhere you use the "user" from the global scope, which leads to the exception here: dmg = rand() % atk + 1; because the atk variable happened to be initialized with 0 (which you should never rely on). So to solve your current problem, remove line 130, so that the global user gets initialized in line 131.

As you said, this code is far from being perfect, so I just concentrated on the question you had. To find this kind of bugs - use a debugger to see what your program does step-by-step (I assume you work in an IDE, and pretty much all of them have this functionality).

Good luck with learning C++ :)
dmg = rand() % atk + 1; and similar.

atk is zero. The modulo operator is meaningless applied mod zero

e.g. 7 % 0 causes a problem

Try dmg = rand() % (atk + 1); and similar.
Last edited on
Thanks for the replies. They were both helpful and I will try these solutions out later today when I have time. I'll let you know how they work out.
Thanks guys! I followed both of your advice and the exception error went away.
Topic archived. No new replies allowed.