Thread: Login program

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by cpjust View Post
    Why not? getchar() reads a character from the keyboard and prints it to the screen. getch() only does half the work, so you'd think it would be easier to implement than getchar().
    That's not entirely true. In general, this is more how it actually works:

    getchar() simply reads the char from the buffer of the FILE *. If you don't have anything in the buffer to read, the OS-specific read function is called. The printing of the char to the screen is NOT done by getchar(). That is done by your shell/OS read function/etc. etc..

    The default behavior of your system is to echo chars from beyond stdin into the screen. Think about it for a second. The chars are being printed to the screen BEFORE it's even accepted by stdin (because stdin actually wraps the OS version of stdin).

    Functions like getch() actually change the terminal settings so echoing is NOT done in this manner. On Windows this is done with SetConsoleMode() I believe. For *nix systems you have to similarly play with the settings and have it so the output is not echoed to stdout.

    In fact, I think both Windows and *nix systems allow you to preserve the terminal settings. You might be able to write a program which changes your terminal mode, quits, and then you could start another program which uses the new terminal mode where getchar() would behave like getch().

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    12

    Red face Here the login program in C

    Heres the program to accept a password.While typing the characters are masked using '*' character.It also accepts backspace.Displays the entered pw when user hits ENTER key

    heres the program:
    check out:
    http://www.lern2hack.blogspot.com/20...d-mask-it.html
    Last edited by Salem; 12-15-2007 at 04:10 AM. Reason: Second time I've edited your posts for format abuse - 3rd time will be less gracious.

  3. #18
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Quote Originally Posted by sriki View Post
    Heres the program to accept a password.While typing the characters are masked using '*' character.It also accepts backspace.Displays the entered pw when user hits ENTER key

    heres the program:
    check out:
    http://www.lern2hack.blogspot.com/20...d-mask-it.html
    Sweet! Non-indented code that doesn't compile(VC2008)!
    Don't quote me on that... ...seriously

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Oh look, sriki is back pushing his hacker wannabe site with dubious code full of mistakes.
    Tell me, what happens when I type in 30000 characters into your "program" ?
    More like a "how to write code which makes it easy to be hacked" site IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention it's so poorly written. if (ch == 13) <--- OK, how the heck am I supposed what ASCII value 13 is?
    sriki: It's fine to teach other how to do things, but please, avoid poor programming concepts and things. If you're going to teach someone, do it right! There's enough of bad programming examples out there - we don't need more of them!
    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.

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    That is horrible code.

    What I posted should work on Windows. I believe someone might have posted something similar for *nix systems somewhere else here (in the C++ section perhaps).

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Here's my version (based on MacGyver's getch() function):
    Code:
    /* prototypes */
    char mygetch(void);
    void getpassword(char password[], int maxlength);
    
    /* input password from user */
    void getpassword(char password[], int maxlength)
    {
    	int i=0, done=0;
    	char c;
    
    	printf("Please input password (max. &#37;d characters): ", maxlength);
    	fflush(stdout);
    
    	while (!done)
    	{
    		c = mygetch();
    		switch(c)
    		{
    			case '\r':
    			case '\n':
    				done = 1;
    				break;
    			case '\b':
    				if (i > 0)		/* perform backspace only if characters have already been input */
    				{
    					printf("\b \b");
    					i--;
    				}
    				break;
    			default:
    				if (i<maxlength && isprint(c))	/* save character in password as long as it's not a none-printing character */
    				{
    					password[i++] = c;
    					putchar('*');
    				}
    				break;
    			}
    		fflush(stdout);
    	}
    	password[i] = '\0';
    }
    
    /* like getchar() except without echo */
    char mygetch(void)
    {
      static HANDLE hIn;
      static DWORD  NumRead;
      static INPUT_RECORD InRec;
      static char c;
    
      hIn = GetStdHandle(STD_INPUT_HANDLE);
      if(hIn == INVALID_HANDLE_VALUE)
      {
        printf("GetStdHandle poop");
      }
      else
      {
        do
        {
          ReadConsoleInput(hIn, &InRec, 5, &NumRead);
          if (InRec.EventType == KEY_EVENT)
          {
            if (InRec.Event.KeyEvent.bKeyDown == 1)
            {
              c = InRec.Event.KeyEvent.uChar.AsciiChar;
              /* testing print
              printf("  KDown  Repeat  KeyCode  ScanCode  Unicode  Ascii  Ctrl-State \n");
              printf("  %3x    %3x     %3x      %3x       %3x      %3x    %3lx \n",
                      InRec.Event.KeyEvent.bKeyDown,
                      InRec.Event.KeyEvent.wRepeatCount,
                      InRec.Event.KeyEvent.wVirtualKeyCode,
                      InRec.Event.KeyEvent.wVirtualScanCode,
                      InRec.Event.KeyEvent.uChar.UnicodeChar,
                      InRec.Event.KeyEvent.uChar.AsciiChar,
                      InRec.Event.KeyEvent.dwControlKeyState );
              */
              /* printf("|if %c %02x ", c, c);  testing*/
              break;
            }
          }
        }
        while( c !=  '\r' );
      }
      return c;
    }
    Thanks to all the people who helped me out with it (from this forum)
    Last edited by Abda92; 12-15-2007 at 11:35 AM.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    Error	1	error C2065: 'KEYLENGTH' : undeclared identifier
    You probably meant to put maxlength there.
    Last edited by Elysia; 12-15-2007 at 11:42 AM.
    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.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    sorry Elysia, but the function itself is the harder part. I assumed you can just make the prototype from the function.
    About the KEYLENGTH error, I'm sorry it was supposed to be maxlength.

    I edited the code anyway

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, I suppose you can ignore the prototypes, I guess.
    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.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    And I did mention that it depends on MacGyver's getch() function, which is already known to be Windows specific and to require windows.h

  12. #27
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Not to mention it's so poorly written. if (ch == 13) <--- OK, how the heck am I supposed what ASCII value 13 is?
    sriki: It's fine to teach other how to do things, but please, avoid poor programming concepts and things. If you're going to teach someone, do it right! There's enough of bad programming examples out there - we don't need more of them!
    And what if you aren't even on an ASCII system, like z/OS?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Login Form for program
    By Houssen in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2008, 02:35 PM
  2. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  5. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM