Pieces of advice
1) It is a monumentally rare day when a goto statement is the right way to go to control program flow. In general, dont use them!
2) Do you need a container (vector, list, whatever 'repeat' is) to check for repeat? Wouldn't only saving the last character suffice? So instead of comparing the last char in repeat with the last char in 'password', just compare 'letter' to the previous 'cLastChar' variable I recommend you add.
3) srand(...) should only be called once in an application. Put it before the for loop
4) std::vector<T>::end() does not return the last item in the container, it returns an iterator to 1 past the last element, and is most often used to iterate through a container
ex:
for (vector<char>::iterator itr = password.begin(); itr != password.end(); itr++)
5) by convention, you iterate on the variable 'i', 'j', 'k, etc...'x' seems out of place. could just be my personal style though...
6) in your switch statement, you only have 2 conditions - if/else makes more sense in this case in my opinion.
7) Also, it's generally advisable to factor out common code. In both branches of your switch, you check repeat, you push_back to password, you push_back to repeat. why not just have all this logic AFTER your switch statement?
I'd recommend something like (untested):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
srand(time(0));
char cLastChar = '\0';
for (i=0; i<characters; i++) //allows for a specific number of chars
{
char cPassChar;
do
{
if(rand() % 12 == 4)
{
cPassChar = (char)(rand () % 6 + 33);
}
else
{
cPassChar = (char)(rand () % 74 + 48);
}
} while (cPassChar == cLastChar );
password.push_back (cPassChar);
cLastChar = cPassChar
}
|