I'm writing a fantasy store to purchase items from and need help passing the arrays, I know I need a switch statement; but I just kind of hit a snag and could use some help on me code.
#include <iostream>
#include <string>
using namespace std;
class Item
{
public: int cost;
public: string name;
public: string description;
int main (void)
{
int choice =0;
int num =0;
Item object[100];
object[0] : new Item(10, "Acid flask", "A flask filled with acid to use situationally.");
object[1] : new Item(15, "Alchemist fire", "A flask filled with a combustable gel.");
object[2] : new Item(10, "Bottled Torch", "A bottle filled with light.");
object[3] : new Item(5, "Bottle of cure disease", "A bottle of ground herbs to fight off diseases.");
object[4] : new Item(15, "Bottle of healing powder", "A bottle of ground herbs to heal scrapes and burns.");
object[5] : new Item(10, "Bottle of remove poison", "A bottle of ground herbs to remove poison.");
object[6] : new Item(20, "Healing potion", "A magical potion used to heal deep wounds.");
object[7] : new Item(25, "Potion of swiftness", "A magical potion which increases ones speed temporarily.");
object[8] : new Item(50, "Potion of true ressurection", "A magical potion which has the power to bring back to recently deceased.");
int inventory [100];
int invTot;
cout << "Welcome to the potion shop. We currently are limited on our inventory please pick from our reserves." << endl;
cout << "This is what we currently have." << endl;
for (i = 0; i < invTot; i++) {
cout << "You are carrying " << object[inventory[i]].name;
}
First off, put your code in between [...] and [/...] where ... is the word "code"
Name the parameter in the Item constructor different names the class variables such that you would have cost = _cost; for example.
Use = not : when assigning values to the array.
New is used when creating pointers, get rid of it or declare Item* object[100]; and don't forget to delete the pointers (I.e. every new should have a corresponding delete.)
The end of the Item class declaration should have a semicolon after the last curly brace.
invTot is uninitialized as written so using it in the loop will almost certainly cause a seg fault bc it will likely be a random large number. I would set it to 8 based on the code above.
You don't have to declare everything public individually. Write it once and then everything after it is public until you declare private or protected.
int main (void)
{
int choice =0;
int num =0;
Item* object[100];
object[0] = new Item(10, "Acid flask", "A flask filled with acid to use situationally.");
object[1] = new Item(15, "Alchemist fire", "A flask filled with a combustable gel.");
object[2] = new Item(10, "Bottled Torch", "A bottle filled with light.");
object[3] = new Item(5, "Bottle of cure disease", "A bottle of ground herbs to fight off diseases.");
object[4] = new Item(15, "Bottle of healing powder", "A bottle of ground herbs to heal scrapes and burns.");
object[5] = new Item(10, "Bottle of remove poison", "A bottle of ground herbs to remove poison.");
object[6] = new Item(20, "Healing potion", "A magical potion used to heal deep wounds.");
object[7] = new Item(25, "Potion of swiftness", "A magical potion which increases ones speed temporarily.");
object[8] = new Item(50, "Potion of true ressurection", "A magical potion which has the power to bring back to recently deceased.");
int inventory [100];
int invTot;
int credits= 100;