My code:
void Block::word()
{
std::string worda[] = {"cat","owl","cow"};
std::string wordb[] = {"apple","power","block"};
std::string wordc[] = {"account","extreme","kingdom"};
std::string wordd[] = {"adjective","undefined","pineapple"};
srand(time(0));
fnwords[0] = worda[rand() % 3 + 1];
fnwords[1] = wordb[rand() % 3 + 1];
fnwords[2] = wordc[rand() % 3 + 1];
fnwords[3] = wordd[rand() % 3 + 1];
for (int d=0; d<4; d++){
std::cout << fnwords[d] << std::endl;
}
}
int main()
{
Block obj;
obj.word();
return 0;
}
Here the first index of array fnwords must contain a random word from array worda, second index from wordb, and so on.
But it gives error sometimes that program.exe has stopped working.
And it initializes array like this:
-cow
-cat
-kingdom
-undefined
rand() % 3gives 0, 1, or 2. Don't+ 1Blockandfnwordsdefined? Where are the includes for<string>,<iostream>and<cstdlib>? Please edit your code to make it a minimal reproducible example of your problem.