dimanche 4 septembre 2016

C++ Randomly select from string array

I'm trying to devise a function that randomly selects three of the five strings and displays them on the screen. Here's what I have so far. It runs but nothing prints to the screen.

string BoxOfProduce::randomize()
{
    srand(time(0));
    string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};

    for(int i = 0; i < 3; i++)
    {
        string random = choices[rand() % 5];
    }
    return random;
}

int main()
{
    srand(time(0));

    BoxOfProduce bundle1;
    bundle1.randomize();
    cout << bundle1.randomize() << endl;

}




Aucun commentaire:

Enregistrer un commentaire