Easy Question

I am talking a computer Science course at my school. The teacher is not really up on his coding and I have a question. If I am inputing a password with cin, or gets() how can I make it so that it doesn't display the password as the user types it.



THanks,

Andrew.



Please email responses as well.


Comments

  • You can use (from conio.h) getpass, like this:

    char *input = getpass("Enter your password: ");



    That isn't standard though, if I am correct. Or you can write your own getpassword function like this (using both stdio.h and conio.h):



    char *getpassword(char filler,unsigned int maxlen) {

    char *input = new char[maxlen];

    char ch;

    unsigned int a;

    for(a=0; a<maxlen && (ch=getch())!=13; a++) {

    input[a] = (unsigned int) ch;

    printf("*");

    }

    input[a] = '';

    printf("
    ");

    return input;

    }



    You can use that like this:



    void main(void) {

    char *password = getpassword('*',100);

    printf("You entered %s.",password);

    }



    Does that help? :)


  • Replace the printf("*") with printf("%c",filler); to make it work right :)




  • Thanks, I need to find a way to do it easily....I don't have a maxlen. It could be 33 or 2



    I don't know


  • : Thanks, I need to find a way to do it easily....I don't have a maxlen. It could be 33 or 2



    Well, one possible solution would be to modify Null and Void's second example to use malloc() and realloc() to handle arbitrary length passwords and return a pointer to the allocated memory (which you'll need to remember to free()), but realistically, it would probably be much simpler (as Null and Void's examples show) to set a reasonable maximum password length (say, 128) then just drop extra characters if the user enters something longer than that. I don't know about you, but none of my passwords are 128+ characters long. :-)



    As a side note, on Un*x systems, getpass() is declared in "unistd.h". There isn't a "conio.h" header at all, as many people here seem to refer to. There isn't a getch(), either, unless you a) write it yourself using the select() or poll() system call, or b) use curses or ncurses or another terminal programming library that provides it.





    (.piliq.)




  • Yes, I knew that it wouldn't work in Unix or its derivitives, but I don't have the libraries or the system set up to make or run unix based programs, so I'm kind of ignorant in those cases :)



    Every DOS/OS2/Unix program I make is very basic because of this :)




  • : Yes, I knew that it wouldn't work in Unix or its derivitives, but I don't have the libraries or the

    : system set up to make or run unix based programs, so I'm kind of ignorant in those cases :)



    I figured you knew, but some people might not. They do now. :-)



    : Every DOS/OS2/Unix program I make is very basic because of this :)



    Nothing wrong with that: basic, text stream-oriented, standard-compliant C programming == portable. For fancy console stuff, look into the ncurses (or another curses-compatible) library. It's standard on Unix, but there are free implementations available for other OS's as well. I've played with it a little, and it's pretty easy. Functions are available for cursor movement, screen clearing, colors, etc. I think I remember a curses emulator or something for DJGPP, but I haven't used Windows or DOS in so long I don't remember.





    (.piliq.)




Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories