User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,001 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,853 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 8639 | Replies: 15
Reply
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Declaring string array, initializing later...

  #1  
May 25th, 2006
I am having problems with a code that I would like to go something liek this
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)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2005
Posts: 15
Reputation: invinate is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
invinate invinate is offline Offline
Newbie Poster

Re: Declaring string array, initializing later...

  #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  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

  #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
string *array[20];
array = {"string 1","string 2",...}
?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Declaring string array, initializing later...

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

#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";
              ...
   }
}
Reply With Quote  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

  #5  
May 25th, 2006
Originally Posted by Ancient Dragon
yet another method might be to use three string arrays
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;
   ...
};

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
string array;
switch(z)
{
case 0:
string array1[] = {"string1","string2",...,"string20"};
array = array1;
break;
 
case 1:
string array2[] = ....
}
so that it's not initializing all those arrays at once, or will it still initialize array1, array2...etc?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Declaring string array, initializing later...

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 929
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Declaring string array, initializing later...

  #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
// strings.txt
[entry-1]
Entry 1
Entry 2
...
Entry n
[entry-2]
Entry A
Entry B
...
[entry-n]
...
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  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

  #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:
string array[20];
 
switch
{
     case 0: 
                string array1[] = {"string1","string2",...,"string20"};
                for(int i=0; i<20; i++)
                { array[i]=array1[i]; }
  
     case 1: 
                  ...
}
Reply With Quote  
Join Date: Apr 2004
Posts: 3,614
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Declaring string array, initializing later...

  #9  
May 25th, 2006
I might go with something like this.
   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) ];
Reply With Quote  
Join Date: May 2006
Posts: 36
Reputation: CStallion is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
CStallion CStallion is offline Offline
Light Poster

Re: Declaring string array, initializing later...

  #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
// strings.txt
[entry-1]
Entry 1
Entry 2
...
Entry n
[entry-2]
Entry A
Entry B
...
[entry-n]
...
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC