random words

Started by
14 comments, last by da_cobra 22 years, 5 months ago
Those are the usual arguments for main, but you can overload main to take no parameters: int main(void).

int argc - the number of command line parameters (arg ument c ount). Always >= 1, since the first argument is the program name.
char *argv[] - an array of strings (which are arrays of chars) (arg ument v alues). argv[0] is the program name (1st parameter) and so on.
Advertisement
Oluseyi wrote:

// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;

Never do this, because global variables are wanted to be avoided. Not only do they use up a lot more space, but they can be changed by any function, which eliminates the purpose of encapsulation in C++. You may, however, define MAX_WORDS and MAX_CHARS as follows:

#define MAX_WORDS 5
#define MAX_CHARS 10

What this does is it looks in your entire program before compiling and replaces wherever it finds MAX_WORDS with 5. Same with MAX_CHARS. Notice also that there is no '';'', because you only want to replace a number, not a statement. These statements should go right below your #include directives.

You may also define a constant integer, like above, but you must pass it to all the functions that will use it, and it will not be able to be changed. The last method is becomming more and more popular.



-Ace
-Ace
quote:Original post by ace_muncher
Oluseyi wrote:

// note that these are global; they are declared outside *any* function
const int MAX_WORDS = 5;
const int MAX_CHARS = 10;

Never do this, because global variables are wanted to be avoided. Not only do they use up a lot more space, but they can be changed by any function, which eliminates the purpose of encapsulation in C++.

That's not true. Since the globals MAX_WORDS and MAX_CHARS are declared with the 'const' keyword they are constants. This means that the program is not allowed to change them. And in the case of an optimizing compiler their values will be inserted directly into the code (just like with a macro).

Declaring consts is actually to prefered to declaring macros.
quote:
You may also define a constant integer, like above, but you must pass it to all the functions that will use it, and it will not be able to be changed.

No, you won't have to pass it to every function that uses it, because it is declared in the global scope (just make sure it is declared *before* any function that uses it).

Edited by - Dactylos on October 15, 2001 12:44:27 AM
Guys, your killing yourselves with trying to define the bounds of the array. If you know what the array will be at compile time, you don't have to give it bounds.

    const char *cStringArray[] = {    "The cat ran up the hill",    "the cow jumped over the moon",    "there's a hole...5...10...he's running over people! Oh you Herschel Walker!",    ""};void function(void){   int NumStrings;   char *cRandomString;   // find the number of strings   for (NumStrings= 0; cStringArray[NumStrings][0] != 0; NumStrings++)   {}      // now NumStrings is the number of strings in array   cRandomString = cStringArray[rand()%NumStrings];   // now you have a random string in cRandomString}    






Edited by - BeerNutts on October 17, 2001 2:11:14 PM

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

quote:Original post by da_cobra
I think that last one could work, so now I try that to put it in a function like this :

void newword(char *word) ;

void main()
{
char word[10]="" ;
newword(word) ;
cout << word << endl ;
}

void newword(char *word)
{
int const words=5 ;
int const max=10 ;
int a=0 ;
char list [words] [max] =
{
"word", "car", "house", "dog", "cat"
} ;
int word_index = rand() % words ;
word = list[word_index];
}

but my word stays "" ?!?

what am I doing wrong now?
and no I can''t use : *word=list[word_index] ;
because then I get the following error :
error C2440: ''='' : cannot convert from ''char [10]'' to ''char''
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

again thanx in advance

Edited by - da_cobra on October 14, 2001 12:49:57 PM



This is almost correct. You just need to include string.h at the top of your program, and replace

word = list[word_index];

with

strcpy(word, list[word_index]);
Why hasn''t someone suggested to use words out of a text file? Read the text file once counting each word (typically seperated by a space, carriage return, or punctuation mark) then select a random value between 0 and the number of counted words. This way you can add whatever words you want to the game easilly. I did something like this for a word search type game, it worked well.

This topic is closed to new replies.

Advertisement