Thread: masking user input...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    Post masking user input...

    Hello.

    Im wondering does anyone know how to mask a users input in a C++ program. For example if someone was to type in a password you wouldnt want it not to appear in *'s.

    Any ideas or help is appreciated, thank you.

    - rootnix

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can use a combination of getch() to get a char from the keyboard and putchar('*').
    getch() is a nonstandard function but almost every compiler has it although it may be called _getch() or even __getch().Itusually lives in conio.h .consult your help files.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Using _getch (from conio.h) you will take a character from stdin and not echo it. So you put a '*' out in its place after storing the character, example:

    Code:
    #include "stdafx.h"
    #include <conio.h>
    #include <iostream> 
    using namespace std; 
    
    int main(int argc, char* argv[])
    {
    	char ch[30] = {0};
    
    	cout << "Please enter password: ";
    	for ( int i = 0;30 > i;i++ )                //make sure we don't pass the end of the array
    	{
    		ch[ i ] = _getch( );
    		if( '\r' == ch[i] )               //break on carriage return
    			break;
    		cout << '*';
    	}
    	
    	cout << endl;
    
             //do what you want with password in the case I'll display it
    	cout << "The password is: " << ch << endl;
    		
    	
    	return 0;
    }
    I'm sure there are more clever ways of do it but this should get you started.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    char pword[16];
    .
    .
    .
    cout<<"Password: ";
    getch(pword);
    putch('*');

    i dont have my help files im running MS Vis c++ 6.0. i get an error that getch() doesnt take 1 parameter(s)....

    thanks,

    - rootnix

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    i dont have my help files im running MS Vis c++ 6.0. i get an error that getch() doesnt take 1 parameter(s)....
    An online version of the help files (MSDN) is available at the MS site.

    Check Dang's example above on how to use getch().
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM