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";
...
}
}
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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) ];
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
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.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
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.
Ancient Dragon
Retired & Loving It
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I didn't check very closely, but perhaps there may be an initial taste in the Code Snippets .
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314