Thread: Print * when entering a password.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    6

    Print * when entering a password.

    Can any1 share that how to make segment program when user entering a password,it will print * instead of the real character that user is entering?

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Basically your choices are 1. disable echoing of the console using an OS-specific function (For a Windows solution see here - c - How to disable echo in windows console? - Stack Overflow)

    Or 2. Use an abstraction library like libreadline or ncurses which provides control over what gets echoed to the terminal in an OS-independent way.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    thanks for your reply,but i dont understand.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Normally when the user types on the terminal, the input is "echoed" back to the output and displayed. If you want to display only a "*" for each character, then you must do two things:
    1. turn off echoing of the input.
    2. each time you read a character (with getchar() for example), printout a "*"


    I gave two possibilities for solving issue #1. Does it make sense now? If not, which part is not clear.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    103
    you can try this code...
    Code:
    #include<iostream>
    #include<conio.h>
    #include<string.h>
    using namespace std;
    void test()
    {
        char pass[10],ch;
        int i;
       cout<<"enter the password(10 characters max.): "; 
       for(i=0;i<10;i++)
       {
           pass[i]=getch();
            if(pass[i]==13)//ASCII value of enter button (tested)
            {
                pass[i]='\0';
                break;
            }
            else
            {
                cout<<"*";
            }
       }
       if(strcmp(pass,"password")==0)
       {
           cout<<"\n\nthe password is correct!";
       }
       else
       {
           cout<<"\n\nthe password is incorrect!!!";
       }
    }
    int main()
    {
        test();
        getch();
            return 0;
    }
    there are some problems with backspace function in this code so i removed it.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Mukul Kumar View Post
    you can try this code...
    Code:
    #include<iostream>
    #include<conio.h>
    #include<string.h>
    using namespace std;
    void test()
    {
        char pass[10],ch;
        int i;
       cout<<"enter the password(10 characters max.): "; 
       for(i=0;i<10;i++)
       {
           pass[i]=getch();
            if(pass[i]==13)//ASCII value of enter button (tested)
            {
                pass[i]='\0';
                break;
            }
            else
            {
                cout<<"*";
            }
       }
       if(strcmp(pass,"password")==0)
       {
           cout<<"\n\nthe password is correct!";
       }
       else
       {
           cout<<"\n\nthe password is incorrect!!!";
       }
    }
    int main()
    {
        test();
        getch();
            return 0;
    }
    there are some problems with backspace function in this code so i removed it.
    There are more problems than just the backspace function:

    1. You posted C++ code, and this is the C forum.
    2. Your code could use better indentation/formatting. Readability is key, easy to read code means fewer mistakes, and they're easier to find and fix. Also, some spaces around your operators would be nice, to also improve readability.
    3. You have an arbitrary, small limit for a password (9 chars), but you don't #define a constant for that, which is just bad style.
    4. Your program doesn't correctly handle the password. It allows you to fill the entire pass array with password chars, leaving no room for the null character. The missing null character means strcmp could go off the end of the array, resulting in undefined behavior (which is bad).
    5. You use cout for output, but getch for input. Don't mix C and C++ unnecessarily.
    6. Don't use magic numbers like 13. 13 (carriage return) is actually wrong in many cases (clearly you didn't test bit of code widely), other (non-Windows) systems use a new line character instead. Use the character constant '\n' which will work correctly for all line-endings in any text-mode stream. Besides, it's clearer, platform-independent and not ASCII-specific (yes, there are non-ascii systems out there).
    7. Other Windows-specific stuff that should be avoided unless you know the user wants a Windows-only solution.
    8. conio.h is old and outdated. Consider newer, more portable options.
    9. getch() is a Windows-specific function. Prefer the standard getchar() function instead. Again, there are modern, portable libraries that properly handle unbuffered terminal input, disabling echo, etc.
    10. Even if you should be using getch, you're doing it wrong. Read the documentation, it returns an int, not a char. A char is too small to handle all possible characters and special return values like EOF. Your code will mis-interpret things like EOF as some other char and not work correctly.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here is windows code to look at: Problem with inputting password?

    Making your own getch() for *nix style os's isn't too hard either: while getch question

    gg

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Thanks for all the reply
    appreciated

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I read this far too often (I know - don't read so much -- hilarity ensues, I'm sure):
    conio.h is old and outdated. Consider newer, more portable options.
    So is the wheel, and pants. Both designs have had a good run, so far. For everything that conio.h ever did, it still does, and does it just fine. Not taking anything away from PDCurses, but let's not take anything away from conio.h either. Fair is fair. A lot of students visiting the site still have to work with conio.h, not PDCurses, etc.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Adak View Post
    So is the wheel, and pants. Both designs have had a good run, so far. For everything that conio.h ever did, it still does, and does it just fine. Not taking anything away from PDCurses, but let's not take anything away from conio.h either. Fair is fair. A lot of students visiting the site still have to work with conio.h, not PDCurses, etc.
    1. People actually still wear pants and use wheels. The analogy is a bit off.

    2. If someone uses conio.h in the code here, there's a good chance that the users can't test it because conio.h is dos only.

    Nothing personally wrong with conio.h, but users of these function calls may be better off seeking the help of a "DOS Programming" forum.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by c99tutorial View Post
    1. People actually still wear pants and use wheels. The analogy is a bit off.
    Incorrect.


    2. If someone uses conio.h in the code here, there's a good chance that the users can't test it because conio.h is dos only.
    Wrong again. It works great in Windows 98, Windows2000, WindowsME, WindowsXP, and Windows 7.

    Nothing personally wrong with conio.h, but users of these function calls may be better off seeking the help of a "DOS Programming" forum.
    You're basing this on your feelings, not on facts:

    1) conio.h works on all DOS AND on the above listed Windows OS versions

    2) it's standard fare and required for many schools in India, and elsewhere.

    3) we get a lot of visitors who use it. We shouldn't be telling them to go to a DOS programming forum. Really, a "DOS programming forum"?? ROFL!

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Adak View Post
    Really, a "DOS programming forum"?? ROFL!
    Exactly my sentiment. It doesn't matter to me if a particular school uses it. It's still DOS even if a certain operating system emulates it. Personally I'd use DOSBox to run such a program, but actually, why bother. If the program uses a library I don't want to install, then it's basically untestable.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I checked and if you install the APR library then you can use the apr_password_get function. Not sure of the details because I've never used it:

    Apache Portable Runtime: General Purpose Library Routines

    Your choice of what libraries to install. Just say no to conio

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by c99tutorial View Post
    Exactly my sentiment. It doesn't matter to me if a particular school uses it. It's still DOS even if a certain operating system emulates it. Personally I'd use DOSBox to run such a program, but actually, why bother. If the program uses a library I don't want to install, then it's basically untestable.
    That's fine for YOU, but what about them, and this forum? I don't believe you're seeing all the POV's on this matter.

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >>Just say no to conio
    But there is no reason to say no. I've yet to come across a windows compiler that doesn't provide it. So I see nothing wrong with using something that is as "windows-portable" as the WinAPI. We could implement _getch() ourselves using the WinAPI - but there is no need since conio.h exists and is supported by windows compilers.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  3. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. print astrik when enter password
    By ck12 in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2001, 10:16 PM