Hi, I was told by a work colleague that the code within the main function should be kept to a minimum. With that in mind, I started trying to move some of the heavier functions away from the main function and call a separate function instead. Only, I'm having a problem getting the function to work as intended, I am pretty sure the problem lies in this section (the whole function is around 180 lines). If it isn't clear in the code, this is part of a shopping section in a game, as the code stands, you are able to buy and sell items, but the gold doesn't change.
for (int j=0; j<ITEMSNUMBER; j++)
{
if (shopitem==j+1) //the user selects 1 for 0th element etc.
{
int afford= MainCharacter.Gold/Items[j].BuyPrice; // this calculates afford correctly
cout << "How many do you want to buy? (max " << afford << ")" << endl;
int quantity;
while(!(cin >> quantity))
{
cerr << "Choice <" << quantity << "> is Invalid, Please Choose a Valid Option\n";
cin.clear(); // Clears the input stream fail flag
cin.ignore(100, '\n'); // Ignores any characters left in the stream
}
if (quantity<=afford)
{
Items[j].Quantity+=quantity; // this quantity rises
MainCharacter.Gold-=quantity*Items[j].BuyPrice; //Gold does not go down
cout << "Thank you for your purchase" << endl;
}
else
{
cout << "Insufficient Gold" << endl;
}
}
}
Sorry for the edit, but I did some debugging, turns out the gold is going down, but it's getting reset for some reason, here's the MainCharacter class (object type Hero).