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)

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

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

try it

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",...}

?

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";
              ...
   }
}

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?

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.

>>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.

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: 
                  ...
}

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) ];

>>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?

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?

Yup, working with text files is definitely something you need to learn, either now or later. Fact.

Yup, working with text files is definitely something you need to learn, either now or later. Fact.

The book that I'm using (C++ for dummies) doesn't cover that. Any tutorials or resources you'd recommend to get started?

look you your book for fstream class. If it isn't there then you wasted your money buying that book -- pawn it off on some other sucker.

This article has been dead for over six months. Start a new discussion instead.