Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 962
  • Last Modified:

hide console input

Using C++, how to display a prompt for a password, ie a cin that will hide the text that the user types in?
0
fyuber
Asked:
fyuber
  • 2
  • 2
2 Solutions
 
sunnycoderCommented:
Hi fyuber,

set noecho and use getch()
man getch() for more info

Cheers!
Sunny:o)
0
 
fyuberAuthor Commented:
Sorry, Im rather new to this. How to set noecho?
0
 
brettmjohnsonCommented:
Have you tried calling getpass() instead?

NAME
     getpass - get a password

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <pwd.h>
     #include <unistd.h>

     char *
     getpass(const char *prompt);

DESCRIPTION
     The getpass() function displays a prompt to, and reads in a password
     from, /dev/tty.  If this file is not accessible, getpass() displays the
     prompt on the standard error output and reads from the standard input.

     The password may be up to _PASSWORD_LEN (currently 128) characters in
     length.  Any additional characters and the terminating newline character
     are discarded.

     The getpass() function turns off character echoing while reading the
     password.

RETURN VALUES
     The getpass() function returns a pointer to the null terminated password.

0
 
fyuberAuthor Commented:
thanks, it works but i still want to know how to use sunny's method. guess I should increase points but I'll have to wait for them to accumulate.
0
 
sunnycoderCommented:
here are two more methods:

1. system ( "stty echo" );
    system ( "stty -echo" );  //look into man page before using this

2. using ncurses
    #include <curses.h>
     int noecho(void);
     man curs_inopts
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now