How to output a string in random order

Jun 4, 2013 at 7:39am
Ok so im making a game where you enter a word and it mixes up the word and the other person has to guess what it is but im unsure of how to actually mix up the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string word = " ";

    cout << "CRYPTO GUESS\n" << endl;

    cout << "Please enter your word" << endl;
    cin >> word;
}
Jun 4, 2013 at 7:49am
The easiest way is to use std::random_shuffle().

http://www.cplusplus.com/reference/algorithm/random_shuffle/

1
2
3
4
5
#include <algorithm>

// ...

std::random_shuffle(word.begin(), word.end());

Jun 4, 2013 at 8:03am
ok i got it working but i noticed when entering a word twice it puts it in the same order. I entered Traps 3 times and it comes out as srpat every time, why is that and how can i fix it?
Jun 4, 2013 at 8:12am
I entered Traps 3 times and it comes out as srpat every time, why is that and how can i fix it?

Interesting. So if you run your program multiple times, it still reorders "traps" the same way? I don't think it's supposed to do this.

Anyway, the page I linked to contains an example using a custom random function. The following page shows how to do the same thing using the C++11 random library, instead of the old cstdlib:

http://en.cppreference.com/w/cpp/algorithm/random_shuffle
Jun 4, 2013 at 8:22am
Yes this is what i have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string word = " ";
    string guess = " ";
    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    cout << "Please enter your word" << endl;
    cin >> word;

    random_shuffle(word.begin(), word.end());

    cout << word << endl;
}
Jun 4, 2013 at 11:39am
random_shuffle(word.begin(), word.end());

std::rand is often used to implement random_shuffle. Use std::srand to seed the random number generator prior to using random_shuffle this way.
Jun 4, 2013 at 11:47am
how would that look?
Jun 4, 2013 at 11:54am
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
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

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

    string word = " ";
    string guess = " ";
    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    cout << "Please enter your word" << endl;
    cin >> word;

    random_shuffle(word.begin(), word.end());

    cout << word << endl;
}
Jun 4, 2013 at 12:00pm
Ah i see, i thought there was more to it than that. Ok well it works now the words are mixed up every time i run it, but now i have another problem that should be simple, i have been trying to figure it out for the last 45 minutes, i cant get the program to brak the second while loop when the right word is entered.

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

using namespace std;

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

    string word = " ";
    string guess = " ";
    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    while(game_loop != false)
    {
        cout << "Please enter your word" << endl;
        cin >> word;

        random_shuffle(word.begin(), word.end());

        for(int i = 0; i < 20; i++)
        {
            cout << "\n";
        }

        cout << word << endl;

        cout << "\n";

        while(guess != word)
        {
            cout << "Your guess: ";
            cin >> guess;

            if(guess == word)
            {
                cout << "Correct!" << endl;
                continue;
            }
        }
    }
}
Jun 4, 2013 at 12:27pm
Second? If you mean inner loop, replace line 43 with a break;.

You don't have anything to break the outer loop yet. Perhaps a check on state of cin, and/or for empty words?
Jun 4, 2013 at 7:54pm
Well the problem is it doesnt even go into the if statement to show the "Correct!" dialog it just keeps asking to input my guess. How do i check the state of cin?
Jun 4, 2013 at 7:57pm
Ch1156 wrote:
Well the problem is it doesnt even go into the if statement to show the "Correct!" dialog it just keeps asking to input my guess.
Maybe you're guessing wrong?
Jun 4, 2013 at 8:27pm
no, my word is trap, and i enter trap and it just keeps asking me.

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

using namespace std;

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

    string word = " ";
    string guess = " ";
    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    while(game_loop != false)
    {
        cout << "Please enter your word" << endl;
        cin >> word;

        random_shuffle(word.begin(), word.end());

        for(int i = 0; i < 25 ; i++)
        {
            cout << "\n";
        }

        cout << word << endl;

        cout << "\n";

        while(guess != word)
        {
            cout << "Your guess: ";
            cin >> guess;

            if(guess == word)
            {
                cout << "Correct!" << endl;
                break;
            }
        }
    }
}
Jun 4, 2013 at 9:03pm
you need 2 variables for word, i.e original_word and shuffled_word

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "Please enter your word: ";
cin >> original_word;

shuffled_word = original_word;
random_shuffle(shuffled_word.begin(), shuffled_word.end());

for(int i = 0; i < 25 ; i++)
    cout << "\n";
cout << shuffled_word << "\n\n";

while(guess != original_word)
{
    cout << "Your guess: ";
    cin >> guess;
    if(guess != original_word)
        cout << "Wrong!\n";
}

cout << "Correct!" << "\n";
Jun 4, 2013 at 9:41pm
Ok i changed it and it seems to stop working when it gets to the inner while loop.

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

using namespace std;

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

    string original_word = " ";
    string shuffled_word = " ";
    string guess = " ";
    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    while(game_loop != false)
    {
        cout << "Please enter your word" << endl;
        cin >> original_word;

        shuffled_word = original_word;
        random_shuffle(shuffled_word.begin(), shuffled_word.end());

        for(int i = 0; i < 25 ; i++)
        {
            cout << "\n";
        }

        cout << shuffled_word << endl;

        cout << "\n";

        while(guess != original_word);
        {
            cout << "Your guess: ";
            cin >> guess;

            if(guess == original_word)
            {
                cout << "Correct!" << endl;
                break;
            }
        }
    }
}
Jun 4, 2013 at 9:47pm
-_____- never mind it was my fault, i tried to do a do while loop and reversed it but forgot to take the ; off of the end of my while statement. thanks. now im curious, why does it work when i store the random shuffle in the random shuffle variable? is it because the random shuffle is stored in the original_word variable? so therefore when i enter the correct word it tries to compare it to the shuffled one?
Last edited on Jun 4, 2013 at 9:47pm
Jun 4, 2013 at 9:48pm
so now you have to figure how to exit the game_loop or wut @.@

just add
1
2
3
4
char response;
cout << "Another game?(y/n) ";
cin >> response;
game_loop = response == 'y';

after line 47 @.@
Topic archived. No new replies allowed.