DevC++ - multi dimensional char arrays???
depends:
would work if, or if you want to be able to modify them:
be a bit more specific and someone will point you in the right direction :)
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 :)
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?
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?
#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;}
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
-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
well then you would want to do:
and C/C++ starts indexing at 0 don't forget that.
edit: or if you want to use a resizable vector instead:
std::string weird[SOME_NUMBER][10];//then you could doweird[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);
Thanks mate I think thats what I was after , do I need to include the string header for that?
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.
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;
}
#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;
}
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement