Hello. Let me start by warning you that my program is very large and it may require a bit of patience on your part to truly understand the problem I'm having with my code. I've tried everything I can think of to fix this, and so now I humbly request some help troubleshooting the problem.
The root of the issue is that I receive a Segmentation Fault error when one particular function is called. I have gotten this error before, usually when I stupidly attempt to write a value to an array that doesn't exist. My function is called itemaction, and it contains an int called xcounter. I initialize xcounter to 0, but for some reason, within the function is gets changed to a ridiculous number (5124883 in my last attempt). I use xcounter to loop through an array (inventoryname[100]) that has 100 elements, so I assume the error is thrown when the program tries to access the non-existent inventoryname[5124883].
I set a watch on inventoryname[xcounter], and receive this text when the error occurs:
inventoryname[xcounter] = The program being debugged was signaled while in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior use "setunwindonsignal off"
Evaluation of the expression containing the function (std::string::c_str() const) will be abandoned.
Program received signal SIGSEGV
Segmentation fault
So, I suppose some code is needed. I'll try to include the relevant pieces, and if necessary I can upload the entire thing to a pastebin or something, but I don't want to bother you all by giving you 6,000 lines to work through.
I define these variables globally:
1 2 3 4
|
string inventoryname[100], inventorycat[100];
int inventorynum[100], removednum;
string packname = "DEFAULT";
double maxinvweight, invweight;
|
The program is a game, and throughout its execution, you fill your inventory with items (which go into the respective global arrays). I have made a function called itemaction which allows you to either display the stats for an item or remove the item from your inventory. Code below:
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
|
string itemaction (string itemname, string action, int tempcounter){ //Accepts stringname of an item, matches it to a class, and performs an action like EQUIP, UNEQUIP, DISPLAY STATS, DISCARD, etc., and returns itemnumber removed
int xcounter = tempcounter;
item newitem; //create an empty class item
//Here, I create all of the available items which I'm omitting for space's sake
if(itemname==leatherboots.stringname) newitem = leatherboots; //Then I have a line of code like this for each item so I can determine the item that the user chose
if (action=="DISPLAY"){
xcounter = 0;
newitem.display_stats(1,newitem.itemcat);
}//end of DISPLAY if
else if (action=="REMOVE"){
xcounter = 0;
//cout<<"MADE IT HERE"<<endl;
for(xcounter=0;inventorynum[xcounter]!=0;xcounter++){ //Loop through inv starting at the beginning and going until an empty spot is reached (meaning inv must be compressed)
if(itemname==inventoryname[xcounter]){ //If the item they selected matches the item in the inv, remove it
inventoryname[xcounter]="X";
inventorynum[xcounter]=0;
inventorycat[xcounter]="X";
cout<<"You remove the "<<itemname<<" from your pack. ";
invweight = invweight - newitem.weight; //deduct the weight from your pack
while(getchar() != '\n');
wipescreen();
}
}//end of for
compressinv();
removednum = newitem.itemnumber; //set global var to itemnumber
}//end of REMOVE
else cout<<"ERROR: Incorrect action coded";
}
|
This function is called after filling in string variable selecteditem with the name of an item from a list. Here is the code where the function is called:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
if(selecteditem=="DONE"){
choiceok == 1; //If they chose DONE, continue with the loop
wipescreen();
}
else {
wipescreen();
itemaction(selecteditem,"DISPLAY",0); //Otherwise, they chose an item, so display the stats
cout<<"REMOVE the "<<selecteditem<<"? Or are you DONE?"<<endl<<">";
gets(choice2);
UP(choice2);
if(strcmp(choice2,REMOVE)==0) { //If they choose to remove the item
fireloop++; //Time is spent
itemaction(selecteditem,"REMOVE",0); //Remove it from the pack; function sets removednum to item number that they removed
for(counter=0;counter<11;counter++){
if(removednum==bedroomitem[counter].itemnumber) bedroomitem[counter].stringname = selecteditem; //if the removed item is one of the items of clothing in the bedroom, replace it on the list
}
}
|
I know this may be difficult to help with given only these bits of code. If you have any questions about variables or program order or anything, just ask and I'll try to give more detail. I'm teaching myself how to code c++, so I realize that some of my syntax may not be perfect.
If it helps, I'm using Code::Blocks 10.05 with the GNU GDB 6.8 debugger. I'm running on a Windows 7 x64 PC.
Thanks for your help, and sorry for the long and complicated post!