help on final project

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;

// constructor
public: Item (int cost, string name, string description)
{
cost = cost;
name = name;
description = 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;
}

cout << object[i].description << " costs " << object[i].cost;
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.


#include <iostream>
#include <string>
using namespace std;


class Item
{
public: int cost;
string name;
string description;

// constructor
public: Item (int cost, string name, string description)
{
cost = cost;
name = name;
description = 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;
int credits= 100;

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;
cout << '1' << object[0][cost, name, description] << endl;
cout << '2' << object[1][cost, name, description] << endl;
cout << '3' << object[2][cost, name, description] << endl;
cout << '4' << object[3][cost, name, description] << endl;
cout << '5' << object[4][cost, name, description] << endl;
cout << '6' << object[5][cost, name, description] << endl;
cout << '7' << object[6][cost, name, description] << endl;
cout << '8' << object[7][cost, name, description] << endl;
cout << '9' << object[8][cost, name, description] << endl;

switch (num) // outputs number of which item is being purchased.
{
case 1: object [0]
{
cout << object [0](cost, name, description)
object [0] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = object[0](cost)- credits;
}
break;
}
case 2: object [1]
{
cout << object[1](cost, name, description)
object [1] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = object[1](cost)- credits;
}
break;
}
case 3: object [2]
{
cout << object[2](cost, name, description)
object[2] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits- object[2](cost);
}
break;
}
case 4: object [3]
{
cout << object[3](cost, name, description)
object [3] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits- object[3](cost);
}
break;
}
case 5: object [4]
{
cout << object[4](cost, name, description)
object[4] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits- object[4](cost);
}
break;
}
case 6: object[5]
{
cout << object[5](cost, name, description)
object[5] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits- object[5](cost);
}
break;
}
case 7: object[6]
{
cout << object[6](cost, name, description)
object[6] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits- object[6](cost);
}
break;
}
case 8: object[7]
{
cout << object[7](cost, name, description)
object[7] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits- object[7](cost);
}
break;
}
case 9: object[8]
{
cout << object[8](cost, name, description)
object[8] = inventory[i];
for (i=0, i<invTot, i++)
{
cout << "You are carrying " << object[inventory[i]].name;
credits = credits - object[8](cost)
}
break;
}
case 10: default
{
break;
}
}
Topic archived. No new replies allowed.