Thread: about password

  1. #1
    Unregistered
    Guest

    Question about password

    I'm a newbie in C
    Recently I meet a problem that I don't know is there any solution can solve it!

    I want to write a function that the prog will display a '*' after I input 1 character.

    I was tried getch().But not work as my imagine

    Can any one help me?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Perhaps you could view the many theads on this topic that have already been answered with the search function.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    tks alot

  4. #4
    lemurian
    Guest

    Post

    #include <conio.h>
    #include <iostream.h>

    int main()
    {
    char *pass;

    pass=getpass("");

    cout<<pass;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > #include <conio.h>
    > #include <iostream.h>

    What's wrong with this picture? Oh, I know! This is not the C++ board!

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    Below is some code I wrote a while ago which could be improved, etc, it will accept upto 20 characters and will display the character but when delete is hit will return a ., should be pretty easy to change.

    Code:
    void prompt(char word[21])
    {
    	char ch;
    
    	for (ch=0;ch<21;ch++)
    		word[ch]='\0';
    	do
    	{
    		ch = getch();
    		if (ch==8 && strlen(word)>0)        /*if backspace is pressed*/
    		{
    			if (strlen(word)==20)
    				_setcursortype(2);
    			textcolor(7);
    			word[strlen(word)-1]='\0';
    			cprintf("\b.\b");
    		}
    		else
    		if ((ch>=32) && (ch<=255) && ((strlen(word))<20))  /*if a key is pressed*/
    		{
    			textcolor(15);
    			word[strlen(word)]=ch;
    			cprintf("%c",ch);
    			if (strlen(word)==20)
    				_setcursortype(0);
    		}
    		else
    		if ((ch==8 && strlen(word)==0) || (ch>=32 && ch<=255 && strlen(word)==20))
    			printf("\a");
    	} while(ch != 13);                              /*until enter is pressed*/
    }

    the line that says: cprintf("%c",ch);
    change to cprintf("*");
    and it should make it all work perfectly, if you need more characters change the values refering to 20 and 21 to a higher number or better yet declare a constant to hold it.
    Hope this helps

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and it should make it all work perfectly
    Assuming that the guy uses a Borland compiler, which is the only one to my knowledge that supports all of the nonstandard functions that you use in that snippet.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    I dont really know the difference between the compilers honestly, which parts in particular are borland specific?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Everything in conio.h is implementation defined and nonstandard. The only two functions from conio in that program that I have with MSVC++ is cprintf and getch. I also have Borland C++, but nonstandard functions are best avoided when possible.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Unregistered
    Guest
    this functions will go rghit:
    Code:
    int getcode(void)
    {
    	int t;
    	t = getch();
    	
    	if ( ( t == 0 ) || ( t == 0xE0 ) )
    		t = getch() ;
    
    	return t;
    }
    //---------------------------------------
    //---------------------------------------
    void getpass(char pass[])
    { 
    
    	int i, c;
    	i=0;
    	c=0;
    
    		c = getcode();
    
    			while( (c != 13) && (i < 8) ) 
    			{ 
    				if( c == 8 )
    				{
    					if( i == 0 )
    						;	// Do Nothing....
    					else
    					{
    						i-- ;
    						printf("\b \b");	//Back than print blank ' ' then back...
    					}//else
    				}
    				else if( ((c>=65) && (c<=90)) || (( c>=97 ) && ( c<=122 )) || (( c>=48 ) && ( c<=57 )))
    				{	// make sure that the input is not anything but A-Z || a-z || 0-9 ...
    					pass[i] = c ;
    					i++ ;
    					printf("*");
    				}//else if
    					
    				c=getcode();
    			}//while 
    	
    			if( i<8 ) pass[i] = '\0';
    			// give the string NULL if not full (less than 8) ...
    }// getpass

    see you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading a password from a file.
    By medeshago in forum C Programming
    Replies: 15
    Last Post: 12-21-2008, 07:20 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. password
    By hammers6 in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 12:14 AM