Declaring string array, initializing later...

Reply

Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Declaring string array, initializing later...

 
0
  #1
May 25th, 2006
I am having problems with a code that I would like to go something liek this
  1. string array;
  2. int z = rand()%2;
  3. switch(z)
  4. {
  5. case 0:
  6. array = {"entry 1","entry 2",...,"entry n"};
  7. break;
  8.  
  9. case 1:
  10. array = {"entry A","entry B",...,"entry (letter)"};
  11. break;
  12. }
  13.  
  14. //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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 17
Reputation: invinate is an unknown quantity at this point 
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

Re: Declaring string array, initializing later...

 
0
  #2
May 25th, 2006
what you need is an array of pointers to string, like
const int n = 10;
string *array[n];

array = {"1", "2", ..., "10"};

try it
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

 
0
  #3
May 25th, 2006
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
  1. string *array[20];
  2. array = {"string 1","string 2",...}
?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,171
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Declaring string array, initializing later...

 
0
  #4
May 25th, 2006
use a vector if you want an array of strings.

  1. #include <string>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. vector<string> array(5); // initialize to be an array of 5 strings
  8.  
  9. switch(z)
  10. {
  11. case 0:
  12. array[0] = "entry 1";
  13. array[1] = "entry 2";
  14. ...
  15. }
  16. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

 
0
  #5
May 25th, 2006
Originally Posted by Ancient Dragon
yet another method might be to use three string arrays
  1. string array1[] = { "entry 1","entry 2", ...};
  2. string array2[] = { "entry A", "entry B", ..};
  3. string array;
  4. ...
  5. switch(z)
  6. {
  7. case 0: array = array1; break;
  8. case 1: array = array2; break;
  9. ...
  10. };
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). But you gave me an idea.
Could I put it like this
  1. string array;
  2. switch(z)
  3. {
  4. case 0:
  5. string array1[] = {"string1","string2",...,"string20"};
  6. array = array1;
  7. break;
  8.  
  9. case 1:
  10. string array2[] = ....
  11. }
so that it's not initializing all those arrays at once, or will it still initialize array1, array2...etc?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,171
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Declaring string array, initializing later...

 
0
  #6
May 25th, 2006
The code I posted earlier was wrong -- I edited it to use correct code.

Arrays can only be initialized the way you are trying to do it when the array is first declared. After that you have to initialize it one at a time.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,171
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1439
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Declaring string array, initializing later...

 
0
  #7
May 25th, 2006
>>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
  1. // strings.txt
  2. [entry-1]
  3. Entry 1
  4. Entry 2
  5. ...
  6. Entry n
  7. [entry-2]
  8. Entry A
  9. Entry B
  10. ...
  11. [entry-n]
  12. ...
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

 
0
  #8
May 25th, 2006
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:
  1. string array[20];
  2.  
  3. switch
  4. {
  5. case 0:
  6. string array1[] = {"string1","string2",...,"string20"};
  7. for(int i=0; i<20; i++)
  8. { array[i]=array1[i]; }
  9.  
  10. case 1:
  11. ...
  12. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,321
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Declaring string array, initializing later...

 
0
  #9
May 25th, 2006
I might go with something like this.
  1. static const string data[][20] =
  2. {
  3. {
  4. "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
  5. "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
  6. },
  7. {
  8. "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
  9. "31", "32", "33", "34", "35", "36", "37", "38", "39", "40"
  10. },
  11. };
  12. 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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

 
0
  #10
May 25th, 2006
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
  1. // strings.txt
  2. [entry-1]
  3. Entry 1
  4. Entry 2
  5. ...
  6. Entry n
  7. [entry-2]
  8. Entry A
  9. Entry B
  10. ...
  11. [entry-n]
  12. ...
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.
I would eventually like to have the program linked to a text file so that others (I do plan to distribute someday) can add to the program themselves. But unfortunately at this time I don't know how to work with such text files! Do you think I should learn this now before I get to far into coding the program?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC