Thread: Program : enter a password

  1. #1
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41

    Program : enter a password

    HIII
    i look for a way to write a function that hide the characters and replaces them by "*"
    also using the secure text input to avoid buffer problem
    So I wrote the code for the secure text input

    here is the code :
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    void empty_buffer()
    {
        int c = 0;
        while (c != '\n' && c != EOF)
        {
            c = getchar();
        }
    }
    
    
    int readpsw(char *chaine,int length)
    {
        char *carac;
    
    
        if(fgets(chaine,length,stdin)!=NULL)
        {
    
    
            carac=strchr(chaine,'/n');
            if(carac!= NULL)
            {
                *carac='/0';
            }else
            {
                empty_buffer();
            }
    
    
        }
    
    
        else
        {
           empty_buffer();
        }
    
    
    }
    
    
    int main ( int argc, char** argv )
    
    
    {
    
    
    char psw[100];
    printf("enter a password   :");
    readpsw(psw,21);
            return 0;
    
    
     }
    any suggestion ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You can't "hide as you type" in a portable manner, you have to tell us which OS/Compiler you're using.

    Also, you need to tell the difference between say /n and \n.
    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.

  3. #3
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    i use Codeblocks
    for '/n' and '\n' i think maybe there are similar ^^
    Last edited by slyer4ever; 10-07-2013 at 03:55 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    You can't "hide as you type" in a portable manner, you have to tell us which OS/Compiler you're using.
    Quote Originally Posted by slyer4ever View Post
    i use Codeblocks
    Please read the CB FAQs FAQ-General - CodeBlocks

    Q: What is Code::Blocks?

    A: Code::Blocks is an Integrated Development Environment, aka IDE. Thus, a framework for working with source code and using compilers and linkers (in the case of Code::Blocks, these can be several).


    Q: What Code::Blocks is not?

    A: Code::Blocks is not a compiler, nor a linker. Release packages of Code::Blocks may include a compiler suite (MinGW/GCC), if not provided by the target platform already. However, this is provided "as-is" and not developed/maintained by the Code::Blocks development team.
    Tim S.
    Last edited by stahta01; 10-07-2013 at 05:32 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    Thank you @stahta01 indeed i use Code::Blocks(MinGW/GCC)

  6. #6
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    can someone give me a little hand ^^

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You didn't answer Salem's question about which OS you are using. You can find an example of what you're looking for here: FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You will need to disable echoing of user input on the console which, as Salem mentioned, is OS/compiler specific. Take a look at the Windows API documentation, specifically console functions: Console Functions (Windows). Find ones that have promising names for what you want to do, and read through their documentation, to see if they indeed will work for you. You're probably looking for something like "disable echo" or some such, which will prevent the console from echoing back the characters the user types. Note, that may not be in the function name, but rather in the documentation for a different function, that sets all the modes/attributes.

  9. #9
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    No i did
    OS/Compiler : Code::Blocks(MinGW/GCC)
    "for '/n' and '\n' i think maybe there are similar ^^" actually i don't know what's the difference i just found it on the internet

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    '\' indicates an escape sequence - it is not similar to '/'.
    Code::Blocks is [edit*] an IDE [/edit], not an operating system.
    You now have two link to read through. See if you can find answers, and come back with specific questions if you're having trouble.

    (*Thanks for pointing that out, anduril462!)
    Last edited by Matticus; 10-08-2013 at 12:06 PM.

  11. #11
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    Thank you guys i'm gonna do some research , i hope that i'll find the solution

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Actually Matticus, Code::Blocks is an IDE. MinGW (if that is what he is using) is the compiler, which would imply the OS is Windows. However, if the compiler is GCC, we can only guess it's probably some *NIX system. Still, slyer4ever, you should have mentioned all that explicitly. It's not hard to type "OS: Windows 7".

  13. #13

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Actually Matticus, Code::Blocks is an IDE.
    Yikes! I knew that! How embarrassing - my brain must have been focused elsewhere as I was typing that...

  15. #15
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    My OS : "Windows xp" this is a little old school

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To enter password without displaying it
    By hello in forum C Programming
    Replies: 3
    Last Post: 01-07-2009, 11:04 AM
  2. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  3. Enter password, hide by "*".
    By toysoldier in forum C++ Programming
    Replies: 28
    Last Post: 08-15-2004, 08:13 AM
  4. how to enter a password with asterisks
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-06-2001, 12:38 PM
  5. print astrik when enter password
    By ck12 in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2001, 10:16 PM