944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 34965
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 25th, 2006
0

Declaring string array, initializing later...

Expand Post »
I am having problems with a code that I would like to go something liek this
C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006
May 25th, 2006
0

Re: Declaring string array, initializing later...

what you need is an array of pointers to string, like
const int n = 10;
string *array[n];

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

try it
Reputation Points: 10
Solved Threads: 0
Light Poster
invinate is offline Offline
25 posts
since Oct 2005
May 25th, 2006
0

Re: Declaring string array, initializing later...

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)
  1. string *array[20];
  2. array = {"string 1","string 2",...}
?
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006
May 25th, 2006
0

Re: Declaring string array, initializing later...

use a vector if you want an array of strings.

C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
May 25th, 2006
0

Re: Declaring string array, initializing later...

Quote originally posted by Ancient Dragon ...
yet another method might be to use three string arrays
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006
May 25th, 2006
0

Re: Declaring string array, initializing later...

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
May 25th, 2006
0

Re: Declaring string array, initializing later...

>>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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
May 25th, 2006
0

Re: Declaring string array, initializing later...

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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006
May 25th, 2006
0

Re: Declaring string array, initializing later...

I might go with something like this.
C++ Syntax (Toggle Plain Text)
  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) ];
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 25th, 2006
0

Re: Declaring string array, initializing later...

Quote 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
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Light Poster
CStallion is offline Offline
36 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: visual c++ .net 2003 command prompt
Next Thread in C++ Forum Timeline: struct tag question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC