What should I use?

If I wanted to have a user enter his name, enter forty-two more names after, and then have the names print in a random order on the screen, how would I do this?
A vector to store all the names (as std::strings). Loop 42 times to get user input to add to the vector. Then rearrange the vector randomly (you could do this but picking 2 random numbers between 0-41 and swapping the strings at those positions and doing that a whole bunch. I also believe there is a shuffle() function in <algorithm>...). Then just loop through the vector and print the strings.
So this should work....hopefully.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void enternames()
{

int namecounter = 0;
apstring names;

apvector <apstring> names(43);

cout << "Enter your name, followed by the names of the other 42 people: "

do
{
namecounter++;
getline (cin, names);
'\n';
}

while (namecounter < 43);

std::random_shuffle( v.begin(), v.end() ); 
cout << "The race results are: ";
std::copy( v.begin(), v.end(), output );
cout << endl;
}


I don't believe that prints each string on a new line, so does anyone know how to do that?
did you compile the code? Based on your paste, the code has many errors.

Then, whey using "std::copy"? If you just want to print the name out after shuffling, loop the vector to print them out.

Where did you declare "output"? Do you want to assign another vector with "names"?
Last edited on
Topic archived. No new replies allowed.