A string array... with more than 1 string?

How would i do this?

Im trying to create an inventory but this just will not work, when i do it how i have it originally (as shown below):

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
 cout << "Do you have weapons and armor?: ";
        cin >> yEquip;
        if (yEquip == 'y')
        {
            
            cout << endl;
            
            string invintory2[6] =
            {   {"Gold Armor"},
                {"Gold Shield"},
                {"Gold Helemt"},
                {"Gold Boots"},
                {"Gold Guantlets"},
                {"Gold Sword"},  };
            cout << "This is your invintory:" << endl;
            for (int s = 0; s < 7; ++s)
            {
                
                cout << invintory2[s] << endl;
                
                
                cout << "Stat boosts: "<<endl;
                cout << "HP + 235 for full Gold" << endl;
                yhp + 235;
                cout << "P.Def + 310 for full Gold" << endl;
                ypdef + 310;
                cout << "P.Atk + 120 for Gold Sword" << endl;
                ypatk + 120;
            }
        }



the program outputs this:


This is your invintory:
Gold Sword
Stat boosts: 
HP + 235 for full Gold
P.Def + 310 for full Gold
P.Atk + 120 for Gold Sword
This is your invintory:
/\251Ę\210\204\342\277


however i have found this to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 cout << "Do you have weapons and armor?: ";
            cin >> yEquip;
            if (yEquip == 'y')
            {
                cout << endl;
                string invintory1[6];
                invintory1[1] = "Bronze Breast Plate"

                cout << "This is your invintory:" << endl;
                for (int s = 0; s < 7; ++s)
                {
                    cout << invintory1[m] << endl;
                }
                cout << "Stat Boosts: " << endl;
                cout << "HP + 150 for full bronze" << endl;
                yhp + 150;
                cout << "P.Def + 210 for full bronze and bronze shield" << endl;
                ypdef + 210;
                cout << "P.Atk + 75 for bronze sword" << endl;
                ypatk + 150;
            }


however programing it like this seems very...

oh whats the word...

time consuming and painful.

my other arrays (normal ones that just hold a set of int's) look like this:

1
2
    int *player[] = {&yhp,&ymp,&ypatk,&ymatk,&yratk,&ypdef,&ymdef,&yrdef,&ydb,&yacc,&ycr,&yas};
        


is there any way to do a string array in this way?
maybe i should use a vector? like this:

1
2
3
4
5
6
7
                vector<string> invintory1;
                invintory1.push_back("Bronze Breast");
                invintory1.push_back("Bronze Helm");
                invintory1.push_back("Bronze Gauntlet");
                invintory1.push_back("Bronze Shield");
                invintory1.push_back("Bronze Boots");
                invintory1.push_back("Bronze Sword");


but isn't it better to use an array over a vector?
but isn't it better to use an array over a vector?

That's an unusual opinion. I would say that the added functionality of a vector makes up for the slightly larger footprint it takes up in memory. So, I'd say use vectors where you can.
I would say that the added functionality of a vector makes up for the slightly larger footprint it takes up in memory. So, I'd say use vectors where you can.


im not really sure what you mean, as i havn't really tried working with vectors before.

what does a vector have that an array dosn't aside from iterating... which i suppose arrays have kind of...
The for loop is accessing an element beyond the array boundary.

Change this line:
 
    for (int s = 0; s < 7; ++s)
to:
 
    for (int s = 0; s < 6; ++s)


also the array declaration can be simplified:
1
2
3
4
5
6
7
    string invintory2[6] =
    {   "Gold Armor",
        "Gold Shield",
        "Gold Helemt",
        "Gold Boots",
        "Gold Guantlets",
        "Gold Sword" };
lel. im noob.

but if its accessing beyond the last element, wouldnt it display everything then the random text and not just the random text?
Did you try it?
std::string is a complex class. i don't know what would happen if the program tries to access an element which isn't there.
nvm
To answer your earlier question about vectors, not having to know the size of your array ahead of time and managing the for loops by setting the condition to: for(unsigned i = 0; i < YourVector.size(); ++i)//... is usually enough reason to use them alone. Otherwise check this out: http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.