Program crashing at startup
Apr 13, 2009 at 9:30pm UTC
So I'm doing a test app in the console for an rpg inventory system befor I move into the GUI zone and after debugging all the code, it starts up, and then crashes >.> I've went through and fixed the reasons I saw it may have crashed it, but it still is not working, thanks in advance for any help, code attached.
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
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Invintory {
public :
int TSpaces;
int FSpaces;
int NItems[];
string Items[];
void AddItem(int *ItoAdd){
ItoAdd++;
};
void DropItem(int *ItoDrop){
ItoDrop++;
};
void UseItem(int *ItoUse){
ItoUse--;
};
};
int main(int argc, char *argv[])
{
string Input;
Invintory PInv;
PInv.TSpaces = 16;
PInv.FSpaces = 0;
PInv.NItems[6];
PInv.Items[6];
PInv.Items[0] = "Staff" ;
PInv.NItems[0] = 0;
PInv.Items[1] = "Sword" ;
PInv.NItems[1] = 0;
PInv.Items[2] = "Shield" ;
PInv.NItems[2] = 0;
PInv.Items[3] = "Gun" ;
PInv.NItems[3] = 0;
PInv.Items[4] = "Bracers" ;
PInv.NItems[4] = 0;
PInv.Items[5] = "Potion" ;
PInv.NItems[5] = 0;
cout << "What is your request?" ;
while (Input != "Stop" ){
cin.clear();
cout << "Well?" << endl;
cin >> Input;
if (Input == "Help" ){
cout << "Type 'Sword' to get a sword" << endl;
cout << "Type 'Staff' to get a staff" << endl;
cout << "Type 'Shield' to get a sheild" << endl;
cout << "Type 'Gun' to get a gun" << endl;
cout << "Type 'Bracers' to get bracers" << endl;
cout << "Type 'Potion' to ge a potion" << endl;
cout << "Type 'Inv' to see your invintory" << endl;
} else if (Input == "Sword" ){
PInv.AddItem(&PInv.NItems[1]);
PInv.FSpaces++;
} else if (Input == "Staff" ){
PInv.AddItem(&PInv.NItems[0]);
PInv.FSpaces++;
} else if (Input == "Sheild" ){
PInv.AddItem(&PInv.NItems[2]);
PInv.FSpaces++;
} else if (Input == "Gun" ){
PInv.AddItem(&PInv.NItems[3]);
PInv.FSpaces++;
} else if (Input == "Bracers" ){
PInv.AddItem(&PInv.NItems[4]);
PInv.FSpaces++;
} else if (Input == "Potion" ){
PInv.AddItem(&PInv.NItems[5]);
if (PInv.NItems[5] == 0){
PInv.FSpaces++;
};
} else if (Input == "Inv" ){
cout << "You have" << PInv.TSpaces << "slots in your invintory." << endl;
cout << "You have" << (PInv.TSpaces - PInv.FSpaces) << "spaces free." << endl;
if (PInv.NItems[0] > 0){
cout << "You have" << PInv.NItems[0] << PInv.Items[0] << endl;
} else if (PInv.NItems[1] > 0){
cout << "You have" << PInv.NItems[1] << PInv.Items[1] << endl;
} else if (PInv.NItems[2] > 0){
cout << "You have" << PInv.NItems[2] << PInv.Items[2] << endl;
} else if (PInv.NItems[3] > 0){
cout << "You have" << PInv.NItems[3] << PInv.Items[3] << endl;
} else if (PInv.NItems[4] > 0){
cout << "You have" << PInv.NItems[4] << PInv.Items[4] << endl;
} else if (PInv.NItems[5] > 0){
cout << "You have" << PInv.NItems[5] << PInv.Items[5] << endl;
};
};
system("PAUSE" );
};
return 0;
}
Last edited on Apr 13, 2009 at 9:32pm UTC
Apr 13, 2009 at 9:36pm UTC
You need to give your arrays a size. Empty brakets are no good:
1 2 3 4 5 6
class Invintory {
public :
int TSpaces;
int FSpaces;
int NItems[6]; // or more than 6 if you need more
string Items[6]; // ditto
If you need a variable size (grows as you get more items), use a vector:
1 2 3 4 5 6 7 8 9 10
vector<string> Items;
// to add a new item
Items.push_back("NewItem" );
// to see how many items they have
size_t totalitems = Items.size();
// to get a specific item:
string s = Items[ 2 ]; // be sure Items.size() is > 2! or Bad Things happen
Apr 14, 2009 at 12:56am UTC
Thanks man, that worked! Other bugs are popping up (add items isn't working >.>) but I think I can figure that out, I'll have to look up vectors and such.
Edit: I did some tinkering, and it mostly works, but I can't figure out how to get AddItem to not return the Hex address of the variable going in >.> Once again muchas gracias to any help received.
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
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
typedef int **ItemFunc;
typedef int *OtherIF;
int StacksP = 0;
class Invintory {
public :
int TSpaces;
int FSpaces;
ItemFunc NItems[6];
string Items[6];
void AddItem(ItemFunc &ItoAdd){
ItemFunc ItoAdd2 = new OtherIF[6];
ItoAdd2++;
ItoAdd = ItoAdd2;
};
void DropItem(int *ItoDrop[]){
*ItoDrop++;
};
void UseItem(int *ItoUse[]){
*ItoUse--;
};
//bool Stacks(ItemFunc &ItoStack)
};
int main(int argc, char *argv[])
{
string Input;
Invintory PInv;
PInv.TSpaces = 16;
PInv.FSpaces = 0;
PInv.NItems[6];
PInv.Items[6];
PInv.Items[0] = "Staff" ;
PInv.NItems[0] = 0;
PInv.Items[1] = "Sword" ;
PInv.NItems[1] = 0;
PInv.Items[2] = "Shield" ;
PInv.NItems[2] = 0;
PInv.Items[3] = "Gun" ;
PInv.NItems[3] = 0;
PInv.Items[4] = "Bracers" ;
PInv.NItems[4] = 0;
PInv.Items[5] = "Potion" ;
PInv.NItems[5] = 0;
cout << "What is your request?" << endl << "Pro-Tip: Help" << endl;
while (Input != "Stop" ){
cin.clear();
cout << "Well?" ;
cin >> Input;
if (Input == "Help" || Input == "help" ){
cout << "Type 'Sword' to get a sword" << endl;
cout << "Type 'Staff' to get a staff" << endl;
cout << "Type 'Shield' to get a sheild" << endl;
cout << "Type 'Gun' to get a gun" << endl;
cout << "Type 'Bracers' to get bracers" << endl;
cout << "Type 'Potion' to ge a potion" << endl;
cout << "Type 'Inv' to see your invintory" << endl;
} else if (Input == "Sword" || Input == "sword" ){
cout << "Adding a " << PInv.Items[1] << endl;
PInv.AddItem(PInv.NItems[1]);
PInv.FSpaces++;
} else if (Input == "Staff" || Input == "staff" ){
cout << "Adding a " << PInv.Items[0] << endl;
PInv.AddItem(PInv.NItems[0]);
PInv.FSpaces++;
} else if (Input == "Shield" || Input == "shield" ){
cout << "Adding a " << PInv.Items[2] << endl;
PInv.AddItem(PInv.NItems[2]);
PInv.FSpaces++;
} else if (Input == "Gun" || Input == "gun" ){
cout << "Adding a " << PInv.Items[3] << endl;
PInv.AddItem(PInv.NItems[3]);
PInv.FSpaces++;
} else if (Input == "Bracers" || Input == "bracers" ){
cout << "Adding a " << PInv.Items[4] << endl;
PInv.AddItem(PInv.NItems[4]);
PInv.FSpaces++;
} else if (Input == "Potion" || Input == "potion" ){
cout << "Adding a " << PInv.Items[5] << endl;
PInv.AddItem(PInv.NItems[5]);
if (StacksP == 0){
PInv.FSpaces++;
StacksP++;
};
} else if (Input == "Inv" || Input == "inv" || Input == "Invintory" ){
cout << "You have " << PInv.TSpaces << " slots in your invintory." << endl;
cout << "You have " << (PInv.TSpaces - PInv.FSpaces) << " spaces free." << endl;
if (PInv.NItems[0] > 0){
cout << "You have " << PInv.NItems[0] << " " << PInv.Items[0] << endl;
}
if (PInv.NItems[1] > 0){
cout << "You have " << PInv.NItems[1] << " " << PInv.Items[1] << endl;
}
if (PInv.NItems[2] > 0){
cout << "You have " << PInv.NItems[2] << " " << PInv.Items[2] << endl;
}
if (PInv.NItems[3] > 0){
cout << "You have " << PInv.NItems[3] << " " << PInv.Items[3] << endl;
}
if (PInv.NItems[4] > 0){
cout << "You have " << PInv.NItems[4] << " " << PInv.Items[4] << endl;
}
if (PInv.NItems[5] > 0){
cout << "You have " << PInv.NItems[5] << " " << PInv.Items[5] << endl;
};
} else {
cout << "That is an invald request mi'lord" << endl;
}
};
system ("PAUSE" );
return 0;
}
Last edited on Apr 14, 2009 at 3:46am UTC
Apr 14, 2009 at 4:48pm UTC
The addItem function's design makes no sense to me. Why are you incrementing ItoAdd2? Why are you trying to create a local ItemFunc and then return a reference to it? Why is AddItem a member of the class when it is simply creating a new array and returning the pointer to the caller without affecting the class instance at all? Why is ItemFunc declared in the way that it is. It is a pointer to a pointer. What exactly do you expect AddItem to do? It isn't adding anything to the Invintory instance. This class needs to be totally redesigned. Make the member variables private and design the interface so that it operators on the members directly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class Invintory {
public :
int TSpaces;
int FSpaces;
ItemFunc NItems[6];
string Items[6];
void AddItem(ItemFunc &ItoAdd){
ItemFunc ItoAdd2 = new OtherIF[6];
ItoAdd2++;
ItoAdd = ItoAdd2;
};
void DropItem(int *ItoDrop[]){
*ItoDrop++;
};
void UseItem(int *ItoUse[]){
*ItoUse--;
};
//bool Stacks(ItemFunc &ItoStack)
};
Topic archived. No new replies allowed.