Practical use of code?

I have had no issue learning the use of cout and if and else if and while/for, #include <cstdlib> .etc because all of these things make sense to me and serve obvious non-situational practical use in coding. But some of the stuff that I've been learning such as arrays, I don't understand how you would make use of them in making a program or game. Like why would I want to use a string array then call out what character is the 22nd in the array? That just seems like a lot of work to call out a single character.

There's a few other things I've wondered about as well but it's late and I cannot recall them right now, I will reply to this thread to add more if/when it comes to me.
Why would you use an array of names?

string names[22] = {/*list the names here*/};

vs.
1
2
3
4
string name1 = name1;
string name2 = name2;
...
string name22 = name22;

Without even getting into the utility of arrays, which way do you think is more work?
Last edited on
I guess I should have mentioned. I'm getting into C++ because I'm interested in game development. C++ is most widely used in windows and windows is most used for gaming.
So could you give me an example of the use of an array in a video game?
Don't you want a container to store all the entities/game objects in? Also, I am not sure where the "c++ is most widely used in windows" comes from. It is used on *nix a lot as well. Maybe you are mixing c++ and c# up?
Didn't know about c# tbh. I guess if I ever had seen it I probably confused it with c sharp? c sharp is regular c right?
Sorry I'm a bit of a newb. If I learn c++ would I be able to use much of what I learned in c#? Actually http://en.wikipedia.org/wiki/Game_programming Scroll down a bit on the right hand side and the words "widely used" right beside C++, I didn't even view this page before making that post though, lol. It does state that c# is generally limited to windows platforms(so that page also states). So all in all I think that I would prefer to learn C++ as it seems more versatile from what little I know, although learning c# at some point would not hurt.

By game objects do you mean items, potions .etc? and entities? Some example code of that in action would explain a great deal more than an explanation. Seeing is believing and all that. Thanks for all help provided btw.
C# is closer to Java than C++. If you want to see example code, look through this book:
http://www.amazon.com/Beginning-C-Through-Game-Programming/dp/1435457420

Using arrays, or more specifically vectors, to hold inventory items is a perfect example of where they come in handy.
c# is c sharp. Game objects/entities I mean like cars, enemies, guns, potions, walls, ect...

You can develop games in c++, c#, java, and other languages I think c++, c#(c sharp), and java are probably the most common though.

If you use a game engine I believe unreal engine uses c++ or a language very similar and unity you can use c sharp, unityscript(javascript), or boo(python-like), there are other game engines out there though.

I would pick a language and stick with it then after you get the basics and some of the intermediate stuff done then you could try another language.

I would say c++ is a good language of choice. Java and C# use references for almost everything though and everything is within a class so they have forced OOP while c++ doesn't have forced OOP.

PS the part when I asked if you were mixing them up is because you said c++ is mostly used on windows which I think if we are talking about a platform specific I would think of c#.
Last edited on
Yeah, I plan to stick to C++. I certainly know of a few easier languages to learn but C++ is one of the most flexible that I know of. Besides I'm not the smartest person around and I'd probably have the issue of getting errors in my code from entering C# code into C++ or vice versa.

Wouldn't a vector be better for an inventory over an array since it would kill the empty spaces when something is removed from the inventory and perhaps an array would be better for a character gear equip screen? Of course that may also change between making a console game and making a game with actual graphics(I would think).

I guess it'd be easier to ask if a vector would also work for making an inventory. I still know very little about the implementation of vectors.
Last edited on
If i remember right, the book I linked earlier uses an inventory to explain vectors. About the only time an array would be better than a vector would be when the contents (or amount of the contents) will not change. Suppose you your character has an inventory limit of 10 items. He starts off with 3 items and you want to display his inventory.
1
2
3
4
for (int i = 0; i < inventory.size(); ++i)
{
    cout << inventory[i];
}

After the 3rd item it will print garbage if you used an array, but only print the 3 items if it is a vector. Then if you remove the 1st item from the array and run that same block of code, the array will print garbage, followed by the 2 remaining items, then garbage again for the remainder of the array. The vector will only print the 2 remaining items.
Topic archived. No new replies allowed.