Thread: password program

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    password program

    hi..i am trying to write a small password program..this is the code i have written...

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
      clrscr();
      int count,pass[5];
      printf("Enter password..\n");
      count = 0;
      while(count < 5)
      {
         pass[count] = getch();
         count = count + 1;
         printf("*");
      }
      return 0;
    }
    The following is my problem..
    1.when i press "RETURN" key or "BACKSPACE" key.. the character "*" is being displayed..
    2.rather when "BACKSPACE" key is pressed i want the previously printed stars to be deleted.. and when "RETURN" key is pressed i want to compare the entered password with the actual password..

    Can somebody please Guide me..

    Thanking you,
    V.Narendra

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You could try to check the character before you print "*", for example:

    Code:
    if( isprint(pass[count]) ) {
       printf("*");
    }
    Obviously you need to do this before you update count. BTW if you plan on using pass as a string you need to terminate it with \0, which means you only have 4 characters for the actual password.

    For number 2, I think you will need more control over the terminal (something like x,y coordinates and refresh) I don't know how much of that conio.h will provide for you.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    thanks..for the reply..
    but i dont know what does this isprint function does..
    can you please explain..

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    isprint() tests if the character is printable, and if not returns false. Backspace and return would return false as they are not printable characters. You need to include ctype.h to use isprint() btw.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    pass[count] = getch();
    count = count+1;
    
    /* turns into: */
    
    pass[count++] = getch();
    Also, if you're only programming this for yourself, go ahead, but realize that conio.h and its associated functions are outdated and system-specific.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    All components of a for loop is there (init, condition, update), so that is another option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password Program
    By lukesowersby in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 11:36 AM
  2. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  3. Program Password
    By west in forum Windows Programming
    Replies: 8
    Last Post: 03-14-2005, 10:32 PM
  4. Password program
    By subdene in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2002, 07:43 AM
  5. Password Program
    By TeenyTig in forum C Programming
    Replies: 5
    Last Post: 02-05-2002, 08:32 PM