Random Selection from a Vector

How would it be possible to take a random number of elements from a vector and store them in string?

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
	srand(time(0));
	while(true){
		vector<string>animals;
//		for(int k = 1; k < 6; ++k){
			string word;
//			cout << "Enter: " << k << ".";
			cout << "Enter: ";
			string input;
			getline(cin,input);
			if (input == "end") break;
			animals.push_back(input);
//		}
		string selection;
		for (int i = 0, len = rand()%3+1; i < len; ++i) selection = animals.at(rand()%animals.size());
		for (int k = 0; k < selection.size(); ++k) cout << selection.at(k); cout << endl;
		string display;
		for (int i = 0; i < selection.length(); ++i) display += selection.at(i);
		random_shuffle(display.begin(), display.end());
		for (int k = 0; k < display.length(); ++k) cout << display.at(k); cout << endl;


	}
	return 0;
}
Last edited on
How would it be possible to take a random number of vectors and store them in string?

I'm sorry, your question is not making any sense to me. (And it's different from the title of your thread).

You don't have a random number of vectors in your program. You have a single vector (animals).

Line 23: You're setting selection to a random element of the vector. That's fine. However, you're doing that a random number of times. Why?

Line 24: You're outputting selection one character at a time. Why not just cout selection?

Line 26: You're building display one character at a time? Why not just assign selection to display?

Line 27: You're shuffling the characters in display. I'm assuming you making some kind of guessing game.

Line 28: You're outputting display one character at a time. Again, you can output the whole string at one time.
Last edited on
Sorry, I just noticed, it was a typo, I meant:
How would it be possible to take a random number of elements from a vector and store them in string?

So what I need the code to do is:

1)A vector that stores some string inputs.

2)A vector that randomly selects up to 3 elements from the vector.

3)Transfer those elements into a string.

4)Shuffle the string so that it displays a scrambled version of the game.

This is all part of a code to create a game that asks a user to type in a bunch of inputs of which it takes a random number of inputs and then scrambles them and has the user guess which inputs those are.

I hope this is more understandable.
I am not at all sure this is what you're looking for.
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
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
using namespace std;

int main() 
{   srand(time(0));
	while(true){
		vector<string>  animals;
		//  1)  Store some string inputs
		for(int k = 1; k < 6; ++k)
        {   string word;
			cout << "Enter " << k << ":";
            cin >> word; 
		    if (word == "end") 
		        break;
			animals.push_back (word);
		}
		string result;
		//  2,3)  Select up to 3 animals & transfer to string
		for (int i = 0, len = rand()%3+1; i < len; ++i) 
		    result += animals.at(rand()%animals.size());	
        //  4)  Shuffle the string	    
        random_shuffle (result.begin(), result.end()); 
        //  5)  Output the scrambled string
		cout << result << endl;
	}
	return 0;
}   
Last edited on
Thanks! you fixed up the code,

just the only thing is the input has a minimum of 5 inputs (sorry... i know its poor coding on my part... I just a beginner there is a lot I have to learn :( ), it was kind of why I used getline. But aside from that it seems to be working fine.

To be more precise I am making a guessing game.

This was my original script that I needed help with (i know its a mess a // a few things because those were lines I tried but commented out because they werent working):
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
              srand(time(0));
              while(true){
//Vector taking some number of inputs 
                             vector<string>animals;
                                           string word;
                                           cout << "Enter: ";
                                           string input;
                                           getline(cin,input);
                                           if (input == "end") break;
                                           animals.push_back(input);
                     if (animals.size() < 5) cout << "Enter At Last 5 Inputs: "; continue

                             vector<string>copy(animals);
                             random_shuffle(copy.begin(), copy.end());
//                         vector<string>selection;
//                         for (int i = 0; i < (rand()%3+1); ++i) {selection.push_back(copy[i]);};
//                         for (int k = 0; k < selection.size(); ++k) cout << selection.at(k); cout << endl;
                             string display;
                             for(int i =0; i <= rand()%3+1; ++i) display += copy.at(i);
//                         for (int i = 0; i < selection.size(); ++i) display += selection.at(i);
                             random_shuffle(display.begin(), display.end());
                             for (int k = 0; k < display.length(); ++k) cout << display.at(k); cout << endl;


              }
              return 0;
}


I need the program to:
1) Form a string vector from at least 5 user inputs (Vector 1)
2) Take from 1 to 3 random elements from above vector (Vector 2)
3) Copy Vector 2 to a temporary Vector or string
4) Shuffles this Copy Vector or string and displays
5) User then inputs the value
6) Checks Users Value

Example: Enter up to 5 inputs: alpha bravo charlie delta echo
Computer takes alpha and delta.
Output: What are the inputs in: palhdaetal
Input: alpha delta
Output: Correct!
Last edited on
This is the latest version of my code:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

	srand(time(0));
	int correct = 0;
	int incorrect = 0;

	while(true){
		//  1)  Store some string inputs
		vector<string>animals;

			string word;
			cout << "Enter At Least 5 Animals :";

			getline(cin, word);
		    if (word == "end")
		    	break;
			animals.push_back (word);
			for(int i = 0; i < animals.size(); ++i)cout << animals[i] << endl;


		string result;
		//  2,3)  Select up to 3 animals & transfer to string
		for (int i = 0, len = rand()%3+1; i < len; ++i)
		    result += animals.at(rand()%animals.size());
		cout <<  result << endl;
		string display;
		for (int i = 0, len = result.length(); i < len; ++i)
			display += result.at(i);
        //  4)  Shuffle the string
        random_shuffle (display.begin(), display.end());
        //  5)  Output the scrambled string
		cout << "What are the Animals in: " << display << endl;
		//User Answers the Question
		cout << "Enter Answer: ";
		string answer;
		getline(cin, answer);
		vector<string>ans; ans.push_back(answer);
		string answers;
		for (int i = 0; i < ans.size(); ++i) answers += ans.at(i);
//Checks to see if Users input is right
		if (answers == result) {
			cout << "Correct!" << endl;
			++correct;
			cout << "Correct Count: " << correct << endl;
		}
		else if (answers != result) {
			cout << "Wrong" << endl;
			++incorrect;
			cout << "Incorrect Count: " << incorrect << endl;
		}
	}
	cout << "Bye..." << endl;
	return 0;
}


This is the result that keeps coming.

Enter At Least 5 Animals :frog dog shark hound
frog dog shark hound
frog dog shark houndfrog dog shark hound
What are the Animals in: daoa shhhsodkhdgrou o ndfgor on rgkfgru
Enter Answer: frog dog shark hound
Wrong
Incorrect Count: 1
Enter At Least 5 Animals :end
Bye...
Topic archived. No new replies allowed.