Thread: problem with for loop

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    problem with for loop

    Hi all, I have some code that I am trying to compile and every time I try I keep getting an error on the for loop.

    I compiled this same code before without any problem with Dev C++ Compiler running both Win 98SE and XP. I originally got this code online as I wanted a random pasword generator. When I 1st got it I even made changes to suit my needs and it still compiled. I say that because I'm still a newbie to C. I will post the code below to see. Thanks in advance,

    array

    #include <stdio.h>
    #include <stdlib.h>

    char gen_letter(void)
    {
    char ch = (rand() % 26) + 'a';

    return ch;
    }

    char gen_alphanum(void)
    {
    char vAlpha[] = "D9kc1ZayFj2mTds6Mx2Q8UiCp1J9uEoIw3Aq5GYL4gNr8WhBV 3PfH7KbX6l4SvL6Ot5n";
    char ch = vAlpha[ rand() % 68 ];

    return ch;
    }

    void gen_pass(char *sPassWord, int iSize)
    {
    if (iSize < 1)
    {
    sPassWord[0] = '\0';
    return;
    }

    sPassWord[0] = gen_letter();

    for ( i=1; i < iSize; i++)

    sPassWord[i] = gen_alphanum();

    sPassWord[iSize] = '\0';
    }

    int main(void)
    {
    char password[15];

    srand((unsigned int) (GetTickCount() / 100) );

    gen_pass(password, 14);
    printf("\nPassword: ");
    printf(" \n");
    printf(" \n");


    system("PAUSE");
    return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    i is not declared in gen_pass().

    It's amazing when you actually read the compiler errors. It like, spells it right out for you!

    Use code tags when posting code.
    Last edited by itsme86; 11-04-2004 at 12:59 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I have some code that I am trying to compile and every time I try I keep getting an error on the for loop.

    Something like, "Undefined symbol 'i' in function gen_pass"? Declare i before you use it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Warning? What warning? You mean your compiler can help you find problems? :boggles:

    This dose of sarcasm was brought to you by the letter S, and by the number 17.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    Hi all

    I saw that error but I wasn't quite sure exactly where to declare the variable.

    So I declared it after this line of code:

    void gen_pass(char *sPassWord, int iSize)

    and I declared it like this:

    char 'i';

    and I got 4 errors

    then I changed it to:

    char i;

    It compiled but all it did was print out the word Password:

    Did I declare it in the right place or did I simply declare it wrong?

    Thanks again to all that answered, array

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It only printed out "Password:" because that's all you're asking it to print:
    gen_pass(password, 14);
    printf("\nPassword: ");
    printf(" \n");
    printf(" \n");
    You're never printing the password variable.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM