Sign in to follow this  
KyleM

Deleting a specific element in a string of vectors.

Recommended Posts

void use_inventory(enemy &players_enemy,family_member &player)
{

	vector<std::string>::const_iterator inv_iter;




	cout<<"--------------------------------------"<<endl;
	cout<<"             Inventory"<<endl;
	cout<<"--------------------------------------"<<endl;

	for(inv_iter = player.player_inventory.begin();inv_iter != player.player_inventory.end();inv_iter ++)
	{ 
		std::cout<<(inv_iter - player.player_inventory.begin())<<": "<<*inv_iter<<std::endl;



	}


	bool valid_choice = false;  
	int choice;


	while(valid_choice == false)
	{
		cout<<"Type the name of the item you want to use: ";

		cin>>choice;

		if(player.player_inventory.at(choice) == "Small Potion")
		{
			use_small_potion(player,inv_iter,players_enemy,choice);
		}
		if(player.player_inventory.at(choice) == "Medium Potion")
		{
			use_medium_potion(player,inv_iter,players_enemy);
		}
		if(player.player_inventory.at(choice) == "Woma Potion")
		{
			use_woma_potion(player,inv_iter,players_enemy);
		}
		if(player.player_inventory.at(choice) == "Granada")
		{
			//use_granada();
		}

	}







}

void use_small_potion(family_member &player, vector<string>::const_iterator potion_iter,enemy players_enemy,int choice)
{
	potion_iter = player.player_inventory.at(choice);

	if(player.health != player.max_health)
	{
	clear_screen();
	cout<<"You used a small potion and heal by 20 health points"<<endl;
    player.health += 20;

	if(player.health > player.max_health)
	{
		player.health = player.max_health;
	}
    clear_and_delay(3500);
	player.player_inventory.erase(potion_iter);
	fighting_loop(player,players_enemy);
	}


	if(player.health == player.max_health)
	{
		cout<<"You are at full health!";
		fighting_loop(player,players_enemy);

	}

}

void use_medium_potion(family_member &player, vector<string>::const_iterator potion_iter,enemy players_enemy)
{
if(player.health != player.max_health)
	{
	clear_screen();
	cout<<"You used a medium potion and heal by 40 health points"<<endl;
    player.health += 40;

	if(player.health > player.max_health)
	{
		player.health = player.max_health;
	}
    clear_and_delay(3500);
	player.player_inventory.erase(potion_iter);
	fighting_loop(player,players_enemy);
	}


	if(player.health == player.max_health)
	{
		cout<<"You are at full health!";
		fighting_loop(player,players_enemy);

	}
}

void use_woma_potion(family_member &player, vector<string>::const_iterator potion_iter,enemy players_enemy)
{
if(player.health != player.max_health)
	{
	clear_screen();
	cout<<"You use a woma potion and get fully healed"<<endl;
	player.health = player.max_health;

	if(player.health > player.max_health)
	{
		player.health = player.max_health;
	}
    clear_and_delay(3500);
	player.player_inventory.erase(potion_iter);
	fighting_loop(player,players_enemy);
	}


	if(player.health == player.max_health)
	{
		cout<<"You are at full health!";
		fighting_loop(player,players_enemy);

	}
}

void use_granada(family_member &player, enemy &players_enemy)
{

}

I have an inventory which the user chooses from and then the program checks what is within the element the user picked. It then calls a function and runs more code. Once done with that I want to delete that choosen element they picked, but im having trouble with that. At first I tried player.player_inventory.erase(choice) but it requires an iterator to the element and not an int (I know that .erase() is not the best for performance, but performance is not going to be an issue in a console game) I then tried assigning a iterator to the inventory vector and used the choice variable to assisign it to that element. The compiler tells me: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion) Here is the line of code I tried: potion_iter = player.player_inventory.at(choice); This program is a training exercise and I have not covered clasees / templates an alot of other things so I was a bit hard pressed to try make an alternative. Help on this matter would be greatly appreciated. Thank you in advance for any help given.

Share this post


Link to post
Share on other sites
Thank you for that, worked like a charm. I don't know how I didin't think of it, it's fairly simple. I guess my mind hasn't been clear these past few days.

Share this post


Link to post
Share on other sites
Another question, say I want to acsess the last element in the vector (using at()) and I want to print it, how would I go about doing this.

Share this post


Link to post
Share on other sites
Ahh thank you, I was typing player.player_inventory.at(player.player_inventory.back())

So mines was wrong. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this