Thread: Masking input

  1. #1
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    Masking input

    How would one go about masking an input... Like when entering passwords and pins and such? Running Windows Vista, making a Console app. Thanks guys.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by bradszy View Post
    How would one go about masking an input... Like when entering passwords and pins and such? Running Windows Vista, making a Console app. Thanks guys.
    You probably think you are the first who is asking this question...

    Search forum for similar posts, read the answers, write your code and ask question about problems you encountered

    For the start - read thsi http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    It explains how you can read an input before user press enter
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    getch()

    I'm having some trouble using it. Here's my code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <windows.h>
    #include <winable.h>
    #include <string>
    #include <winuser.h>
    #include <stdio.h> 
    #include <conio.h> 
    #include <ctype.h> 
    
    
    using namespace std;
    int main(int argc, char *argv[])
    {
    int ch;
    int i = 0;
    char PROTECTEDLOL[12];
    char password[90];
    int SECRETANSWER;
    LOL:
    system("color fc");
    system("cls");
    
    puts ("What would you like to do?");
    puts ("Enter password");
    puts (" OR " );
    puts ("Press [2] To Exit" );
    fflush(stdout);
    
     while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r' 
              && i < sizeof(PROTECTEDLOL) - 1)
      {
        if (ch == '\b' && i > 0) 
        {
          printf("\b \b");
          fflush(stdout);
          i--;
          PROTECTEDLOL[i] = '\0';
        }
        else if (isalnum(ch))
        {
          putchar('*');
          PROTECTEDLOL[i++] = (char)ch;
        }
      }
    
    if (PROTECTEDLOL=="password")
    {
    	
    	puts ("Hi");
    }
    
    else if (PROTECTEDLOL=="2")
    {
         cout<<endl<<endl<<"Bye"<<endl;
         Sleep(3000);
         return 0;
         }
    	
        else
    	{
    		MessageBox(NULL, "Password is invalid", "Denied", 48 | 1);
    		MessageBox(NULL, "Access is denied", "Denied", 48 | 1);
    		goto LOL;
    	}
    	
    return 0;
    }
    It gives me my error EVERY time. I don't know how to make it work. Any help? Thanks.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    conio and getch are for Borland compilers
    what compiler are you using?
    what is the error?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Dev-C++,
    And by 'my error' I'm refering to line 62, my 'else' statement.
    I've looked in Dev-C++'s 'include' directory, and conio.h is in there.
    Thanks to anyone that can help with my problem.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if (PROTECTEDLOL=="password")
    This compares pointers -> will never be true
    use strcmp()
    Kurt

  7. #7
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Like:
    Code:
    strcmp(PROTECTEDLOL=="password");
    ?

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    more like
    Code:
    if ( strcmp(PROTECTEDLOL,"password") == 0 )
    or
    Code:
    if ( !strcmp(PROTECTEDLOL,"password"))
    Kurt

  9. #9
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Oh thankyou.

    EDIT:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <windows.h>
    #include <winable.h>
    #include <string>
    #include <winuser.h>
    #include <stdio.h> 
    #include <conio.h> 
    #include <ctype.h> 
    
    
    using namespace std;
    int main(int argc, char *argv[])
    {
    int ch;
    int i = 0;
    char PROTECTEDLOL[BUFSIZ];
    char password[BUFSIZ];
    int SECRETANSWER;
    LOL:
    system("color fc");
    system("cls");
    
    puts ("What would you like to do?");
    puts ("Enter password");
    puts (" OR " );
    puts ("Press [2] To Exit" );
    fflush(stdout);
    
     while ((ch = getch()) != EOF 
              && ch != '\n' 
              && ch != '\r' 
              && i < sizeof(PROTECTEDLOL) - 1)
      {
        if (ch == '\b' && i > 0) 
        {
          printf("\b \b");
          fflush(stdout);
          i--;
          PROTECTEDLOL[i] = '\0';
        }
        else if (isalnum(ch))
        {
          putchar('x');
          PROTECTEDLOL[i++] = (char)ch;
        }
      }
    
    if ( !strcmp(PROTECTEDLOL,"password"));
    {
    	
    	puts ("Hi");
    }
    
    else if ( !strcmp(PROTECTEDLOL,"2"))
    {
         cout<<endl<<endl<<"Bye"<<endl;
         Sleep(3000);
         return 0;
    }
    	
        else
    	{
    		MessageBox(NULL, "Password is invalid", "Denied", 48 | 1);
    		MessageBox(NULL, "Access is denied", "Denied", 48 | 1);
    		goto LOL;
    	}
    	
    return 0;
    }
    ERRORS:
    Code:
    55 C:\Users\bradszy\Desktop\Untitled1.cpp expected primary-expression before "else" 
    55 C:\Users\bradszy\Desktop\Untitled1.cpp expected `;' before "else" 
    62 C:\Users\bradszy\Desktop\Untitled1.cpp expected primary-expression before "else" 
    62 C:\Users\bradszy\Desktop\Untitled1.cpp expected `;' before "else"
    Last edited by bradszy; 02-23-2008 at 03:58 AM.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if ( !strcmp(PROTECTEDLOL,"password"));  <<-- remove that semicolon

  11. #11
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Still says invalid password. This is interesting.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bradszy View Post
    Still says invalid password. This is interesting.
    Guess PROTECTEDLOL is not properly terminated
    try
    Code:
        else if (isalnum(ch))
        {
          putchar('x');
          PROTECTEDLOL[i++] = (char)ch;
          PROTECTEDLOL[i] = 0;
        }
    Kurt

  13. #13
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Thanks Kurt, you're pretty good at this eh? Worked perfectly. Thanks again, it's really appreciated.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another suggestion for you, in case you have time or is willing: http://cpwiki.sf.net/Indentation
    Good luck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I'm partial to this implementation - no particular reason
    http://cboard.cprogramming.com/showp...94&postcount=5

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. Masking user input in C
    By alpha561 in forum C Programming
    Replies: 4
    Last Post: 05-23-2002, 02:18 PM