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;

	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
			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;
		cout << "\nPlease enter a word" << endl << "Original Word: ";
		cin >> word;
	  }while(word != "1");
	return 0;

Thank you

No, because it is not necessary.
You can fix that with a while statement.

Because goto can have devastating results on your program and program flow its frowned on. If you really need to jump all over your program try a safe function like longjmp().

void longjmp (jmp_buf env, int val);
int setjmp ( jmp_buf env );

If you really need to jump all over your program try a safe function like longjmp().

void longjmp (jmp_buf env, int val);
int setjmp ( jmp_buf env );

Wrong. longjmp() and setjmp() are not supported in c++ program. I have used goto on very very rare occasion when within a deeply nested loops of if statements and other kinds of loops are not possible. In my 30 years programming I've used it maybe 2 or 3 times.

No, don't use goto for such a simple purpose. If nothing more, it's better to replace it with while(true) { }; loop (and a break; statement somewhere in the loop). At the least, this makes it clear that the code is a loop, and respect indentation rules, that alone is already a good argument to motivate using a traditional loop statement (for, while, do-while) instead of a label/goto pair.

Your code does a generation of permutations of letters of a word, may I suggest you take a look at std::next_permutation, and either get inspired from it or use it directly (hints: the number of permutation is fixed (implying a fixed-length loop); and you shouldn't need a random-number generator anywhere, the sequence of all permutations is something that is easily obtained systematically and is deterministic).

Be a part of the DaniWeb community

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