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?

Dani AI

Generated

A few practical points that tie the thread together and avoid the pitfalls you hit.

First, don’t try to use srand(...) inside an expression: srand seeds the generator and has no return value, so it can’t be used to produce an index. Seed once (at program start or when you want reproducible output), then call the generator repeatedly. Re-seeding before every pick will usually give poor results.

For modern, predictable and bias-free selection prefer <random>. It’s easy to seed with a user number for repeatable output and then draw uniform indices:

#include <random>
#include <sstream>

std::mt19937 rng(static_cast<unsigned int>(seed_val));   // deterministic seed
std::uniform_int_distribution<int> dist(0, MAX_ARTICLES - 1);

std::ostringstream out;
for (int i = 0; i < wordCount; ++i) {
  out << articles[dist(rng)];
  if (i + 1 < wordCount) out << ' ';
}
std::string sentence = out.str();

If you must stay with C-style strings, avoid repeated strcat. Track the write position and use snprintf to append safely, checking return values so you never overflow the buffer:

char sentence[MAX_SENTENCE] = "";
size_t left = MAX_SENTENCE;
char *p = sentence;
for (int i = 0; i < wordCount && left > 1; ++i) {
  int n = snprintf(p, left, "%s%s", articles[rand() % MAX_ARTICLES],
                   (i + 1 < wordCount) ? " " : "");
  if (n < 0 || (size_t)n >= left) break;
  p += n; left -= n;
}

Notes: was right to point out you need an integer index; was right that std::string is the simplest route. Reserve capacity on the std::string if you expect long sentences to avoid repeated reallocations. For tiny arrays the rand() % N approach is usually fine, but use std::uniform_int_distribution when you care about statistical quality.

Recommended Answers

All 8 Replies

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.