Sign in to follow this  
stealth

DevC++ - multi dimensional char arrays???

Recommended Posts

depends:

const char *strings[10] =
{
"Hello World!",
"GameDev.net is teh fluff!",
//...
};



would work if, or if you want to be able to modify them:

char more_strings[10][MAX_STRING_LENGTH];



be a bit more specific and someone will point you in the right direction :)

Share this post


Link to post
Share on other sites
stealth    100
yeah sweet , I know I need to be a bit more specific I just dont know how to ask the question lol , I'm used to a different programming language where Strings are an actual type so I could go like this


String[1,10]


and it would give me 10 strings inside the first index


so basically what I want to do is make a character array like that

it's for Menu's and Sub Menu's for windows


so I want to make an array that holds the menu names , and I'm hoping to do something like


char MenuNames[1,10]

MenuName[1,1] = "File"

MenuName[1,2] = "Exit"


how do I go about dping this?

Share this post


Link to post
Share on other sites
petewood    819

#include <vector>
#include <string>

typedef std::vector<std::string> StringArray;

StringArray myStrings(10);





If you want to initialise it from DigitalDelusions strings array you can do

const char *strings[2] =
{
"Hello World!",
"GameDev.net is teh fluff!",
};

#include <vector>
#include <string>

int main() {
typedef std::vector<std::string> StringArray;
StringArray myStrings(string, strings+2);//this example has two strings
return 0;
}




edit:
If you want to initialise individually you can do

#include <vector>
#include <string>

int main() {
typedef std::vector<std::string> StringArray;
StringArray myStrings(2);//this example has two strings
myStrings[0] = "Hello World!";
myStrings[1] = "GameDev.net is teh fluff!",
return 0;
}




If you don't know how many strings you are going to have you can push a new string onto the (back) end of the vector (if you say myStrings[3] = "Goodbye, cruel world!";, it will crash as vector isn't the right size. You need to resize it first (which will destroy the data already in there or use the push_back member function which will reallocate if necessary and preserve the existing data).

#include <vector>
#include <string>

int main() {
typedef std::vector<std::string> StringArray;
StringArray myStrings;//zero size
myStrings.push_back("Hello World!");
myStrings.push_back("GameDev.net is teh fluff!"),
myStrings.push_back("STL makes me happy"),
return 0;
}


Share this post


Link to post
Share on other sites
stealth    100
Thanks , but I dont think thats it , what I want is to be able to:

-Store 10 strings with a length of anything from 1 to 256
-and then go thru the array and read all the strings

but what I want is to be able to go to the first index of the array and read the 10 strings saved in the first index , u know what I mean?

so when I write

char String[10][256]

I thort I could go

String[1][1] = "String1"
String[1][2] = "String2"

etc

it's a bit confusing for me , but thats basically what I want to be able to do


Share this post


Link to post
Share on other sites
well then you would want to do:

std::string weird[SOME_NUMBER][10];

//then you could do

weird[0][0] = "thingy1";
weird[0][1] = "thingy2";
//.. etc




and C/C++ starts indexing at 0 don't forget that.

edit: or if you want to use a resizable vector instead:

std::vector<std::string[10]> weird(initial_size);

Share this post


Link to post
Share on other sites
petewood    819
Quote:
Original post by stealth
Thanks mate I think thats what I was after , do I need to include the string header for that?


If you're talking about the one with just std::string then #include <string>

If you're talking about the one with std::vector then #include <vector> as well.

Share this post


Link to post
Share on other sites
stealth    100
it seems to work without including the string header, I dunno how to put my code into a code box thingy so I have to just post it like normal txt , anyway this works without including the string header



#include <iostream>
#include <stdlib.h>

using namespace std;

//declare global string array
// 10 index's which hold 10 strings each
std::string MenuName[10][10];

int LastIndex;


//function to check the last index of the string array which holds a value
int CheckArrayIndex()
{
int Total = 9;
int count = 0;

while(count < 9)
{
if(MenuName[0][count] != "" )
{
LastIndex = LastIndex+1;
}
count++;
}

}

int main(int argc, char *argv[])
{

//fill the first 3 strings of the first index
MenuName[0][0] = "&File";
MenuName[0][1] = "&New";
MenuName[0][2] = "E&xit";

CheckArrayIndex();

cout << LastIndex << endl;

for(int count = 0; count < LastIndex; count++)
{
cout << "String stored at array index " << count << "= " << MenuName[0][count] << endl;
}

system("PAUSE");
return 0;
}



Share this post


Link to post
Share on other sites
use [ source] [ /source] tags (without the spaces).

also when testing if a string is empty use myString.empty() instead.

Also it's good form to include the needed headers even if your current compiler and stl implementation doesn't require it.

Share this post


Link to post
Share on other sites
petewood    819
You can edit your post to put the [ source ] tags in. See the faq at the top of the page to learn about using the forums.

It is likely that <iostream> is including <string>. I wouldn't rely upon that though as it could change between compilers and library implementations and your code won't be portable if you do.

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