password

This is a discussion on password within the C Programming forums, part of the General Programming Boards category; Hi, Could anyone advise me. I am doing a project in C that requires a password. The problem I have ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Question password

    Hi, Could anyone advise me. I am doing a project in C that requires a password. The problem I have is when the user types in the password, it is displayed on screen. How can the password entered be disguised as **.Thanks RC




    void Password(void)
    {

    printf("\t\tPlease enter password ");
    scanf("%s",Passwordin);

    //check if user entered correct password marcie using strcmp

    if(strcmp(Passwordin,"marcie")==0)
    {
    printf("\n\t\tWelcome\n");
    clrscr();
    }
    else
    {
    // if password incorrect Password function recalled until correct Password entered
    printf("\t\tincorrect password\n");
    Password();
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Just a very slight modification to the above code, to make changing max. password length easier:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    #define MAXPASSWD 10
    
    int main()
    {
    
    	char buffer[MAXPASSWD + 1];
    	int i, ch;
    
    	printf("Enter a password (MAXPASSWD alphanumeric sequenc max):\n" );
    	printf("Password: ");
    
    	/* Read password but no larger than 10 characters */
    
    	for( i = 0; i < MAXPASSWD ; i++ )
    	{
    		//get character
    		ch = _getch();
    		//if user enters newline than quit
    		if(ch == '\r' || ch == '\n') break;
    		//if user enters backspace
    		if(ch == '\b')
    		{
    			//if there are no preceeding letters
    			if(i == 0) 
    			{
    				--i;
    				continue;
    			}else //if there are preceeding letters
    			{
    				printf("\b%c\b",' ');
    				i-=2;
    			}
    		}else //no backspace was entered
    		{
    			putc('*',stdout);
    			buffer[i] = (char)ch;
    		}
    
    	}
    
    	/* Terminate string with null character: */
    	buffer[i] = '\0';
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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