Hi guys, I was wondering if this was a good place to use a goto statement? I know this will get stuck in an infinite loop eventually and I am working to fix that right now. However I wanted everyones views on whether or not this is acceptable, the goto statement I meant. If not, how else would you have done it? Also is there anyway to make this code more efficient?
#include <iostream>
#include <string>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> words;
void print_permutations(string s)
{
short x=0;
short y=0;
vector<string>::iterator result;
srand(time(NULL));
no_word_found: for(short i = 0; i < s.length(); ++i){
x = rand()%s.length(); //produces a random number between 0 and the length of the string.
y = rand()%s.length();
if(x != y){ //if both i and j are the same number and you xor them you lose chars.
s[x] = s[x] ^ s[y]; //takes random characters from the string and XOR's them
s[y] = s[y] ^ s[x]; //in order to swap characters in place without allocating memory.
s[x] = s[x] ^ s[y];
}
}
result = find(words.begin(), words.end(), s);
if(result == words.end()){//if string not in vector array execute code
words.push_back(s);
cout << "Permutation: " << s << endl;
}
else if(result != words.end()) //if the permutation is in the array it will display this message
goto no_word_found;
}
int main()
{
string word;
do{
cout << "\nPlease enter a word" << endl << "Original Word: ";
cin >> word;
print_permutations(word);
}while(word != "1");
return 0;
}
Thank you