Hi I have wrote some code but it never seems to reach the for loop and cannot understand why. I want it to check whether or not the word is in the array and if it is it will break out and leave the method else it will add to string array and output the word. Here is my code

http://pastebin.com/i8KYAYXa

Thanks in advance!

For future reference, please post your code here on the forum rather than on a 3rd party site. And please don't forget to use code tags!

For everybody elses benefit, here's the OP's code:

#include <iostream>
#include <string>
#include <time.h>

using namespace std;
const int MAX_SIZE = 100;
string words[MAX_SIZE];
int counter = 0;

void print_permutations(string s)
{
	bool exists = true;
	int i=0;
	int j=0;
	srand(time(NULL));
  
	for(int k = 0; k < s.length()/2; ++k)
	{
		i = rand()%s.length();//-1;
		j = rand()%s.length();//-1
		/*if (i < 0)
			i = 0;
		if (j < 0) 
			j = 0;
		if (i == j) 
			 i++;
		if (i > s.length()-1) 
			i = 0;*/
		if(i != j){
			s[i] = s[i] ^ s[j];
			s[j] = s[i] ^ s[j];
			s[i] = s[i] ^ s[j];
		}
	}

	if(exists == true)
	{
		for(int m = 0; m < MAX_SIZE; m++)
		{
			if(words[m] == s)
			{
				cout << "Permutation exists" << endl;
			}
			break;
		}
	}else
		{
			words[counter] = s;
			cout << "permutation: " << s << endl;;
		}
}


int main()
{
	string word;
	do{
		cout << "\nPlease enter a word" << endl << "Original Word: ";
		cin >> word;
		print_permutations(word);
		counter++;
	}while(word != "1");
}

Now from what I can see, despite the fact it probably compiles (although, if memory serves you might need to include cstlib for srand and rand) your code is full of logical problems and doesn't look like it will find all permutations of any given string.

I won't go into all of the logical problems your code has, but I'll explain the problem you've mentioned about the for loop not being entered:
If you take a good look at your code (you might want to try setting some breakpoints and stepping through your program) you'll see that after entering a word, the permutations function is called.

Inside the permutations function, the first for loop is iterated through.
Then because the boolean variable 'exists' is always true, the block of code from lines 38-44 is always executed. So the 2nd for loop is entered.

But you aren't getting any output from this block because your array 'words' is empty/uninitialised.
So at line 40:

if(words[m] == s)

The if statement always fails because the array 'words' is empty, so there's nothing to compare the string 's' against. Which is why you are getting no output from your program.

Plus the 2nd for loop exits on the 1st iteration anyway because of the break statement at line 44. So even if you did have some words in there, it would only compare s with the first one before breaking out of the loop.

Incidentally, the code at lines 48 and 49, which adds the string to the word array is currently unreachable.

So it turns out that both for loops in your program ARE being entered... You just need to rethink what you are doing inside them!

As for the rest of the problems in your code, I'll leave you to work them out as you continue working on your program.
But if you do have any further problems, please repost (with any relevant, problematic code, or error messages) and I'm sure somebody here will help you!

ok I have only just tweaked this code a bit but now it displays properly however what I want is that if the word is in the array you would find another permutation that is not in the loop. If that makes sense. Here is my new code. Basically I wanna have random permutations of a word...if the word is called how ever many times each time should be different. To be fair I don't think this is the best way to approach this but it was the only way I could think of.

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

const int MAX_SIZE = 100;
string words[MAX_SIZE];
int counter = 0;

void print_permutations(string s)
{
	int i=0;
	int j=0;
	bool does_exist = true;
	srand(time(NULL));
	do{
		for(int k = 0; k < s.length(); ++k){
		i = rand()%s.length(); //produces a random number between 0 and the length of the string.
		j = rand()%s.length();
			if(i != j){
				s[i] = s[i] ^ s[j]; //takes random characters from the string and XOR's them
				s[j] = s[i] ^ s[j]; //in order to swap characters in place without allocating memory.
				s[i] = s[i] ^ s[j];
			}
		}

		for(int m = 0; m < MAX_SIZE; m++){
			if(words[m] == s)
				does_exist = true;
			else does_exist = false;
			words[counter] = s;
		}
	 }while(does_exist);

	if(!does_exist){
		
		cout << "Permutation: " << s << endl;
	}
}


int main()
{
	string word;
	do{
		cout << "\nPlease enter a word" << endl << "Original Word: ";
		cin >> word;
		print_permutations(word);
		counter++;
	  }while(word != "1");
}

ok so now it seems to work but the comparison doesnt...I really can't see what the problem is...anyone got any ideas? What happens now is this line keeps printing and there are still some permutations that are the same

cout << "Permutation: " << s << endl;
#include <iostream>
#include <string>
#include <time.h>

using namespace std;

const int MAX_SIZE = 100;
string words[MAX_SIZE];
int counter = 0;

void print_permutations(string s)
{
	int i=0;
	int j=0;
	bool does_exist = true;
	srand(time(NULL));
	
	for(int m = 0; m < MAX_SIZE; m++){
		if(words[m].find(s))
		{
			does_exist = true;
		} else does_exist = false;

	if(does_exist)
	{
		for(int k = 0; k < s.length(); ++k){
		i = rand()%s.length(); //produces a random number between 0 and the length of the string.
		j = rand()%s.length();
			if(i != j){ //if both i and j are the same number and you xor them you lose chars.
				s[i] = s[i] ^ s[j]; //takes random characters from the string and XOR's them
				s[j] = s[i] ^ s[j]; //in order to swap characters in place without allocating memory.
				s[i] = s[i] ^ s[j];
			}
		}
	}else does_exist = false;
			words[counter] = s;
			cout << "Permutation: " << s << endl;
			break;
	}
	
}

int main()
{
	string word;
	do{
		cout << "\nPlease enter a word" << endl << "Original Word: ";
		cin >> word;
		print_permutations(word);
		counter++;
	  }while(word != "1");
	return 0;
}

ok nevermind...my mistake I had the first for loop wrap around everything and didn't see it but the only problem now lies wiith the array comparison.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.