convert all the keys to *****

This is a discussion on convert all the keys to ***** within the C++ Programming forums, part of the General Programming Boards category; hi how can i convert all the keys for the user how enter for example his passwort to char ********...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    7

    convert all the keys to *****

    hi

    how can i convert all the keys for the user how enter for example his passwort
    to char ********

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    It depends on your OS and compiler.
    Do a board search, this is a common question.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Here's a Windows API version. Remember to include windows.h...

    Code:
    char *getPass(char *szBuffer, size_t len)
    {
    	size_t i;
    	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    	DWORD dwOld, dwNumRead, dwNumWritten;
    	if((hIn) && (hOut))
    	{
    		if(GetConsoleMode(hIn,&dwOld))
    		{
    			if(SetConsoleMode(hIn,ENABLE_PROCESSED_INPUT))
    			{
    				for(i=0;i<len;i++)
    				{
    					if(ReadConsole(hIn,&szBuffer[i],1,&dwNumRead,NULL))
    					{
    						if(szBuffer[i] == '\r')
    						{
    							WriteConsole(hOut,"\n",1,&dwNumWritten,NULL);
    							szBuffer[i] = '\0';
    							break;
    						}
    						else WriteConsole(hOut,"*",1,&dwNumWritten,NULL);
    					}
    					else break;
    				}
    				SetConsoleMode(hIn,dwOld);
    				return szBuffer;
    			}
    		}
    	}
    	return NULL;
    }

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    If using the WIN32 API, the edit control has a 'password' style ES_PASSWORD which will automatically replace text with asteriks.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    7
    but what is the size_t ......................

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    size_t is a data type in the standard C library, inside stdio.h. If size_t is not defined in windows.h, which I believe it is, you can include it in your C++ program by including cstdio.

    GIYF.
    Last edited by MacGyver; 05-22-2007 at 07:26 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I think size_t is associated with <cstddef>.

    http://www.cplusplus.com/reference/c...ef/size_t.html

    Edit: Actually the same site indicates that it's also in <cstdio>. I'm confused. Why is it guaranteed to be defined in more than one standard header file?

    Edit: This is important to me since my code uses std::size_t a lot (and I always qualify it with std:: since I don't think it's guaranteed to be in the global namespace).
    Last edited by robatino; 05-22-2007 at 08:51 AM.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This site names 4 header files where size_t is define:

    • stdio.h
    • stddef.h
    • stdlib.h
    • string.h


    The date type is important for strings, as well as malloc() and other functions, so I guess they chose the route of defining it quite a few times as opposed to just keeping it in one header file that all the others would be dependent upon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 07:14 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 02:54 PM
  4. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  5. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM

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