GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Apr-2004, 12:03
Psycop Psycop is offline
New Member
 
Join Date: Apr 2004
Posts: 2
Psycop is on a distinguished road
Question

Help a C++ Idiot: I am trying to fill an array based on a switch statement.


Ideally I would like to fill an array with different sets of strings as follows:
CPP / C++ / C Code:
string items[];

switch(itemset){
    case 0:
       items = {"frog","bat","toenail"};
       break;
     case 1:
       items = {"trouser","pillow","elbow","shotgun"};
       break;
...
...  etc;
};

I have tried using temporary arrays and copying them in but if you use the same one then the compiler whines about mulitple definitions. If you use separate ones it complains about some local variables not being initialsed by the end of the switch statement!
I could initialse all of the temp arrays outside of the switch and then use another method to copy them one by one into the array, but is there a less ugly way to do it?
I'm new to C++ but used to Java (where the answer would be items = temp; or items = new int[]{"blah","bloh"}; ). Can you make an anonimous array?

Thanks very much,
Psycop
Last edited by dsmith : 13-Apr-2004 at 18:11.
  #2  
Old 13-Apr-2004, 12:30
Psycop Psycop is offline
New Member
 
Join Date: Apr 2004
Posts: 2
Psycop is on a distinguished road
Well it seems that if I use a horrible bunch of if statements the problem is circumvented. I should think this is because the compiler is not detecting the fact that the other case statements are unreachable (due to breaks). Stupid compiler (borland bcc55). Is this true for them all, and more generally is there a more elegant solution to this?


Chars.
  #3  
Old 14-Apr-2004, 04:12
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Psycop
Well it seems that if I use a horrible bunch of if statements the problem is circumvented. I should think this is because the compiler is not detecting the fact that the other case statements are unreachable (due to breaks). Stupid compiler (borland bcc55). Is this true for them all, and more generally is there a more elegant solution to this?
Not a stupid compiler. An attempt at an illegal use of the language.

Are you going to modify the strings once you assign them to items? If not, define items as a pointer and load an address of the strings into the pointer:
Code:
string itemlist0[] = {"frog","bat","toenail"}; string itemlist1[] = {"trouser","pillow","elbow","shotgun"}; string *items; switch(itemset){ case 0: items = itemlist0; break; case 1: items = itemlist1; break; ... ... etc; };

If you are going to change the strings, your job is much harder.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
switch statement freedomJoe C Programming Language 0 27-Oct-2003 09:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.