Hey everyone! For some reason I am having trouble altering the values of an array in a class. I wrote one function to set its elements to certain values and I wrote another to change them back to 0. I can't seem to understand why the first works and the second does not. Or maybe it does and something is wrong with my function that displays the values in the array? Is there something I am completely missing? Here's the code you need to look at, it's part of a larger program, though, I am only copying the stuff that deals directly with this problem. Excuse me if the code is not the most elegant, I am a beginner. Thanks so much!
class Place {
private:
int PlaceItems[10];
public:
void ItemPlace(int ID);
void ListItems(string itemID[NUM_ITEMS], int optionDisplay[10]);
void ItemRemove(int itemChoice);
};
/*this function is called with specific items and to specific places (there is a map class with an array of places). It works in filling up a place's items array with values which represent specific items.*/
void Place::ItemPlace(int ID) {
int x;
for (x = 0; x < 10; x++) {
if (PlaceItems[x] != 0) {
;
}
else {
PlaceItems[x] = ID;
return;
}
}
}
void Place::ListItems(string itemID[NUM_ITEMS], int optionDisplay[10]) {
int x;
for (x = 0; x < 9; x++) {
if (PlaceItems[x] != 0) {
cout << " " << x << ". " << itemID[PlaceItems[x]] << endl;
optionDisplay[x] = PlaceItems[x];
if (x == 8) {
optionDisplay[x + 1] = 420;
cout << " " << x << ". Cancel" << endl;
return;
}
}
else {
optionDisplay[x] = 420;
cout << " " << x << ". Cancel" << endl;
return;
}
}
}
/* This function is supposed to remove the item selected but for some reason does not work. It even doesn't work if I just have it loop through the whole array and set them all to 0, so something else has to be wrong */
void Place::ItemRemove(int itemChoice) {
int x;
for (x = 0; x < 10; x++) {
if (PlaceItems[x] == itemChoice) {
PlaceItems[x] = 0;
return;
}
else {
;
}
}
}
/* Everything about this function works except when it gets called again, it lists all the same items that there were before, regardless of whether you took one or not */
bool Map::Take(int &row, int &col, int Inventory[INVENTORY_CAPACITY], string itemID[NUM_ITEMS]) {
cout << endl << "Take what?" << endl;
int optionDisplay[10];
map[row][col].ListItems(itemID, optionDisplay);
int selection;
cout << "Selection: ";
cin >> selection;
int itemChoice = optionDisplay[selection];
if (itemChoice == 420) {
return true; //exits back to the function taking the commands
}
else {
PlaceInventory(itemChoice, Inventory); //this function works
map[row][col].ItemRemove(itemChoice); //this does not
cout << "Item taken!" << endl;
return true;
}
}
Example Output:
Action: take
Take what?
0. Silver Necklace
1. Paper
2. Cancel
Selection: 0
Item taken!
Action: take
Take what?
0. Silver Necklace
1. Paper
2. Cancel
Selection: