0

i have a string array that contains 20 words. I made a function that take 1 random word from the array. But i want to know how can i return that word from array. Right now i am using void function, i had used char type but it wont work. Little help here ? Need to make word guessing game.

CODE:

#include <iostream>
#include <time.h>
#include <cstdlib>
#include <stdlib.h>
#include <algorithm>///lai izmantotu random shuffle funckiju
#include <string>
using namespace std;


void random(string names[]);


int main() {
     char a;
     string names[] = {"vergs", "rokas", "metrs", "zebra", "uguns", "tiesa", "bumba",
                       "kakls", "kalns", "skola", "siers", "svari", "lelle", "cimdi",
                       "saule", "parks", "svece", "diegs", "migla", "virve"};

random(names);

        cout<<"VARDU MINESANAS SPELE"<<endl;
        cin>>a;







return 0;
}

void random(string names[]){
    int randNum;
for (int i = 0; i < 20; i++) { /// makes this program iterate 20 times; giving you 20 random names.
srand( time(NULL) ); /// seed for the random number generator.
randNum = rand() % 20 + 1; /// gets a random number between 1, and 20.
names[i] = names[randNum];
}
//for (int i = 0; i < 1; i++) {
//cout << names[i] << endl; /// outputs one name.
//}

}
arrowd
  • 33,231
  • 8
  • 79
  • 110
Maaverick
  • 63
  • 1
  • 8
  • 1
    I don't see how the code you posted relates to what you ask (which isn't all that clear, TBH). – StoryTeller - Unslander Monica May 01 '16 at 14:48
  • 2
    Either use a `vector` or tell the function how many elements are in the array. Don't hard-code 20 into the function. That's tangential to your question, but relevant general advice. Make sure that functions know how big an array they're dealing with. – Jonathan Leffler May 01 '16 at 14:48
  • 1
    What about returning a string? `std::string random(std::string names[]);` – Galik May 01 '16 at 14:49
  • 1
    See [`srand()` — Why call it only once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) for details on why your use of `srand()` is thoroughly misguided. – Jonathan Leffler May 01 '16 at 14:49
  • Also never seed a random number generator in a loop, seed it *once* at the start of the program. – Galik May 01 '16 at 14:50

4 Answers4

2

Make random return string. You also only need to seed the number generator once. Since you only want to get 1 random word from the array, you don't need a for loop.

string random(string names[]){
    int randNum = 0;
    randNum = rand() % 20 + 1;
    return names[randNum];
}

Then, in the main function, assign a string variable to the return value of the random function.

int main() {
    srand( time(NULL) ); // seed number generator once
    char a;
    string names[] = {"vergs", "rokas", "metrs", "zebra", "uguns", "tiesa", "bumba",
                       "kakls", "kalns", "skola", "siers", "svari", "lelle", "cimdi",
                       "saule", "parks", "svece", "diegs", "migla", "virve"};

    string randomWord = random(names);

    cout<<"VARDU MINESANAS SPELE"<<endl;
    cin>>a;

    return 0;
}
Greg M
  • 954
  • 1
  • 8
  • 20
2

In your question as well as in the previous answer, you are running out of bounds accessing the names array:

int randNum = rand() % 20 + 1;
return names[randNum];

You are never accessing names[0] but instead reach behind the array when addressing names[20].

Florian
  • 255
  • 1
  • 9
1

Additionally srand(time(NULL)) should be called only one time, on the beginning of main() function.

Lucas535
  • 26
  • 3
0

I'm not super familiar with strings, but you should be able to just declare random() as a string function.

Ex: string random (string names[]);

Eric
  • 79
  • 1
  • 3