Mistery word : can't display a string

Hi,
first of all, excuse my english, i am french.
So, i am trying to make a "game". First user writes a number, the program mixes it (ex : dog -> ogd), and a second user has to find the word. But when i try to display the mixed word after passing it into my function, it does not work.
Here's my code :

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

string word = "", mixed = "";

string mixing (string mot, int longueur){
string result = "";
srand(time(0));
for (int i=0; i < longueur; i++){
int position = rand() % mot.size();
result[i] = mot[position];
mot.erase(position, 1);
}
return result;
}

int main()
{
cout << "Enter a word : " ;
cin >> word;
int lenght = word.size();

mixed = mixing(word, lenght);
cout << mixed;

bool equal = false; int counter = 0;
while ( counter < 10 && equal == false) {
cout << "What's the mistery word : ";
string user_idea = "";
cin >> user_idea;
counter++;
if (user_idea == word){
equal = true;
cout << "You found : " << user_idea << ", " << counter << " tentatives.";
}
else {cout << "No, Try again"<< endl;}
}

return 0;
}

----------------------

this line : cout << mixed; doesn't work...
Any idea why? hope you understood, thanks in advance....
try 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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

string word, mixed;

string mixing (string mot, int longueur){
	string result;
	srand((int)time(0));
	for (int i=0; i < longueur-1; i++)
	{
		int position = rand() % (mot.size()-1);
		result += mot[position];
		mot.erase(mot.begin()+position);
	}
	result += mot[0];
	return result;
}

int main()
{
	cout << "Enter a word : " ;
	cin >> word;
	int lenght = word.size();

	mixed = mixing(word, lenght);
	cout << mixed << endl;

	bool equal = false; int counter = 0;
	while ( counter < 10 && equal == false) 
	{
		cout << "What's the mistery word : ";
		string user_idea = "";
		cin >> user_idea;
		counter++;
		if(user_idea == word)
		{
			equal = true;
			cout << "You found : " << user_idea << ", " << counter << " tentatives.";
		}
		else 
			{cout << "No, Try again"<< endl;}
	}
}
Last edited on
Thanks a lot ! It does work.. But can you explain me why, mine didn't work and your does?
And do you know where i can find an easy tutorial about the pointers?
Thanks again
I would write function mixing the following way (without the second parameter)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string mixing( std::string source )
{
	std::string result;
	result.reserve( source.length() );

	std::srand( static_cast<unsigned int>( std::time( 0 ) ) );		
		
	while ( !source.empty() )
	{
		std::string::size_type n = std::rand() % source.length();
		result += source[n];
		source.erase( n, 1 );
	}

	return ( result );
}
Last edited on
your mixing function was the problem, you were using/calculating wrong idexes
Also you can use standard algorithm std::random_shuffle. For example

1
2
3
std::string s = "word";

std::random_shuffle( s.begin(), s.end() );
Last edited on
Your mistake is that you are trying to assign the result string which was not allocated memory for. You can not use subscript operator for an empty string

string mixing (string mot, int longueur){
string result = "";// empty string
srand(time(0));
for (int i=0; i < longueur; i++){
int position = rand() % mot.size();
result[i] = mot[position]; // memory overwritting
mot.erase(position, 1);
Topic archived. No new replies allowed.