Reputation: 13
Ok this is a difficult question to google. What I want is once a user enters 10 items in the inventory array I want it to clear the console and tell a story. I tried but it would display the story no matter how many items were in the array as I defined the array as max 10. Also Im trying to find a way to SWAP an item in the array. what I mean by this is I want the console to ask the user if they would like to trade {item which is already in the array} for {item2} y or n?
Problem is it doesnt display the items name just the position in the array, whe outputted in the story example. "would you like to trade1for enchanted sword" is displayed. Then if you hit yes it doesnt add it to the array. Also it bypasses my thank you anyways
void tellStory(InventoryRecord list[], int& size)
{
int numOfRecs = size;
int pos = rand()%numOfRecs; //Generate a random position of the item
//Generate item to trade, I'll refer to it as "Item
int TRADE_CHANCE = 0;
const string str = "Enchanted Sword";
InventoryRecord recList[MAX_SIZE];
if (numOfRecs == MAX_SIZE) {
system("cls");
cout << "So your ready to begin your quest!" << endl << endl;
cout << "Before you begin I would like the opportunity to trade you a rare item!" << endl << endl;
cout << "Would you like to trade" << pos << "for" << str << "?" << endl << endl;
cout << "Yes or No? ";
cin >> choice;
if (toupper(choice) == 'Y')
(rand()%100 < TRADE_CHANCE);
(recList[pos], str);//Here add the trading stuff, which is some text, user input(y/n) and replacing realist[pos] = Item, etc
}
else {
cout << "Thanks anyways!" << endl << endl;
system("pause");
}
system("cls");
}
Upvotes: 0
Views: 146
Reputation: 274
Thing is you aren't checking anywhere for the inventory to be full. You may want to replace
case 'A': addData(recList, numOfRecs); break;
If you want to tell a story only once
case 'A':
if (numOfRecs == MAX_SIZE-1) {
addData(recList, numOfRecs);
//TELL STROY HERE
} else{
addData(recList, numOfRecs);//If you want to tell a story only once
}
break;
If you want to tell the story anytime
case 'T': //TEll story
if (numOfRecs == MAX_SIZE) {
//TELL STORY HERE
}
break;
OR, even though it will be annoying, after every action, check if 10 items are in inventory and display the story
Now for the trading, assuming you want them to trade an item they have, for a random item you are gonna generate use: pos = rand()%numOfRecs; //Generate a random position of the item Now if you want to generate a "random" suggestion for trade:
if (rand()%100 < TRADE_CHANCE) {
int pos = rand()%numOfRecs; //Generate a random position of the item
//Generate item to trade, I'll refer to it as "Item"
SuggestTrade(recList[pos], Item);//Here add the trading stuff, which is some text, user input(y/n) and replacing realist[pos] = Item, etc
}
If the item you want to Trade, its actually a position swap in the same array:
int pos = rand()%numOfRecs; //Generate a random position of the item
int pos2;
do {
pos2 = rand()%numOfRecs; //Generate a random position of the item
}while (pos2 == pos);
//Actually "swapping"
InventoryRecord tmp = recList[pos];
recList[pos] = recList[pos2];
recList[pos2] = tmp;
Hope it Helps!
Upvotes: 3