Sign in to follow this  
XDaWNeDX

Arrays?

Recommended Posts

XDaWNeDX    113
So currently I have my console application game (mhm, the full RPG and everything.:D)

But, I have every single map created externally, in a .TBG file.
When I load it, I have to count how many numbers across there are
and how many down there are
then create an array with those variables, and load it into there.

Is there a way to create a char array so I can load the file into that array, and however many numbers across/down will be the constants in the char array[x][y] ?

I'm using C++ by the way.

Share this post


Link to post
Share on other sites
mhagain    13430
[quote name='rip-off' timestamp='1307002772' post='4818568']
Have you tried using a standard container, such as std::vector?
[/quote]
I think this is more about how to load the values correctly from disk than it is about the implementation of the container, to be honest.

Share this post


Link to post
Share on other sites
XDaWNeDX    113
I can't post the code cuz I only get internet from about 4PM till 9:30PM.

But uhhh I haven't tried anything at all. I can post some real basic code as to what I'm doing though (on my phone, yeah there's mistakes... )

Char chararray[11[11];
ifstream MyFileI;
MyFileI.open("test.tbg");
For (int i = 0; i <10;i++)
MyFileI >> chararray[i]
MyFileI.close();

And that's basically what I use to load my maps. Is there a way to get the char array to automatically loop and load as many lines as there are, rather than hardcoding it?

But I believe a vector would work. I have the code with me, but no compiler. Just notepad++ to do some editing on the go. Never know when a great idea will hit you. :)

Share this post


Link to post
Share on other sites
rip-off    10979
Consider getting a "mobile" compiler, if you can. Something like wxDev-C++ on a USB stick can do.

A vector should suffice. Initially you can use vector<vector>:
[code]
typedef std::vector<char> MapRow;
typedef std::vector<MapRow> Map;

void getRow(std::ifstream &in, MapRow &row)
{
row.clear();
char c;
while(in >> c)
{
row.push_back(c);
}
}

void load(const std::string &filename, Map &map)
{
std::ifstream in(filename.c_str());

MapRow row;
while(getRow(in, row))
{
map.push_back(row);
}
}
[/code]
This code doesn't check that the map is rectangular.

Share this post


Link to post
Share on other sites
NightCreature83    5006
[quote name='rip-off' timestamp='1307026424' post='4818681']
Consider getting a "mobile" compiler, if you can. Something like wxDev-C++ on a USB stick can do.

A vector should suffice. Initially you can use vector<vector>:
[code]
typedef std::vector<char> MapRow;
typedef std::vector<MapRow> Map;

void getRow(std::ifstream &in, MapRow &row)
{
row.clear();
char c;
while(in >> c)
{
row.push_back(c);
}
}

void load(const std::string &filename, Map &map)
{
std::ifstream in(filename.c_str());

MapRow row;
while(getRow(in, row))
{
map.push_back(row);
}
}
[/code]
This code doesn't check that the map is rectangular.
[/quote]

An other option is to write out the array size to the file and read that in first and then construct the arrays and loop.


Share this post


Link to post
Share on other sites
XDaWNeDX    113
[quote name='rip-off' timestamp='1307026424' post='4818681']
Consider getting a "mobile" compiler, if you can. Something like wxDev-C++ on a USB stick can do.

A vector should suffice. Initially you can use vector<vector>:
[code]
typedef std::vector<char> MapRow;
typedef std::vector<MapRow> Map;

void getRow(std::ifstream &in, MapRow &row)
{
row.clear();
char c;
while(in >> c)
{
row.push_back(c);
}
}

void load(const std::string &filename, Map &map)
{
std::ifstream in(filename.c_str());

MapRow row;
while(getRow(in, row))
{
map.push_back(row);
}
}
[/code]
This code doesn't check that the map is rectangular.
[/quote]

I know of a few mobile compilers. The problem isn't that I can't get one... It's I'm too lazy to go and download/install onto usb drive lol


Plus, I'm at HOME from 3 and on, and the rest of the day I'm at school or working. My mom just limits my internet time... D: Ah well, I can always stream internet through my phone.


I have never used vectors before, and refuse to use something without understanding it. Where could I get some GOOD information on vectors?

And thanks for being so helpful and responding so quickly!

[quote name='NightCreature83' timestamp='1307032349' post='4818718']

An other option is to write out the array size to the file and read that in first and then construct the arrays and loop.



[/quote]

You can't create an array with a nonconstant value. Believe me... I've tried chararray[x][y]
Unless x/y have been defined or are a static const(I believe these would work) it will result in a compiling error.

So I SUPPOSE I could read it into a static const value, but I would prefer vectors. Something new to learn.

Share this post


Link to post
Share on other sites
frob    44974
[color=#1C2837][size=2][quote]I have never used vectors before, and refuse to use something without understanding it. Where could I get some GOOD information on vectors?[/quote][/size][/color]
[color=#1C2837][size=2]Books about C++.[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[size="2"][color="#1c2837"]Since it seems you don't even know the basics of the C++ standard library, I suggest you start with "Accelerated C++" by Koenig and Moo. [/color][/size]

Share this post


Link to post
Share on other sites
XDaWNeDX    113
[quote name='frob' timestamp='1307035163' post='4818744']
[color="#1c2837"][size="2"][quote]I have never used vectors before, and refuse to use something without understanding it. Where could I get some GOOD information on vectors?[/quote][/size][/color]
[color="#1c2837"][size="2"]Books about C++.[/size][/color]
[color="#1c2837"][size="2"]
[/size][/color]
[size="2"][color="#1c2837"]Since it seems you don't even know the basics of the C++ standard library, I suggest you start with "Accelerated C++" by Koenig and Moo. [/color][/size]
[/quote]

I read most of sam's teach yourself c++ in 21 days. Well, I am reading it. I know C++ pretty well, I believe. I just wasn't taught much of the c++ standard library, like you said.

Is there a good place to get information, without spending money, on the standard library?

Share this post


Link to post
Share on other sites
frob    44974
[quote name='beatlefan' timestamp='1307037018' post='4818771']
I would use this: [url="http://www.cplusplus.com/reference/"]http://www.cplusplus.com/reference/[/url]
[/quote]

That is a good reference.

However, it is a REFERENCE.

It is not an introduction or tutorial, nor does it have a good discussion of when to use what or the software patterns involved. It assumes you have already been properly introduced and tutored.


I don't know of any web pages that give a solid learning experience for the standard library. I do know several dead-tree books that do so, such as the "Accelerated C++" book I listed above which is one of the best for beginners.

For more advanced C++ programmers wanting to learn just the standard library, "The C++ Standard Library: A Tutorial and Reference" by Josuttis is a heavyweight book that covers the standard library before TR1. It is not for beginners. It goes quite deep and covers everything in the Standard Library in the original standard. There have been some updates to the standard since that book was written, but the things in the book still apply, and the book still covers the vast majority of the C++0x standard library even with the new revisions since most of it is not significantly altered.

Share this post


Link to post
Share on other sites
Narishma    119
Here's a good free book: [url="http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html"]http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html[/url]

Share this post


Link to post
Share on other sites
owl    376
std is a library with a bunch of classes to help you do things like arrays in an object oriented manner. Take a look at this [url="http://www.codeguru.com/Cpp/Cpp/cpp_mfc/stl/article.php/c4027"]tutorial[/url].

If you can't access the intarweb, your best shot is borrowing some books from a library.

Share this post


Link to post
Share on other sites
NightCreature83    5006
[quote name='XDaWNeDX' timestamp='1307032422' post='4818719']
[quote name='rip-off' timestamp='1307026424' post='4818681']
Consider getting a "mobile" compiler, if you can. Something like wxDev-C++ on a USB stick can do.

A vector should suffice. Initially you can use vector<vector>:
[code]
typedef std::vector<char> MapRow;
typedef std::vector<MapRow> Map;

void getRow(std::ifstream &in, MapRow &row)
{
row.clear();
char c;
while(in >> c)
{
row.push_back(c);
}
}

void load(const std::string &filename, Map &map)
{
std::ifstream in(filename.c_str());

MapRow row;
while(getRow(in, row))
{
map.push_back(row);
}
}
[/code]
This code doesn't check that the map is rectangular.
[/quote]

I know of a few mobile compilers. The problem isn't that I can't get one... It's I'm too lazy to go and download/install onto usb drive lol


Plus, I'm at HOME from 3 and on, and the rest of the day I'm at school or working. My mom just limits my internet time... D: Ah well, I can always stream internet through my phone.


I have never used vectors before, and refuse to use something without understanding it. Where could I get some GOOD information on vectors?

And thanks for being so helpful and responding so quickly!

[quote name='NightCreature83' timestamp='1307032349' post='4818718']
An other option is to write out the array size to the file and read that in first and then construct the arrays and loop.



[/quote]

You can't create an array with a nonconstant value. Believe me... I've tried chararray[x][y]
Unless x/y have been defined or are a static const(I believe these would work) it will result in a compiling error.

So I SUPPOSE I could read it into a static const value, but I would prefer vectors. Something new to learn.
[/quote]
Every heard of new [] and delete[] they are used to create arrays on the heap, the whole point of it is that you don't know the size before run time so it has to be a dynamic array. A vector is nothing more than a stl implementation of a dynamic array.

Share this post


Link to post
Share on other sites
Krohm    5031
[quote name='NightCreature83' timestamp='1307032349' post='4818718']An other option is to write out the array size to the file and read that in first and then construct the arrays and loop.[/quote]I want to quote this. It is very important to have a [b]clear structure on persistent data[/b]. In general, the idea of "loop until everything is consumed" is very bad. Additional meta-data is useful and typically necessary. I don't count anymore the number of times the "loop until there's no more data" concept has screw me big way - especially when the format was later expanded to include more data. Headers are necessary, and I suggest everyone to look in that direction.
First problem first: [b]how you encode what in persistent storage.[/b]
Second problem: [b]how you store it at runtime.[/b]


You can use every possible container you want but this doesn't change the fact that the structure on disk should have a clear interpretation, besides those implementation oriented details.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this