Ok well I have 4 char arrays. Each array has predefined words in them. For example:

char *article[MAX_ARTICLES] =
      { "the", "a", "one", "some", "every", "any" };

I'm trying to make random sentences as defined by the user. So if they type in the value 567 or whatever it may be I use it as the seed for srand. I'm trying to print whats in the value of the array. So if the word "every" was print it would need would need some seed value mod 6. I tried this

cout << article[srand(seed_val) % MAX_ARTICLES];

Just to test if I was accessing a random element but it was to no avail because its invalid syntax. Is there another way I can do this? I'm sure I can make variables and assign the new random value every time but I want to make this efficient. Any ideas?

If you really want to keep it all on one line like that, something like this is generally a sure fire way to make this sorta thing work:

cout << article[(static_cast<int>(seed_val) % MAX_ARTICLES)];

I tried something and this works nicely.

cout << article[rand() % MAX_ARTICLES];

Now that I got that fixed I have a problem. I'm gonna make an array called sentence that I want to add these random words to. Is there an easy function instead of strcat? I don't want to have to use strcat 15 times to I can concat the word then a space, word then a space etc.

Well you can define a variable of type "string" and then use += to add the words to it

for example:

string myString;

for (int i =0; i < maxwords; i++){
  myString += randomwords[];
  myString += " "; 
}

That would be the way I would go because I hate char. But I need a way to do it without using string type.

Well if you cannot use string and have to use a char*, then you will need to use either strcpy or strcat or memcpy or memove ..

I got this and it works well, but I get a warning.

Warning 2 warning C4996: 'strcat' was declared deprecated

char sentence[MAX_SENTENCE] = {0};
      strcat(sentence, article[rand() % MAX_ARTICLES]);
      strcat(sentence, " ");
      strcat(sentence, noun[rand() % MAX_NOUNS]);
      strcat(sentence, " ");
      strcat(sentence, verb[rand() % MAX_VERBS]);
      strcat(sentence, " ");
      strcat(sentence, preposition[rand() % MAX_PREPS]);
      strcat(sentence, " ");
      strcat(sentence, article[rand() % MAX_ARTICLES]);
      strcat(sentence, " ");
      strcat(sentence, noun[rand() % MAX_NOUNS]);

Visual C++ has deprecated most of the string functions, so you can add the define _CRT_SECURE_NO_WARNINGS to disable it or use strcat_s (but then the code will only compile with MSVC)

Thats what it was. I've used strcpy_s but didn't know it would apply to all the other str functions. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.