• 10
• 9
• 13
• 10
• 18

# Arrays?

This topic is 2482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

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 on other sites
Have you tried using a standard container, such as std::vector?

##### Share on other sites

Have you tried using a standard container, such as std::vector?

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 on other sites
Perhaps. The question is vague. It isn't clear how the OP has tried.

@OP: what have you tried? Can you post any code you currently have?

##### Share on other sites
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
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 on other sites
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>:
 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); } } 
This code doesn't check that the map is rectangular.

##### Share on other sites

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>:
 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); } } 
This code doesn't check that the map is rectangular.

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 on other sites

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>:
 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); } } 
This code doesn't check that the map is rectangular.

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!

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.

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 on other sites
[color=#1C2837][size=2]
I have never used vectors before, and refuse to use something without understanding it. Where could I get some GOOD information on vectors?[/quote]
[color=#1C2837][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.

##### Share on other sites

[color="#1c2837"]
I have never used vectors before, and refuse to use something without understanding it. Where could I get some GOOD information on vectors?

[color="#1c2837"]

[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.
[/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?