I'm looking for a bit of help please, I have a merchant class that you can buy from and sell to. The merchant has his own vector of struct 'items' as does the player. To begin the transactions, a merchants menu function is called, passing in the address of a player (character class)
The below code works perfectly as expected, (e.g. if you haven't got enough gold, it states it correctly, or if either person doesn't have the item, etc) but as soon as a transaction occurs, like the player selling something, any attempts to sell anything else will show no message and just show the ".1. Back" option, or trying to buy something will say twice the merchant doesn't have it, or if he does, it will say he doesn't have it, but then say you bought it... it's probably something really simple and bad design on my part, but I can't figure it out
void merchant::buyfrom(character *a)
{
print("\nItem to buy: ");
string item1;
fflush(stdin);
getline(cin, item1);
vector<item>::iterator it;
for (it = this->items.begin(); it != this->items.end(); ++it)
{
if (it->name == item1)
{
if (it->price > a->gold)
{
print("\n\nYou don't have enough gold to buy the "); cout << it->name; break;
}
print("\n\nYou bought "); std::cout << item1; print(" for "); std::cout << it->price; print(" gold");
this->items.erase(it);
a->inventory.push_back(*it);
a->gold -= it->price;
this->gold += it->price;
break;
}
else
{
print("\n\nMerchant does not have a "); std::cout << item1; print(" to sell");
}
}
print("\n\n\n1. Back\n\n\n> ");
char c;
std::cin >> c;
}
void merchant::sellto(character *a)
{
print("\nItem to sell: ");
string item1;
fflush(stdin);
getline(cin, item1);
vector<item>::iterator it;
for (it = a->inventory.begin(); it != a->inventory.end(); ++it)
{
if (it->name == item1)
{
print("\n\nYou sold "); std::cout << item1; print(" for "); std::cout << it->price; print(" gold");
a->inventory.erase(it);
this->items.push_back(*it);
a->gold += it->price;
this->gold -= it->price;
break;
}
else
{
print("\nYou do not have "); std::cout << item1; print(" to sell");
}
}
print("\n\n\n1. Back\n\n\n> ");
char c;
std::cin >> c;
}