Click here to Skip to main content
16,009,176 members
Please Sign up or sign in to vote.
1.06/5 (3 votes)
See more:
how are you good people, how do i code a password in C such that when being entered it doesnt show entered keys but asterisks
Posted
Comments
Patrice T 19-Oct-15 17:24pm    
Google don't find ?
Sergey Alexandrovich Kryukov 19-Oct-15 18:20pm    
The question does not really makes sense, because it all depends on the UI framework/library you are using. This problem is solved in all places.
—SA
Patrice T 19-Oct-15 20:56pm    
may be it is bare bone C with console app !
Sergey Alexandrovich Kryukov 19-Oct-15 21:11pm    
Well, maybe. The inquirer has to tell us the story... :-)
—SA
Patrice T 19-Oct-15 21:20pm    
:-)

1 solution

C++
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

void beep(){
    printf("\a");
}

int InputPass(char * prompt,char *buff,int maxLen){
    int c,n=0;
    int x= _wherex(),y= _wherey();

    printf("%s: ",prompt);
    *buff=0;
    while(c=_getch()){
        if(c==0xd) return n;
        else if(c==8){
            if(!n)
                beep();
            else{
                printf("\b \b");
                n--;
            }
        }
        else if(c==0x1b){
            break;
        }
        else{
            if(!isprint(c)){
                beep();
                continue;
            }

            if(n<maxlen){>
                buff[n]=c;
                n++;
                buff[n]=0;
                printf("*");
            }else{
                beep();
            }
        }
    }
    return 0;
}


int main(){

    char buff[20];
    int n=InputPass("Enter password",buff,sizeof(buff));
    printf("\n");
    if(n==0){
        printf("input canceled\n");
    }else{
        printf("password is '%s'\n",buff);
    }

    return  0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900