Random Word Generator from Multiple Lists

Hi. I am VERY new at programming and I want to make a "Guess Who" game. I want to be able to randomly select a characteristic from each list. I'm pretty sure my code is completely wrong and not complete but this is what I have so far. I know I can generate random from one array, but how would I accomplish this in multiple?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <string>
using namespace std;



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

    string ShirtList[0] = { "blue", "red", "green", "yellow" };
    string FaceList[1] = { "glasses", "no glasses"};
    string EyeList[2] = { "blue", "brown", "green"};
    string HairList[3] = { "red", "black", "brown", "blonde"};

    string words = wordList[rand() % 4];

    cout << words << endl;

    return 0;
}

const int NUM_SHIRT = 4;
vector<string> shirtColor(NUM_SHIRT);
Hello nlizzy,

Lines 25 and 26 will do no good down there. They should be inside "main" before the call to "srand". The "const" is OK and could be used.

Lines 13 - 16 will most likely generate compile errors as written. Line 13 you are trying to create an array of strings with a size of zero. this is not allowed. It must be a size greater than zero. In this case it should be 4 because there are 4 elements that you are initializing the array with. What you are trying to do here is take an array of strings with a size of zero and put 4 strings into it.

Line 18 starts out OK, but then you say to take an element of "wordList" using the "rand" as the subscript, which is OK. But where have you defined "wordList" and what should it contain?

Based on what you started with I rearranged your program to something that might work for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cstdlib>
#include<ctime>
#include <string>
//#include <vector>  // <--- Needed if you are going to use a vector.

//using namespace std;  // <--- Best not to use.

int main()
{
	//const int NUM_SHIRT = 4;
	//vector<string> shirtColor(NUM_SHIRT);

	std::string guess;
	std::string shirtList[4]{ "blue", "red", "green", "yellow" };  // <--- Changed.
	std::string faceList[2]{ "glasses", "no glasses" };  // <--- Changed.
	std::string eyeList[3]{ "blue", "brown", "green" };  // <--- Changed.
	std::string hairList[4]{ "red", "black", "brown", "blonde" };  // <--- Changed.

	srand(time(0));

	guess += shirtList[rand() % 4] + ", ";
	guess += faceList[rand() % 2] + ", ";
        //  <--- Other arrays here.

	std::cout << guess << std::endl;

	return 0;
}

In lines 15 - 18 from C++11 on the "=" is not needed just the {}s. The same is true for numeric variables and "chars". An empty set of {}s will initialize a variable to zero in the form that is needed. Also it is better to start the variable name with a lower case letter and save the capital letters for "classes" and "structs".

You could also write line 15 as: std::string shirtList[NUM_SHIRT]{ "blue", "red", "green", "yellow" }; although I would change the name of the variable.

See if this does something of what you are looking for.

Hope that helps,

Andy
closed account (367kGNh0)
Handy Andy got it right, my take would be more similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include <ctime>
#include <cstdlib>
#define JH '\n'


using namespace std;

int main () {
	
	srand(time(0));
	int RPS = 1+rand()%2;
	int RSS = 1+rand()%2;
	int RAS = 1+rand()%2;
	int UCHOI;
	
	cout<<"Welcome to Rock, Paper Scissors!"<<JH<<"What move do you want to go for?"<<JH<<"(Pick the number)"<<JH<<"1.Rock"<<JH<<"2.Paper"<<JH<<"3.Scissors"<<JH;
	cin>> UCHOI;
	
	if (UCHOI == 1)
	{cout<<"You picked Rock"<<JH;
	
	if (RPS == 1)
	{cout<<"I win! I chose paper and paper defeats rock!";}
	
	else if(RPS == 2)
	{cout<<"Oup! Looks like I chose Scissors so you win! Rock beats scissors!";}}
	
	if (UCHOI == 2)
	{cout<<"You picked Paper"<<JH;
	
	if (RSS == 1)
	{cout<<"I win! I chose scissors and scissors defeats paper!";}
	
	else if(RSS == 2)
	{cout<<"Oup! Looks like I chose rock so you win! Paper annihilates rock!";}}
	
	
	
	if (UCHOI == 3)
	{cout<<"You picked Scissors"<<JH;
	
	if (RAS == 1)
	{cout<<"I win! I chose rock and rock smashes scissors!";}
	
	else if(RAS == 2)
	{cout<<"Oup! Looks like I chose paper so you win! Scissors mutilate paper!";}}
	
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iterator>

// return a random element from the array of some type ARRAY_TYPE
// std::size gives us the number of elements in the array (note: C++17)
//           https://en.cppreference.com/w/cpp/iterator/size
// auto : let the compiler deduce the type of the element in the array
template < typename ARRAY_TYPE > const auto& random_pick( const ARRAY_TYPE& arr )
{ return arr[ std::rand() % std::size(arr) ] ; }

int main()
{
    std::srand( std::time(nullptr) ) ;

    // we do not need to modify these, so make them const
    // let the compiler figure out what the sizes of the arrays are
    const std::string shirtList[] = { "blue", "red", "green", "yellow" };
	const std::string faceList[] = { "glasses", "no glasses" };
	const std::string eyeList[] = { "blue", "brown", "green" };
	const std::string hairList[] = { "red", "black", "brown", "blonde" };

    for( int i = 0 ; i < 8 ; ++i ) // test it
    {
        const std::string selection = random_pick(shirtList) + " shirt, " +
                                      random_pick(faceList) + ", " +
                                      random_pick(eyeList) + " eyes and " +
                                      random_pick(hairList) + " hair" ;

        std::cout << i+1 << ". " << selection << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/cfb67fe7e4e05815
Last edited on
Topic archived. No new replies allowed.