So Confused - Help plox?

Pages: 12
I am trying to make an inventory for a game im making. Basically, you enter a shop, and 10 items that you can purchase show up. You can select each item (by inputing 1 thru 10), and then it asks you how many of that item you want. You input a quantity, and then that quantity gets stored (in an array perhaps?). That number stored, being the quantity, needs to be able to change when you use it, get more, etc. These values have to be able to be accessed at any time. I am trying to use an array for this but can't figure it out. Do I need pointers? How should I go about doing this?

Thanks,
Timmah m/
You have ten items:

int inventory[10];//1-10 each will represent the amount of a type of item

then maybe:

1
2
cin >> item_type >> amount;
inventory[item_type] += amount;


Is that what you want?
Maybe you could create an array or vector of classes that contain variables storing the item
quantity. Something like this maybe:
1
2
3
4
5
6
7
8
9
10
11
12
13

//the class
class item {
    public: //don't use a class exactly like this, make variables private and public assessor methods
    int amount;
};

//somewhere else
item item_array[10]; //0-9 representing the 10 items

//somewhere else again
cin >> type; //get item type to buy
cin >> item_array[type].amount //get amount to buy 
Last edited on
Brown, that did not work - why would you use += ?

Mod - I haven't learned about classes yet, so I will see if I can learn about them and then try this.

Thanks!
Brown - I changed the += to a = and it works the way I want! :D
var+= is equivalent to var = var +. Using just the = sign is fine if the player starts with 0 objects, but will give you runtime bugs if they had more. Say the player has 3 of item two. They choose to buy 5 more. They should end up with 8, but:
1
2
3
item2 = 3; //they have 3 items
item2 += 5; //they add 5 to what they have (8)
item2 = 5; //they get 5, erasing the 3 they previously had 


I don't know why += didn't work for you, but try writing out var_name = var_name + (your number)
Last edited on
This is what I did:

int inventory[10];
int item_type, amount;

cin >> item_type;
cin >> amount;
inventory[item_type] += amount;

cout << inventory[item_type] << endl;
cout << amount;

I input 1 in for item_type and 12 in for amount. It then prints out 2001945468 and then 12.

I think I fixed it by putting this at the very beginning:

int inventory[10] = {0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0};

yes?
cout << inventory[item_type] << endl; should be cout << item_type << endl; That way it prints the type, not the amount stored in the type.

cout << amount; should be cout << inventory[item_type]; to print the amount stored in the array at that type.

I am not sure why you get that number though... (int's are automatically initialized to 0)
Here is what I did and it worked for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//array test for timmah
#include <iostream>
using namespace std;

int inventory[10];
int type, amount;

int main(){
    cout << "Enter item type (0-9): ";
    cin >> type;
    cout << "Enter amount: ";
    cin >> amount;
    inventory[type]+=amount;
    cout << "Type " << type << " has " << inventory[type] << " items in it" << endl;
    system("pause"); //DON"T USE THIS in your game, it is here just to stop the program to see the output
    return 0;}
Last edited on
yeah, this is working for me, thanks! What's wrong with system("pause"); though?
when your program is by itself, it works, but when I put it in my program it has some serious problems. When type = 1, works fine. When type = 2, the output displays as some very large number. When type = 3 it works fine. Everything above that crashes. D:

Asigned values:
s and item_type are both ints
b and d and char

s = getch(); // goes into stores/back to menu of stores to purchase items
switch (s) {
case '1':
cout << "\n\nWelcome To The General store!\n";
cout << "1) 1 Gallon Water:" << endl;
cout << "2) 1 lb Meat:" << endl;
cout << "3) 5 lbs Meat:" << endl;
cout << "4) Assortment of Spices/Herbs:" << endl;
cout << "5) Wool Gloves:" << endl;
cout << "6) Wool Coat:" << endl;
cout << "7) Light Leather Boots:" << endl;
cout << "8) Pack of 5 Torches:" << endl;
cout << "9) Pack of 5 Lanterns:" << endl;
cout << "10) 1 Quart of Oil:" << endl << endl;
loop16:
cout << "Press 1 - 10 and Then Enter To Purachase An Item. Press 'b' To Go Back To The \nShop List. When done, press 'd'";
cin >> item_type;
switch (item_type) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '10':
{
cout << "Enter amount: ";
cin >> amount;
inventory[item_type]+=amount;
cout << "Type " << item_type << " has " << inventory[item_type] << " items in it\n\n";
goto loop16;
}
break;
case 'b':
goto loop15;
break;
case 'd':
goto game;
break;
default:
{
cout << "Incorrect Command!";
goto loop16;
}
}
I had a bad crash last night, and this morning it prints symbols for the amount...as in..

"Type 1 has *SOME RANDOM SYMBOL* items in it"

What the hell did I do?
I honestly have no idea. The code you posted is fine, by looks at least. If your code is less than 200 lines, please post the whole thing. (also, please use the code tags...the 1st button in the format menu on the right.)

Arrays indexing (the [number]) starts at 0. So an array declared int items[10] would be accessed by typing 0-9 in the brackets. Typing a 10 would be trying to access an element that doesn't exist(11). This will cause a Segfault and is one of the most common reasons for a crash. A Segfault is when your program tries to access memory that it doesn't own (i.e. memory for another program).

Did your whole computer crash? (hopefully not...) If it did, you may have bigger problems. Although these shouldn't be related to your code...

Try rebuilding your project completely instead of just compiling. Also, check your array declaration and accesses. If your IDE has a debugger, run it. When your program crashes, it will tell you what happened and what line of code caused the crash. (mostly anyways)

system("pause"); generates platform specific machine code in the .exe (or something like it) and it will fail on different platforms. getch(); should do the same thing, but without restriction.
Last edited on
Hi timmah,


Try to use the Object-Oriented concept in C++ to sort the thing out. At least, try "class".


B2ee
2c:

don't use a class exactly like this, make variables private and public assessor methods

Writing set/get methods for every single private attribute of a class is just as bad as making the attribute public. Then again, there are situations in which making attributes public is perfectly ok (though many people use structs for this, what you should keep in mind is that the only semantic difference between a struct and a class is the default access modifier (public in structs and private in classes). Other than that they are the same thing), like for example in the std::pair - the only purpose of that class is to store two values. It has no logic attached to it, so making the fields private and providing accessor methods to them is completely pointless.

Try to use the Object-Oriented concept in C++ to sort the thing out. At least, try "class".


OOP is powerful, but it's no magic fairy that somehow makes your code better. It takes quite a bit of effort to design a clean OO system, and I really have to ask if that wouldn't be overkill for what the OP is trying to do. Other than that I'd also like to mention that using classes doesn't make anything more object oriented at all.
what he wanted to say is:

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
s = getch(); // goes into stores/back to menu of stores to purchase items
switch (s) {
case '1':
cout << "\n\nWelcome To The General store!\n";
cout << "1) 1 Gallon Water:" << endl; 
cout << "2) 1 lb Meat:" << endl;
cout << "3) 5 lbs Meat:" << endl;
cout << "4) Assortment of Spices/Herbs:" << endl;
cout << "5) Wool Gloves:" << endl;
cout << "6) Wool Coat:" << endl;
cout << "7) Light Leather Boots:" << endl;
cout << "8) Pack of 5 Torches:" << endl;
cout << "9) Pack of 5 Lanterns:" << endl;
cout << "10) 1 Quart of Oil:" << endl << endl; 
loop16:
cout << "Press 1 - 10 and Then Enter To Purachase An Item. Press 'b' To Go Back To The \nShop List. When done, press 'd'";
cin >> item_type;
switch (item_type) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '10':
{
cout << "Enter amount: ";
cin >> amount;
inventory[item_type]+=amount;
cout << "Type " << item_type << " has " << inventory[item_type] << " items in it\n\n";
goto loop16;
} 
break;
case 'b':
goto loop15;
break;
case 'd':
goto game;
break;
default:
{
cout << "Incorrect Command!";
goto loop16;
} 
}


dont yell on me for copying this thing but looks better in CODE !!! [ code ] .... [ / code ]
Last edited on
@hanst99
I am aware of that, its just a good idea to have the most important variables private in larger/more complex programs.
Couple things I want to point out/clarify:

You're initializing int inventory[10]; right? I'm guessing you are or else you'd get a compile error but since it's not in the code i wasn't sure.

Also, remember how array indices work. They count from 0. So if you ever try to access inventory[10] then you'll get a bunch of garbage-- only inventory[0] through inventory[9] are valid. Because of this, you need to inventory[item_type-1] += amount; at line 32. Also in the next line it should be cout << "Type " << item_type << " has " << inventory[item_type-1] << //...

I'm guessing you do need to initialize your array to 0s btw. You can do this with a for loop. I'm not so sure about ints automatically initializing themselves to 0.. sounds sketchy to me. If they don't, it would also explain why you'd get a bunch of random numbers when you try to add something to it.

So summary:
• make sure you're initializing your inventory array correctly and putting in 0s for every value as initial values.
• Also keep track of when to use item_type-1 and when to use item_type. item_type-1 is when you're working with arrays.

good luck!
Last edited on
int's are initialized to zero whether they are by themselves or in arrays.
Like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//array initialization test
#include <iostream>

using namespace std;

int test[10]; //array of ints, not initialized
int abc; //int not initialized

int main(){
    for (int c = 0; c<=9; c++){
        cout << test[c] << endl;} //output array content
    cout << abc << endl; //global int
    system("pause");
    return 0;}


This outputs:

0
0
0
0
0
0
0
0
0
0
0
press any key to continue . . .
int's are initialized to zero whether they are by themselves or in arrays.

Not in all cases. With global variables, that's so, but if they are local:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//array initialization test
#include <iostream>

using namespace std;

int test[10]; //array of ints, not initialized
int abc; //int not initialized

int main(){
    int local;
    int local_arr[10];

    for (int c = 0; c<=9; c++){
        cout << test[c] << endl;
    } //output array content
    cout << abc << endl; // global int
	
    cout<<local<<endl; // local
    for(int c = 0; c < 10; c++) {
        cout<<local_arr[c]<<endl;
    }
    system("pause");
    return 0;
}
0
0
0
0
0
0
0
0
0
0
0
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
Press any key to continue . . .
Pages: 12