Program keeps crashing when i play it D:

I am almost done with my text game and I cannot figure out why it just started crashing. I cant post it here cause its too big (762 lines). I can get so far before it crashes:

i can do 2 for new game>1 for start battle, then i choose 1 for pistol and it crashes. It crashes randomly sometimes too i dont get any compiler errors it just stops working

her is the link to it on pastebin

http://pastebin.com/PDKM2Kus

Does anyone know why its crashing? oh and btw im goin gto fix the code, like the class has getters and setters, im going to get rid of those later i just want to get the program working.
Last edited on
Classes

Dinosaur 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
#ifndef DINOSAUR_H_INCLUDED
#define DINOSAUR_H_INCLUDED

#include "player.h"

/*
==============|
Dinosaur Class|
==============|
*/

class dinosaur : public player
{
    protected:
        int dinosaurHealth;
        int *attacks;

    public:
        void trex();
        void velociraptor();
        void flags();
        void arena();
        dinosaur();
        ~dinosaur();

        void set_dinosaurHealth(int DH);
        void set_attacks(int *A);

        int get_dinosaurHealth();
        int *get_attacks();

};

dinosaur::dinosaur(): dinosaurHealth(100), attacks(new int[0])
{

}

dinosaur::~dinosaur()
{
        if(attacks)
            delete[] attacks;
}

void dinosaur::set_dinosaurHealth(int DH)
{
    dinosaurHealth = DH;
}

void dinosaur::set_attacks(int *A)
{
        if(attacks)
            delete[] attacks;
    attacks = A;
}

//--------------------------------------------

int dinosaur::get_dinosaurHealth()
{
    return dinosaurHealth;
}

int *dinosaur::get_attacks()
{
    return attacks;
}

#endif // DINOSAUR_H_INCLUDED 



Player 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
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
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>
#include <string>

/*
============|
Player Class|
============|
*/

class player
{
    protected:
        int playerHealth;
        int pistolAmmo;
        int shotgunAmmo;
        int rifleAmmo;
        int score;
        int money;
        int healthPacks;
        int turns;
        int *items;

    public:
        void save();
        void load();
        void MainGame();
        void shop();
        void timer();
        void backpack();
        void stats();
        void dinoSelect();
        player();
        ~player();

        void set_health(int H);
        void set_pistolAmmo(int PA);
        void set_shotgunAmmo(int SA);
        void set_rifleAmmo(int RA);
        void set_score(int S);
        void set_money(int M);
        void set_items(int *I);
        void set_healthPacks(int HP);
        void set_turns(int T);

        int get_health();
        int get_pistolAmmo();
        int get_shotgunAmmo();
        int get_rifleAmmo();
        int get_score();
        int get_money();
        int *get_items();
        int get_healthPacks();
        int get_turns();
};

player::player(): playerHealth(100), pistolAmmo(9), shotgunAmmo(15),
                  rifleAmmo(25), score(0), money(100), healthPacks(1),
                  items(new int[4]), turns(0)
{
    for(int i = 0; i < 5; ++i)
    {
        items[i] = 0;
    }
}

player::~player()
{
        if(items)
                delete[] items;
}

void player::set_health(int H)
{
    playerHealth = H;
}

void player::set_pistolAmmo(int PA)
{
    pistolAmmo = PA;
}

void player::set_shotgunAmmo(int SA)
{
    shotgunAmmo = SA;
}

void player::set_rifleAmmo(int RA)
{
    rifleAmmo = RA;
}

void player::set_score(int S)
{
    score = S;
}

void player::set_money(int M)
{
    money = M;
}

void player::set_items(int *I)
{
    items = I;
}

void player::set_healthPacks(int HP)
{
    healthPacks = HP;
}

void player::set_turns(int T)
{
    turns = T;
}



int player::get_health()
{
    return playerHealth;
}

int player::get_pistolAmmo()
{
    return pistolAmmo;
}

int player::get_shotgunAmmo()
{
    return shotgunAmmo;
}

int player::get_rifleAmmo()
{
    return rifleAmmo;
}

int player::get_score()
{
    return score;
}

int player::get_money()
{
    return money;
}

int *player::get_items()
{
    return items;
}

int player::get_healthPacks()
{
    return healthPacks;
}

int player::get_turns()
{
    return turns;
}

#endif // PLAYER_H
It says

"This application has requested the Runtime to terminate in an unusual way. Please contact the applications support team for more information"
Looks like this would cause a problem sooner or later, you declare a dynamically allocated array with size 4 but you are trying to fill it with 5 elements.

1
2
3
4
5
6
7
8
9
player::player(): playerHealth(100), pistolAmmo(9), shotgunAmmo(15),
                  rifleAmmo(25), score(0), money(100), healthPacks(1),
                  items(new int[4]), turns(0)
{
    for(int i = 0; i < 5; ++i)
    {
        items[i] = 0;
    }
}


(This program runs fine for me on VS2008 though).
Last edited on
Does anyone know why its crashing? oh and btw im goin gto fix the code, like the class has getters and setters, im going to get rid of those later i just want to get the program working.


I suggest you implement the advice given in the other thread (btw don't create multiple threads for the same problem)

If you will do this this then It will be much easier to deal with a properly designed & organised application.

Last edited on
I will but i want this program functional first, i'd like to actually complete something for once in my life before i get distracted and never get around to it.
I know it feels bad when someone pours cold water over your project that you have probably spent quite a bit of time on, but the thing is you aren't the first person to have this happen, and certainly won't be the last.

The changes I suggested weren't that bad - you can cut & paste a lot of them, and I really think it would be worth your while. The stuff you learn here can be applied to any larger program you might do in the future.

Wouldn't you rather learn to design & organise properly? You might find that your problems go away when you do this.
ok I'll try to change everything but i'll need some help here and there. Once im guided through something its easier for me to understand. Also I stopped using using namespace std; and just did a search and replace and put the std:: in front of everything that belongs to the std namespace. what else do i need to do? make Trex and raptor their own classes? why is that thoug? i thought it was easier to make a generic class that every dinosaur could inherit from because theres going to be like 10 more in the future.
what else do i need to do? make Trex and raptor their own classes? why is that thoug? i thought it was easier to make a generic class that every dinosaur could inherit from because theres going to be like 10 more in the future.


You do make a generic dinosaur class, and put in it all the functions & variables that are common to all the dinosaurs. Then you derive whatever other dinosaurs from it - that way they can be their own objects, with their own data & behaviour.

At the moment your trex & raptor are just functions.

You don't have to put std:: before everything - you can do this for frequently used things:

1
2
3
4
5
6
7
//#include statements

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;


Have a read through what I said in the other thread, with regard to classes for the Shop, the Application (for saving &loading stuff). Also about what goes into .h and .cpp files.

Hope all goes well - look forward to seeing how you go.

It's nearly 11:30PM here in Melbourne, Australia - so it will be morning here before I reply again.
Last edited on
Ok well before i start making any changes i need it to actually work so i can test it out, so does anyone have any ideas?
closed account (D80DSL3A)
This could certainly be causing crashes:
1
2
3
4
5
6
void dinosaur::set_attacks(int *A)
{
        if(attacks)
            delete[] attacks;
    attacks = A;
}

I wonder how many times the same static array is deleted here?

I think I advised against using dynamic allocation (after someone recommended it). Don't try it unless you know what you're doing.
If I recall from other threads, this array is never used anyway as the values are all hard coded in the attack function.
Your issue may be here.
I changed some value and now everything works fine for some reason, very weird. But anyways thanks for the help. I have another problem I have an arena and its supposed to load the players ammo count and stuff but it loads whats pre defined in the player class and not from the file, if i view my backpack it displays the ammo and stats correctly that were loaded from the file but it wont do it in the arena for some reason. why is that? the code is too long for this site so its here on pastebin

http://pastebin.com/psNTSZWN
I don't know whether you had any boring teachers, who made boring statements like "You should design you application before you start coding it". It seems to me that you are now finding out the hard way why that is so.

Some more thoughts about how you can go about reorganising you project:

1. Start a new project - so you can keep your existing one.

2. Use the class wizard (does your IDE have one?) to make new classes, so it creates the .h & .cpp files for you - and learn how to get it to create function stubs in the .cpp file automatically. I name my classes with a leading C - among other things, it makes it easier to name objects - this is important to know beforehand because it affects the names of your class files. I also name member variables with a leading m_ , this also has advantages.

3. Copy & Paste the things you are going to keep, from the old project to your new one.

Now to think about Object Oriented design. It is not as easy as what people think - there are a number things to consider. I think of it like this:

1. Decide what objects you need, and what attributes (member variables) they need. A big concept is encapsulation - things to do with an object go in that objects class.

2. Decide on what relationships they need (inheritance for now). There are 3 types:
- "IS A" implies inheritance. A CTrex "IS A" CDinosaur;
- "HAS A" implies composition (An object contains another object) - the CBackPack has objects in it;
- "USES A" implies that an object is a parameter for a function. E.g. void Attack(CRaptor *Raptor)

3. Decide on how the various objects are going to interact with each other. This gives clues as to what functions (private, protected or public) are needed for each class, including access to member variables (private or protected only). Only provide what is necessary and don't have getters & setters.

4. Try to push functions & member variables as high up the tree as possible. Generalising the parameters helps with this - make use of polymorphism by making the parameters pointers to a base class.

5. Think about how the virtual functions are going to work - Pure virtual for interfaces, virtual functions for those that may not need to be redefined.

So for the objects (classes) I think you need. Obvious ones are CPlayer, CDinosaur, CTrex, CRaptor.
Also (not so obvious) CActor, CEnemy, CMainApp (for Loading & saving to files, menus etc), CGamePlay, CShop, CResource, CWeapon, CPistol, CRifle, CShotGun, CAmmo, CMediPack, CMoney, CBackPack, CStats

Right now, you are probably thinking that I have massively complicated the situation. On the contrary - your existing code is way too simplified, with a lot of things mixed together, and that is the cause of your problems IMO. Einstein said: "Things should be simple, simple as possible, but no simpler". A lot of people trip over the last part which really means that one has to have just enough complexity to deal with the complexity of the situation - making things too simple makes it worse.

With MediPacks & Ammo it is important to realise that that a player can have them, but might use them later. You could probably get away with having a MediPackCount variable which is decreased while the health is increased. However if this was a Board game where resources are lying around (to be acquired at some stage), then all the resources need to be objects as well.

With Ammo, you might get away with Name & Damage members, and use code to set values & decide how they are used, but it might be easier to make all 3 of them classes as well, because you can use constructors to set their various values, and the type helps decide how they are used.

One of the consequences of good encapsulation is that you should have fairly minimal code in your main.cpp file. There is a bit of a rule of thumb about the length of functions - they should fit onto 1 page (80 LOC say) - if they don't then one is not using enough functions.

So there is a fair bit of stuff to think about - can you decide what the inheritance trees are going to look like for the classes I mentioned above?

Good Luck !! :D




Topic archived. No new replies allowed.