| | |
Declaring string array, initializing later...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 36
Reputation:
Solved Threads: 1
I am having problems with a code that I would like to go something liek this
I can't seem to get the nomenclature right. I've tried array[] and array[n] and permutations of those between the declaration and initialization. Please help!
:o
edit: in case it matters, the number of entries in each case is the same (20)
C++ Syntax (Toggle Plain Text)
string array; int z = rand()%2; switch(z) { case 0: array = {"entry 1","entry 2",...,"entry n"}; break; case 1: array = {"entry A","entry B",...,"entry (letter)"}; break; } //then do stuff with array
I can't seem to get the nomenclature right. I've tried array[] and array[n] and permutations of those between the declaration and initialization. Please help!
:o
edit: in case it matters, the number of entries in each case is the same (20)
•
•
Join Date: May 2006
Posts: 36
Reputation:
Solved Threads: 1
NOOOOOO!!! I was trying to avoid using pointers (I am still new enough to have n00b pointerphobia) but I'll work with your suggestion and see what I can come up with. But instead of using const int n;, would it work the same if the entries were strings? like
?
C++ Syntax (Toggle Plain Text)
string *array[20]; array = {"string 1","string 2",...}
use a vector if you want an array of strings.
C++ Syntax (Toggle Plain Text)
#include <string> #include <vector> using namespace std; int main() { vector<string> array(5); // initialize to be an array of 5 strings switch(z) { case 0: array[0] = "entry 1"; array[1] = "entry 2"; ... } }
•
•
Join Date: May 2006
Posts: 36
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Ancient Dragon
yet another method might be to use three string arrays
C++ Syntax (Toggle Plain Text)
string array1[] = { "entry 1","entry 2", ...}; string array2[] = { "entry A", "entry B", ..}; string array; ... switch(z) { case 0: array = array1; break; case 1: array = array2; break; ... };
Could I put it like this
C++ Syntax (Toggle Plain Text)
string array; switch(z) { case 0: string array1[] = {"string1","string2",...,"string20"}; array = array1; break; case 1: string array2[] = .... }
>>I like this one but I want to avoid declaring all strings at once and then choosing (honestly I might have dozens of 20-entry arrays, I'm not sure how many arrays I will need yet).
If all the strings are hard-coded in the program, they take up the same amount of memory wherever you put them. The compiler will put all those strings into some (probably) read-only memory block, so your concern is a non-issue. Probably a better solution is to put the strings in a text file then read them into memory at runtime. you can create a text file similar to standard ini file
your program will look for the tag it needs then read the strings until ene-of-file or another tag name is encountered. This method gives you a lot more flexibility because you can freely add more strings to the text file and not worry about adding/recompiling your program.
If all the strings are hard-coded in the program, they take up the same amount of memory wherever you put them. The compiler will put all those strings into some (probably) read-only memory block, so your concern is a non-issue. Probably a better solution is to put the strings in a text file then read them into memory at runtime. you can create a text file similar to standard ini file
C++ Syntax (Toggle Plain Text)
// strings.txt [entry-1] Entry 1 Entry 2 ... Entry n [entry-2] Entry A Entry B ... [entry-n] ...
•
•
Join Date: May 2006
Posts: 36
Reputation:
Solved Threads: 1
Good call about the memory. But I'm trying to keep the program from lagging noticably. If it initializes a whole bunch of 20-entry strings at once, it'll probably take a few seconds and the user will experience lag.
Thanks for your input you gave me alot of ideas. One of them was the following code that I have chosen to use:
Thanks for your input you gave me alot of ideas. One of them was the following code that I have chosen to use:
C++ Syntax (Toggle Plain Text)
string array[20]; switch { case 0: string array1[] = {"string1","string2",...,"string20"}; for(int i=0; i<20; i++) { array[i]=array1[i]; } case 1: ... }
I might go with something like this.
C++ Syntax (Toggle Plain Text)
static const string data[][20] = { { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" }, { "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40" }, }; const string *item = data [ rand() % (sizeof data / sizeof *data) ];
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: May 2006
Posts: 36
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Ancient Dragon
>>I like this one but I want to avoid declaring all strings at once and then choosing (honestly I might have dozens of 20-entry arrays, I'm not sure how many arrays I will need yet).
If all the strings are hard-coded in the program, they take up the same amount of memory wherever you put them. The compiler will put all those strings into some (probably) read-only memory block, so your concern is a non-issue. Probably a better solution is to put the strings in a text file then read them into memory at runtime. you can create a text file similar to standard ini file
your program will look for the tag it needs then read the strings until ene-of-file or another tag name is encountered. This method gives you a lot more flexibility because you can freely add more strings to the text file and not worry about adding/recompiling your program.C++ Syntax (Toggle Plain Text)
// strings.txt [entry-1] Entry 1 Entry 2 ... Entry n [entry-2] Entry A Entry B ... [entry-n] ...
![]() |
Other Threads in the C++ Forum
- Previous Thread: visual c++ .net 2003 command prompt
- Next Thread: struct tag question
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






