•
•
•
•
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
![]() |
•
•
Join Date: May 2006
Posts: 36
Reputation:
Rep Power: 3
Solved Threads: 1
I am having problems with a code that I would like to go something liek this
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)
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 arrayI 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)
•
•
Join Date: May 2006
Posts: 36
Reputation:
Rep Power: 3
Solved Threads: 1
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",...}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation:
Rep Power: 38
Solved Threads: 929
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";
...
}
}•
•
Join Date: May 2006
Posts: 36
Reputation:
Rep Power: 3
Solved Threads: 1
•
•
•
•
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[] = ....
}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation:
Rep Power: 38
Solved Threads: 929
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,117
Reputation:
Rep Power: 38
Solved Threads: 929
>>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
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.
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] ...
•
•
Join Date: May 2006
Posts: 36
Reputation:
Rep Power: 3
Solved Threads: 1
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:
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) ];•
•
Join Date: May 2006
Posts: 36
Reputation:
Rep Power: 3
Solved Threads: 1
•
•
•
•
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
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.// strings.txt [entry-1] Entry 1 Entry 2 ... Entry n [entry-2] Entry A Entry B ... [entry-n] ...
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?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: visual c++ .net 2003 command prompt
- Next Thread: struct tag question



Linear Mode