Help with vectors, please

So the program that I've recently been working on is a random password generator in which the user can specify the rules the password must follow.
I've got the main part of the program working (the password gen part), but now I want to make it better.
I want to set it to where no 2 characters can appear twice in a row.
I've been messing with this for about 2 hours now, and I am just stumped.
Here's just part of the program:
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
for (x=0; x<characters; x++) //allows for a specific number of chars
{
there:
srand(time(0));
multiple = rand () % 12 + 1;  //used to have evenly random selection of char and special chars
switch (multiple) {
          case 4:        //1 in 12 chance for the char to be special (thought this was fair)
          ascii = rand () % 6 + 33;  //gen random number
          letter = ascii;            //convert to char
          repeat.push_back (letter); //used to find repeats?
          if (repeat.end() == password.end() && password.size() > 1) //detect repeat only if there are more than 1 chars
              {
                  goto there;
                     }
password.push_back (letter); //if everythings alright, add it to final password
        break;
default:        //this is responsible for generating capitals, lowercase, and #
    ascii = rand () % 74 + 48;  //everything else is the same from here
    letter = ascii;
    repeat.push_back (letter);
    if (repeat.end() == password.end())
    {
    goto there;
     }
     password.push_back (letter);
    break;
    }
    }


What this ends up doing is outputting all of the same characters, for example:
Your password is: :::::::::::::::

or
Your password is: AAAAAAAAAAAAAAA 


I've only been programming for under a year, so take it easy on me...
Help would greatly be appreciated :)
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
}

Last edited on
rollie,
Thank you for your help :) I finally figured it out, and I made it a little more efficient than what I originally had.
Here it is if you wanted to see it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (password.size() < characters) //guarantees the correct # of characters
                 {
                     if(rand () % 12 == 4) {
                              letter= (char) (rand () % 6 + 33);
                              }
                      else {
                               letter= (char) (rand () % 74 + 48);
                                 }
                      if (letter != last) {
                      password.push_back (letter);
                      }
                     else {
                                letter=last;   //idk why I had to put this here, but it made it work.
                             }
                        letter = last;
                            }
Last edited on
Topic archived. No new replies allowed.