Archived

This topic is now archived and is closed to further replies.

elements in undefined arrays

This topic is 6367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m writing a little routine for a game of mine that generates random output in appropriate locations. I can get it to work the long way just fine, but I was wondering if there was another less redundant way. Here''s the long way.
void rand_gen(void)
{
    string rnd_inv;
    int rnd_num_inv;

    string inv1 = "That wasn''t one of the options.?";
    string inv2 = "Hey, nitwit, try choosing a VALID option!";
    string inv3 = "You have to choose a listed option.";

    rnd_num_inv = rand()%3;

    if (rnd_num_inv == 0)
        return inv1;
    else if (rnd_num_inv == 1)
        return inv2;
    else if (rnd_num_inv == 2)
        return inv3;
}
 
Now that might work fine, but I don''t feel like changing and adding all those freaking lines everytime I have to add just ONE more string. So, I''ve decided to try and come up with a better way using arrays. (This code is for the "Press any key" output, the other was for "Invalid Option" but it doesn''t matter...they do the same thing.)
#include 
#include 
#include 
#include 
#include 

int main()
{
    string rnd_pak;
    int rnd_num_pak;
    int i = 0;
    int count = 0;

    srand((unsigned)time(NULL));

    string pak[] = {"Press a key to continue",
                    "What are you waiting for?!",
                    "You can stop wasting my time."};

    for (i = 0; i < THIS_IS_THE_NO_OF_ELEMENTS_IN_ARRAY; i++)
        count++;

    rnd_num_pak = rand()%count;

    cout << endl << pak[rnd_num_pak];
}
 
I think the setup is correct, but how do I figure out the number of elements in the array to put in place of "THIS_IS_THE_NO_OF_ELEMENTS_IN_ARRAY". I don''t want to declare string pak[] with a set number of elements because I want it to dynamically change every time I add another string. Therefore if I wanted another possible output string, I would only have to add it inside the { } and that would be IT! I wouldn''t have to change anything else. Get what I''m saying? --- Joker2000 Stevie Ray Vaughan - The Legend

Share this post


Link to post
Share on other sites
Hey, Joker!

Well, I have really NO IDEA how to deal with "strings" and how they work (I mean these C++ ones eheh). But Id guess that some code like this would work :

    

int x, number_of_strings; // x = dummy counter


for (x = 0; x < sizeof (the_string); x ++)
if (the_string[x] == `\0`)
number_of_strings++;



That would check for the whole string contents how many NULL-chars there are. But here Im assuming the string thing is just 3 strings attached one to the back of the other, but Im just guessing as I dont know how those strings work.

Hope that helps.
Ah, Im sure theres a better way of knowing that hehe, but as you had no replies yet I just started the topic with this .

Cya,
-RoTTer

Share this post


Link to post
Share on other sites
If you want to dynamically add strings (runtime) you will need a linked list or just have an array of pointers to the message strings that surpasses your maximum string count.

ie. sipose you will have no more than 42 srtings

    
int numOfStrings;
char *strings[ 42 ];

strings[ 0 ] = "Message 1";
strings[ 1 ] = "message 2";
numOfStrings = 2;

char *message = strings[ rand() % numOfStrings ];


The linked list would take a bit more work.

OneEyeLessThanNone

everyone should get a catfish

Share this post


Link to post
Share on other sites
Now correct me if I''m wrong, but if you are using string then you must be using the STL. So then why not just do it the STL way and make a vector of strings to handle this array problem?

-> Briar LoDeran <-

Share this post


Link to post
Share on other sites
quote:

int numOfStrings;
char *strings[ 42 ];
strings[ 0 ] = "Message 1";
strings[ 1 ] = "message 2";
numOfStrings = 2;
char *message = strings[ rand() % numOfStrings ];



you should allocate memory first in order to be able to assign those strings to the string array and not get an exception in your code...


quote:

int x, number_of_strings; // x = dummy counter
for (x = 0; x < sizeof (the_string); x ++)
if (the_string[x] == `\0`)
number_of_strings++;



-you should assign 0 to number_of_strings before the for loop..
-sizeof(the_string) should be strlen(the_string)
-there''s an easier way to count the number of strings... using strchr() looking for a NULL char...

quote:

void rand_gen(void)
{
string rnd_inv;
int rnd_num_inv;
string inv1 = "That wasn''t one of the options.?";
string inv2 = "Hey, nitwit, try choosing a VALID option!";
string inv3 = "You have to choose a listed option.";
rnd_num_inv = rand()%3;
if (rnd_num_inv == 0)
return inv1;
else if (rnd_num_inv == 1)
return inv2;
else if (rnd_num_inv == 2)
return inv3;
}



by just glancing through your code, you cant return a value if the function''s return type is void... you gotta make it to return a "string"

btw, OneEyeLessThanNone''s idea is a good one.. use that, but allocate memory for the strings, or simply assign it like this...

char* str[30] =
{
"string 1",
"string 2",
"string 3",
"string 4",
"string 5"
};

Good luck with your function!


..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Unfortunately, there is no way to keep a count of the strings before-hand, at compile time.. Not without doing the book-keeping yourself.

A common way is to keep a NULL string at the end of the list to indicate the end. Then you can implement it like this:

    
static char* rv_options[] = {
"Slap any o ''dem keys now!",
"What are you waitin'' for? Christmas? Press a key!",
"Whack a key to continue ...",
NULL
};

int nOpt = 0;

while( rv_options[nOpt] != NULL )
++nOpt;

return( rv_options[rand() % nOpt] );

////////////////////////////////////////////////////////////

// Or, if you wanted to bum the running time down a little
// bit for long lists, you could implement it like this:

static nOpt = -1;
static char* rv_options[] = {
"Slap any o ''dem keys now!",
"What are you waitin'' for? Christmas? Press a key!",
"Whack a key to continue ...",
NULL
};

if(nOpt == -1)
{
// Count the items.. just once

nOpt = 0;

while( rv_options[nOpt] != NULL )
++nOpt;
}

return( rv_options[rand() % nOpt] );


// CHRIS

Share this post


Link to post
Share on other sites