Password Field

This is a discussion on Password Field within the C Programming forums, part of the General Programming Boards category; I would like to have a password field similar to scanf but without echoing what you typed in... So you ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Password Field

    I would like to have a password field similar to scanf but without echoing what you typed in... So you can press backspace and redo a character if you know you messed up:

    The best I have so far is this:
    Code:
    for (x = 0; x <= 50; x++) {
    usrKey = getch();
    		
    if (isspace(usrKey)) {
    usrEntry[x] = '\0';
    break;
    }
    
    else {
    usrEntry[x] = usrKey;
    putchar('*');
    }
    
    
    }
    Any suggestions? I want it so that if you press backspace, it actually backspaces instead of adding another *...
    Last edited by Explicit; 05-21-2004 at 07:03 PM.

  2. #2
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    how would you know you messed up if your input isn't echoed. seems to me you'd have to wait till you flushed the buffer and your password was processed.

    anyway check the FAQ there are some answers there about password fields.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    19
    Like say your entering a password to get into this forum, sometimes you might know that you pressed a wrong button, and backspace... With the code above, backspace just shows up as a *...

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Just happened to look this up for another thread....
    It should be easy to modify to suit your purposes.

    How to put a Char into the Input buffer of a console?

    gg

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    19
    That is C++, I havn't even learned all of C yet...

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Code:
    cout << endl << buffer << endl;
    Skip that line then.

    gg

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    he means the one closer to the bottom not the first one
    Code:
        const int ESC = 27;
        const int BACKSPACE = 8;
        const int BUFFER_SIZE = 255;
    
        char buffer[BUFFER_SIZE];
        int c,i=0;
        do
        {
            c = getch();
    
            //accept this char if it's printable
            if (isprint(c))
            {
                buffer[i++] = (char)c;
                putch(c);
            }
    
            //support for backspace functionality
            if ((c == BACKSPACE) && (i != 0))
            {
                putch(c);   //backspace
                putch(' '); //overwrite existing char
                putch(c);   //backspace back into position
                i--;
            }
            
        } while((c != '\r') && (c != ESC) && (i < BUFFER_SIZE));
    
        buffer[i] = '\0';
    
        //did it work?
        /*of course one line comments are c99 but just ignore the next line*/
        cout << endl << buffer << endl;

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>&& (i < BUFFER_SIZE)
    >>buffer[i] = '\0';
    Danger Will Robinson, Danger! Buffer overflow alert

    Here's the FAQ entry that was mentioned earlier:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    19
    I don't want input without pressing enter - I want everything exactly like how scanf works, but printing * when you type, instead of echoing the char you typed!

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by Explicit
    I don't want input without pressing enter - I want everything exactly like how scanf works, but printing * when you type, instead of echoing the char you typed!
    scanf() can't do that, because it works on buffered IO. That's why I pointed you to that particular FAQ entry. Read it through, and you'll find a password example in there.

    [edit]
    Doh!, sorry, I should've read your post properly, I see you're already part way there. Just ignore my ramblings.... it's late (poor excuse, I know )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    19
    Actually, your password entry code is more like what I'm looking for than mine is! In mine, if you press backspace it takes it as a char, heh.

    Your code works for me
    Last edited by Explicit; 05-22-2004 at 09:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 02:42 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  5. password field
    By trekker in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-27-2003, 01:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21