Thread: Passwords/keys

  1. #1
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36

    Passwords/keys

    When you type a key or pass, how can you make it so it displays as ******

    Thanks

  2. #2
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Code:
       //ENTERING PASSWORD
       //DECLARE USERPASSWORD[] AND SET IT ALL TO '\0'
       int u = 0;
       char a ;
       while (1){
          a=getch();
          if (int(a)==13){    //13 = enter
             userpassword[u]='\0';
             break;
          }
          if (int(a)==8){                           //8 = backspace
             userpassword[u-1]='\0';
             u--;
             putchar('\b');
             putchar(' ');
             putchar('\b');
          }
          
          else {
             printf("*");
             userpassword[u]=a;
             u++;
          }
       }
    This would be good to put on the faq if it isnt alerady there.

  3. #3
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Actually I was able to cut that down a few lines.
    Code:
       //ENTERING PASSWORD
       //DECLARE USERPASSWORD[] AND SET IT ALL TO '\0'
       int u = 0;
       while (1){
          char a=getch();
          if (int(a)==13){                         //13 = enter
             userpassword[u]='\0';
             break;
          }
          else if (int(a)==8){                           //8 = backspace
             userpassword[--u]='\0';
             putchar('\b',' ',\b');
          }
          
          else {
             printf("*");
             userpassword[u++]=a;
          }
       }
    Last edited by tim545666; 02-16-2002 at 10:45 PM.

  4. #4
    Unregistered
    Guest
    that's bad programming...

    try converting most of the if statements to a switch function:

    example:

    int x;
    cin>>x;
    switch(x)
    {
    case 0: cout<<"hey, you entered a zero... "; break;
    default: cout<<x; break;
    }
    ...

  5. #5
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Why is it bad? I agree that if if-else statements get too big, they are unreadable, but in this case an if-else will do. According to my comp sci teacher, switch statements are slower and sort of clunky. I don't see a point to a switch here. Actually I found another error. I'll edit my previous post.

  6. #6
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    hmmm

    could you possibly write the code for C++, cuz printf("blah blah").. isnt that C? yea im a noob...

  7. #7
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Printf is C, but it can also be used in C++. I was using cout but it was giving me problems so I used printf. The rest is pure C++, except for maybe putchar, that may be C. I never learned C so I don't know.

    I'm a noob too, don't worry about it.

  8. #8
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36
    Tim...The printf and putchar are giving me errors, the error message is call to undefined function, i changed printf to cout and that worked but putchar is still giving me an error.

  9. #9
    Registered User quiksilver9531's Avatar
    Join Date
    Nov 2001
    Posts
    36

    Talking

    my bad, i forgot about #include <stdio.h> hehe

  10. #10
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    why not just make your password *****? saves time...

Popular pages Recent additions subscribe to a feed