what did i do wrong? with classes

what did i do wrong, it bypasses all my if's and go through memeber function stat() and then goes to inventory(), and in stat() it does not give acurate numbers and doesnt show the strings???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
using namespace std;

//global variables
int MONEY = 1000; //initial money
int MEDKIT = 0, LIGHTER =0, KNIFE =0, FOOD =0, WEAPON =0, CLOTHING =0, ROPE =0, TENT =0, COMPASS =0;//items amounts


class store
{
public:
    string name;
    string desc; //decription
    int price;
    void stat(); // display item information
    void storeMenu(); //display menu of items and options
    void inventory();
    store();
    ~store();
};

store::store()//constructor
{
    cout << "constructor" <<endl;
}

store::~store()//destructor
{
    cout << "destructor" <<endl;
}

void store::stat()
{
    cout << "**********" <<endl;
    cout << "Name: \t" << name <<endl;
    cout << "Price \t$" << price <<endl;
    cout << "Description: " << desc <<endl;
    cout << "**********" <<endl <<endl;
}

void store::storeMenu()
{
    char choice;//needs to be char for choices cuz of '1'

    //display money amount
    cout << "**************" <<endl;
    cout << "  Money $" << MONEY <<endl;
    cout << "**************" <<endl <<endl;


    cout << "Choose which supply you would like to purchase" << endl;
    cout << "E) ***Exit Menu***"  <<endl<<endl;

    cout << "1) Medkit" << endl;
    cout << "2) Lighter" << endl;
    cout << "3) Knife" << endl;
    cout << "4) Food" <<endl;
    cout << "5) Weapons" << endl;
    cout << "6) Clothing" << endl;
    cout << "7) Rope" << endl;
    cout << "8) Tent" <<endl;
    cout << "9) Compass" << endl<<endl;

    cin >> choice;


        if(choice == '1')//choice for items
                {
                    store medkit;
                    medkit.stat();
                }

        if(choice == '2')
                {
                    store lighter;
                    lighter.stat();
                }

        if(choice == '3')
                {
                    store knife;
                    knife.stat();
                }

        if(choice == '4')
                {
                    store food;
                    food.stat();
                }

        if(choice == '5')
                {
                        store weapon;
                        weapon.stat();
                }

        if(choice == '6')
                {
                        store clothing;
                        clothing.stat();
                }

        if(choice == '7')
                {
                        store rope;
                        rope.stat();
                }

        if(choice == '8')
                {
                        store tent;
                        tent.stat();
                }

        if(choice == '9')
                {
                        store compass;
                        compass.stat();
                }

        if(choice == 'e' || 'E')
                {
                        inventory();
                }//end of choice exit
}//end store function

void store::inventory()
{
        cout << "****INVENTORY****"<<endl;
        cout << "Money $" <<"\t\t"<< MONEY <<endl;
        cout << "Medkits: " <<"\t"<< MEDKIT <<endl;
        cout << "Lighters: "<<"\t" << LIGHTER <<endl;
        cout << "Knives: "<<"\t" << KNIFE <<endl;
        cout << "Food: "<<"\t\t" << FOOD <<endl;
        cout << "Weapons: "<<"\t" << WEAPON <<endl;
        cout << "Clothing: "<<"\t" << CLOTHING <<endl;
        cout << "Rope: "<<"\t\t" << ROPE <<endl;
        cout << "Tent: "<<"\t\t" << TENT <<endl;
        cout << "Compass: "<<"\t" << COMPASS <<endl<<endl;
}//end inventory()

int main()
{
    store medkit;
    medkit.name = "Medical Kit";
    medkit.price = 100;
    medkit.desc = "A medkit will heal your party's wounds. ";


    store lighter;
    lighter.name = "Lighter";
    lighter.price = 50;
    lighter.desc = "A lighter will provide you with fire. ";


    store knife;
    knife.name = "Knife";
    knife.price = 100;
    knife.desc = "A knife will provide you will a valuable tool as well as a weapon. ";


    store food;
    food.name = "Food";
    food.price = 150;
    food.desc = "Food is essential to surviving. ";


    store weapon;
    weapon.name = "Weapon";
    weapon.price = 150;
    weapon.desc = "A weapon will provide you with strong protection. ";


    store clothing;
    clothing.name = "Clothing";
    clothing.price = 50;
    clothing.desc = "Clothing will provide you protection from the weather. ";


    store rope;
    rope.name = "Rope";
    rope.price = 25;
    rope.desc = "Rope will provide you the ability to craft in the wilderness. ";


    store tent;
    tent.name = "Tent";
    tent.price = 300;
    tent.desc = "A tent will provide your party with shelter. ";


    store compass;
    compass.name = "Compass";
    compass.price = 50;
    compass.desc = "A compass will provide you with navigation. ";

store menu;
menu.storeMenu();
}//end main






Last edited on
That's because you're doing something wrong; the objects medkit, knife, food, weapon, clothing, rope, tent and compass are all created TWICE. Once inside the main() function and once inside the storeMenu function. This means that they don't share anything (except for their name in the code). That's why it shows no description or doesn't subtract a price. (The price will become 0 by default if not set to a value, which is the case inside the storeMenu() function.)
if(choice == 'e' || 'E')

This demonstrates a misunderstanding of how the || operator works. It essentially says

if either of the following two things are true (i.e. not zero):
1) choice == 'e'
2) 'E'

'E' is always not zero. It's 'E'. So this if statement will always come back as true.





I suspect you meant:

if either of the following two things are true (i.e. not zero):
1) choice == 'e'
2) choice == 'E'

which is coded as

if(choice == 'e' || choice == 'E')
Last edited on
closed account (1vRz3TCk)
metulburr, your object model looks a bit funky.

Would medkit, lighter, etc. not be items held in a store not a store themselves?
CodeMonkey are you implying to make the each item a class itself? the store class inherit all the items classes?
are you saying that the store class should list different stores?
Each item should be a separate object, and the store should simply hold a bunch of objects.
didnt i do that?....twice in this code actually

store medkit;
store lighter;
store tent;
etc.
Last edited on
closed account (1vRz3TCk)
The store class is to represent a store where you can buy items. So objects of class store are individual stores such as Walmart or Harrods. The stock of the store should be represented by a different class. You could have a hierarchy of classes if that is appropriate. You could have a general item class and from that derive other classes like clothing, weapon, food, etc. and so on.

Topic archived. No new replies allowed.