Thread: For loop

  1. #1
    Unregistered
    Guest

    For loop

    I'm trying to write a program that lets you find out a given password by testing each possible combination of numbers/letters. I want to use a for loop. I want to set it out like this:

    ///////////////////////////////////////////////////////////////////////////////////

    for ( /* what to place here*/)
    {
    // compare current combination with the actual one
    if ( /* whatever */)
    {
    // do something
    }

    // move onto next combination by incrementing etc...(?)

    }

    ///////////////////////////////////////////////////////////////////////////////////

    As you can see, i'm not very far ahead. Can anybody please give me any ideas?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Hmmm, something like this?:

    Code:
    bool Error=false;
    for(int i=0; i<strlen(CorrectString); i++)
    {
      if(CorrectString[i]!=InputString[i]) Error=true;
    }
    You could also use strcmp() to compare two strings.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Unregistered
    Guest
    if the password is a single combination of letters and numbers then it has to be stored as a string. The problem is the word string means different things to different people. Some people think of a null terminated char array, or c_style string, when they think of string. Others think of an instance of the string class in STL when they think of string. Others think of an instance of the AnsiString or CString or TString or String or APString class or even an instance of your own homegrown string class. If you are going to compare two c_style strings then you need to use one of the strcmp() methods found in the string.h or cstring header files (which file to use depends on your compiler). Most third party string class include either an overloaded equality operator == or a member function (often called c_str()) that allows you to extract the c_style string upon which the instance of the string class is based. Likewise many of the string classes have an overloaded [] operator to allow you to access the elements of the string one at a time like you would an ordinary array.

    You don't know how long the password string will be so take an arbitrary, relatively short length to start out you system, say 3. You can always extend it later once you have the algorhythm perfected. declare a string to hold the "real" password to check. determine a string to hold the "hacked" password. Since the actual password can be 1, 2, or 3 elements long assign the null char to each element of the hack. Then assign each letter and number one at a time to the first element of the hack and compare each in turn with real. Then assign a given letter, probably 'a' or 'A' to the first element of hack and assign, in turn each of letters to the second element of hack and check each two element hack starting with a against real. Then check each two element hack starting with 'b' against real. etc. Then do the same for each 3 element hack starting with the sequence aaa, aab, aac, etc. Once you see how much time it takes to do a 3 element password. Extend it to a 5 or 6 element password, and further if your heart so desires.

    Probably easiest to use the ASCII values of the letters and numbers to make the increments easier.

  4. #4
    Unregistered
    Guest
    got any code example for that? It's really confusing...

Popular pages Recent additions subscribe to a feed