Stacks working with Classes

So im creating a piece of coursework using stacks, its small game that could be implemented into an RPG game. Where basically you open 3 chests looking for gear.
I have my gear/loot and can cal it randomly that is all fine.
HOWEVER i MUST use stacks in my coursework therefore i am trying to use them to create the chests.
All the chest needs is a name and the ability to hold a set number of items in a stack.
Stacks are confusing me, i have created my stack within the Chest class. however how do i write a constructor for it?
how do i push my Loot onto it?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stack>
#define LootNumber 14

using namespace std;

class Loot
{
public:
string Name;
int Rarity;
bool Set;

Loot(string N, int R, bool S)
{
Name = N;
Rarity = R;
Set = S;

}

};
string NameOption[] = { "Stone", "Leather Gloves", "Dragon Gauntlets", "Chair Leg", "Dragon Scale Helmet", "Pebble", "Rusted Breastplate", "Dragon Breastplate", "Empty Bottle", "Chainmail Trousers", "Dragon Skin Trousers", "Broken Stick", "Dagger", "Dragons Sword" };
int RarityOption[] = { 0, 1, 3, 0, 3, 0, 2, 3, 0, 2, 3, 1, 2, 3 };
bool SetOption[] = { 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1 };
string ChestNameOptions[] = { "ChestA", "ChestB", "ChestC" };


void PrintLoot()
{
int i = rand() % LootNumber;
Loot A(NameOption[i], RarityOption[i], SetOption[i]);
cout << "Loot item: " << A.Name << endl;
cout << "Rarity out of 3: " << A.Rarity << endl;
cout << "Part of set: " << A.Set << endl;
}

class Chest
{
public:
string ChestName;
stack <Loot> ChestStack;
int i;

Chest()
{
ChestName = ChestNameOptions[i];
i = rand() % 3;
void push(Loot()); // HELP
void push(Loot);

}
};

void PrintChest()
{
Chest A;
A.ChestStack.push(PrintLoot);
A.ChestStack.top;

}



int main()
{
srand(time(NULL));

PrintChest();



int x; // only here so window does not close automatically
cin >> x;

return 0;
}


however how do i write a constructor for it?
how do i push my Loot onto it?


http://www.cplusplus.com/reference/stack/stack/stack/

1
2
3
std::stack<Loot> MyLoot; // the type in the < > can be any type

MyLoot.emplace(/*Args*/) // calls a Loot constructor with the arguments. places Loot object at top of stack 


Always look at the documentation :+)
Last edited on
thank you :D

I did try looking through but i think i got myself a bit muddled up this time :(
I'll look harder next time ;)
Topic archived. No new replies allowed.