Herodegon
Herodegon

Reputation: 41

Breaking out of an inner loop doesn't terminate the loops

The code is a four-character password generator. I have an if/else break system setup, but I can't figure out why it isn't working. The loop generates past the limit until it exhausts all options, which I'm having a hard time properly debugging. I'm very new to anything other than javascript and HTML, so any help would be useful. Thanks!

#include <iostream>
using namespace std;

int
main ()  {
  char char1;
  char char2;
  char char3;
  char char4;
  int numPasswordsGenerated = 0;

  cout << "Password Generator:" << endl;

  /* Generates four-character passwords (excludin digits) by exhausting all character
     options between '!' and '~' starting with the fourth character. Once the fourth 
     character exhausts all options, the third character increases by the next character 
     in the binary lineup, then continuing with the fourth character. This process continues 
     until the numPasswordsGenerated integer reaches the desired number, or the password 
     output becomes "~~~~"; whichever comes first.
   */
  char1 = '!';
  while (char1 <= '~')    {
      char2 = '!';
      while (char2 <= '~')    {
          char3 = '!';
          while (char3 <= '~')    {
              char4 = '!';
              while (char4 <= '~') {
                  // cout << char1 << char2 << char3 << char4 << endl;
                  numPasswordsGenerated++;                  //!*FIXME*!
                  cout << numPasswordsGenerated << endl;    //DEBUG DELETE
                  if (numPasswordsGenerated == 10)
                    break;
                    
                  char4++;
                  
              }
          char3++;
              
          }
      char2++;
          
      }
      char1++;
      
  }

  return 0;
}

Upvotes: 3

Views: 77

Answers (1)

tadman
tadman

Reputation: 211560

If you want to stop early, put this in a function and instead of break use return.

Remember that break only kicks out of the loop you're in, not all loops in the scope of the function.

Here's a reworked version with a more flexible input arrangement for the limit using argv:

#include <iostream>

void generatePasswords(const size_t limit) {
  char password[5];
  size_t count = 0;

  password[4] = 0;

  password[0] = '!';

  while (password[0] <= '~') {
    password[1] = '!';

    while (password[1] <= '~') {
      password[2] = '!';

      while (password[2] <= '~') {
        password[3] = '!';

        while (password[3] <= '~') {
          password[3]++;

          std::cout << password << std::endl;

          if (++count >= limit) {
            return;
          }
        }

        password[2]++;
      }

      password[1]++;
    }

    password[0]++;
  }
}

int main(int argc, char** argv) {
  std::cout << "Password Generator:" << std::endl;

  generatePasswords(argc >= 2 ? atoi(argv[1]) : 1000);

  return 0;
}

Note the use of char password[5] instead of a bunch of unrelated characters.

Upvotes: 1

Related Questions