Public Group

# Search function with switch?

This topic is 3401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Any suggestions on getting a search function coded for a text based game that will look at 1 value across possibly over 100 "items" and display with a menu of choices for ones that exist in my "inventory"? I.E.: (function goes here) smallLaser.inventoryamount = 1 otheritem.inventory = 1 notanitem.inventory = 0 cout >> You have these two items in your inventory, which one do you want to put in your bank? cin << userResponse (relevant execution code here) I thought about coding that kind of function out but the section of code would still end up being rediculously long to have to search through that in every place I need it to. I think the way I already have planned, while massive and very bulky, will still provide what i'm looking for even if it might be less efficient. It might become more of an important issue once I go over to 3D space.

##### Share on other sites
100 items is neither massive nor bulky. But try (C++) std::search or (C) qsort.

##### Share on other sites
Search functions in general.

I have a feeling you've misworded your question, because while you use the term the 'search' you also seem to just want to display a big list of the items in your inventory and allow the player to pick one.

You can accomplish this with a simple loop assuming your inventory is represented as something like a std::vector of items. Why don't you elaborate for us?

##### Share on other sites
Try to describe the entire process in English first, before writing any pseudocode. What does the data look like? (e.g. "There are N instances of 'item', where N is about 100. Each item has a 'inventory' member that says whether or not the player has it.") What are we going to do with the data? (e.g. "I want to display every item that the player has." Or "I want to find an item with a given name, and check whether the player has it." Or whatever else it is you actually want to do.) What do you think is the first step in accomplishing that? The next? Etc.

##### Share on other sites
Even though i've already coded it in the hard way, and when reading the code it makes the most sense to me to write out a bunch of if, else, and switches,

What I have going on is that I have a file that only executes equip functions, and removal functions from the character. I have thus far separated these functions to isolate the search down to 1 of 4 bodyparts but I have 13 different arms for each side, left and right.

Btw everything except my player file is a struct because they all only hold a set of values. I only have bool and int inside my player file, so could I turn that into a struct as well?

So what I have now is that these equip and remove functions will look at each item in the game (nothing fancy) and check the items invamt member to see if it's in my players inventory before displaying any options. So we're displaying every item the player might have in his inventory so he can choose which item in any particular category he wants to add.

What i'm trying to accomplish is to code together a tighter set of function code so that it will read the same member value across every item, as well as to display that the player can choose to equip the displayed item that has been found. From there the player can enter which option he wants.

So first, here's what I actually have: (Dirty but gets the job done)

if(rightarm10pointer->invamt > 0) {cout << "11) You can equip a " << rightarm10pointer->whicharm << ". " << endl;}if(player->rightarm) //bool{cout << "You already have a right arm on. It must be removed first. " << endl;}else{rightarmpointer->invamt--; // -1 from inventory player->rightarm = true;   // Tell our player sheet we have a right arm rightarm10pointer->equipped = true; //We have the 10th arm in the game on player->maxhp = player->maxhp + rightarm10pointer->maxhp; //Add the max HP of the arm to the player max player->currenthp = player->currenthp + rightarm10pointer->hp; //Add the current HP of the arm to the player current player->currentweight = player->currentweight + rightarm10pointer->weight; //Add to weight total}

What i'd like to have done:
Menu{cout << "1) left arm" << legs << torso;} //etc (I know it's wrong)int input;//player decides to equip left armswitch(input){case 1:     {ArmSearchFunction();}};/*-------------------------------/*SearchFunction::ArmSearchFunction(){     search all left arms for value invamt > 0;     have a counter++ every time it searches an arm whether it pass/fails     cout << counter << " You can equip " << ArmBeingLookedAt.name << endl;     develop similar switch creation that increments and displays only what arms exist};//Then in the equip function all i'd have to run is SearchFunction::ArmSearchFunction();

Ideas to accomplish something like this?

##### Share on other sites
Please show the structures that 'player', 'rightarmpointer' and 'rightarm10pointer' point to.

##### Share on other sites
class player{     	public:	string coretype; //No idea why I have this here...	bool torso; //Are we wearing one?	bool legs; //Ditto	bool leftarm; //Ditto	bool rightarm; //Ditto	int streakAmmo; //ammo in inventory	int missileAmmo; //ditto	int smallAutocannonAmmo;	int mediumAutocannonAmmo;	int largeAutocannonAmmo;	int currenthp;              <--	int maxhp;                  <-- I.e. player hp is 24/70	int currentweight; //how much are we carrying	int maxweight; //how much extra stuff can we carry? (strong legs?)	int credits; //money	int heat; //operating capacity	int smalllaserslots; //i.e. if(leftarm10pointer->equipped)	int mediumlaserslots;//     player->leftarm = true	int largelaserslots; //     player->largelaserslots = player->largelaserslots + leftarm10pointer->largelaserslots;	int largeautocannonslots;	int mediumautocannonslots;	int smallautocannonslots;	int missileslots;	int damage; //combined damage from (Xslots equipped)*(dmg value)	int playermech1(string coretype);	~playermech();};        rightarm rightarm10; //create structure. this is under main()	rightarm10.whicharm = "Medium Laser Arm";	rightarm10.hp = 12;	rightarm10.maxhp = 12;	rightarm10.weight = 8;	rightarm10.smallLaserSlots = 1;	rightarm10.mediumLaserSlots = 1;	rightarm10.largeLaserSlots = 0;	rightarm10.smallAutocannonSlots = 0;	rightarm10.mediumAutocannonSlots = 0;	rightarm10.largeAutocannonSlots = 0;	rightarm10.missileSlots = 0;	rightarm10.broken = false;	rightarm10.bankamt = 0;	rightarm10.mechinvamt = 0;	rightarm10.buyprice = 2250;	rightarm10.sellprice = 900;	rightarm10.equipped = false;	rightarm10pointer = &rightarm10; //pointer      /*rightarm* rightarmpointer; this is outside main function*/

1. 1
2. 2
frob
16
3. 3
Rutin
12
4. 4
5. 5

• 13
• 12
• 59
• 14
• 15
• ### Forum Statistics

• Total Topics
632124
• Total Posts
3004243

×