Creating and outputting string arrays

Hey everyone.

I am making a console RPG game.
I am trying to set-up a very basic inventory system, where there would be an array full of strings that encompass the items in your inventory. An example (though probably not syntactically correct) would be.

 
string inventory[20] = {"Apple","Knife","etc"...};


and then I would have functions in the program to cout this array:

1
2
3
4
5
int example(){
    for(i=0;i++){
        cout << inventory[i] << endl;
    }
}


I use these code samples to give the gist of what I want to happen; how would I actually write this code for it to function?
i'd use a vector rather than a c-style evil array.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<vector>
#include<string>
#include<iostream>

int main()
{
	std::vector<std::string> inventory;
	inventory.push_back("apple");
	inventory.push_back("knife");
	inventory.push_back("dildo");
	inventory.push_back("banana");
	
	std::cout << "Player's inventory:" << std::endl;

	// or iterate using a constant iterator
	for (int i = 0; i < inventory.size(); ++i)
	{
		std::cout << inventory.at(i) << ", ";
	}

	return 0;
}


you might want to create some classes to keep it 'modular'.
Last edited on
Well, you'd need to know the size of that array, to iterate over each element the way you're doing.

I'd strongly recommend that you use std::vector rather than C-style arrays.

EDIT: Ninja'd.
Last edited on
Thanks for the information!
I'll start researching vectors because I don't know what '.pushback' means.

And what is Ninja'd?
he means i gave you a reply before he pressed "submit".
Just wanted to chime in a bit because you will see a lot on this forum that people are really against arrays here. Though that doesn't mean they are evil or bad to use. On the contrary actually sometimes arrays are a best fit then std::vector<> is.

The main thing you need to know is when to use them and when it would be better to use std::vector<> instead. Like every container it comes down to what your requirements are.

One of the most basic rules for arrays vs vectors is this.

If you need a storage container that is dynamic in nature you are going to want to use std::vector<> over an array. By dynamic in nature I mean that over the lifetime of the container object the number of elements stored in it will change, either by adding new elements or deleting some.

Otherwise if you are 100% certain that the number of elements will not be changing over the lifetime of the container object you should instead use an array. There is various reasons behind why you should do this but they are a bit more advanced topics (Where they are stored in memory, performance concerns, etc.).

I also should say that while I don't personally find C-style arrays evil (As long as they are used correctly) you really should use std::array<> instead of C-style arrays when the situation calls for an array. It will provide you basically all the benefits of a C-style array along with additional functionality that allows you to work more easily with the STL.

But anyways just wanted to point out that while yes most of the time an std::vector<> will probably be the best solution (Like it seems for your case) it is not a catch all container that you always use. It is best to always look at the requirements you have and then choose the best container from them.

I'll start researching vectors because I don't know what '.pushback' means.
std::vector<>::push_back() is basically just saying that you wish to "push" this object onto the back of the container. Meaning it will resize the container so that the new object can fit in it and then add that element to the back of the container. So for example here is a std::vector<int> example of this.

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
#include <vector>
#include <iostream>

int main()
{
    // Notice how you can use initialization lists with std::vector<> also.
    std::vector<int> container = {1, 2, 3};

    // Now lets say we want to add a new number to the container, the number 4
    // We just call the push_back() method and it will automatically resize the container
    // if it is needed and add that number to the back of the container.
    container.push_back(4);

    // Now container[3] = 4
    std::cout << "container[3] = " << container[3] << std::endl;

    // Just printing to show all the elements in the container to help     
    // Uses C++11 code but you can ignore this it is just printing everything in the vector
    std::cout << "Printing all elements!" << std::endl;
    for (int i : container)
    {
        std::cout << i << std::endl;
    }
    
    return 0;    
}


So that is a quick demo of what push_back does and also note that you can use initialization lists to create the vector just like you do with arrays.

Hopes this helps clarify some things and sorry bout such a long post it is a bad habit of mine ;p.
Last edited on
Topic archived. No new replies allowed.