• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 955
  • Last Modified:

How do you hide an input field in c++?

I was wondering how do you hide an input field so it shows nothing or show ****'s in the words place, like for passwords and stuff?
0
Marvelous
Asked:
Marvelous
  • 4
  • 2
  • 2
  • +2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Several ways come to mind.

Perhaps the most "straight forward" of the "build your own" is to use the object's OnKeyPress() event/method.  In OnKeyPress(), just collect the characters in your own buffer, and set Key to '*' before exiting.

Borland has a PasswordDialog() object that you may want to check out.  I'm sure the other C++ implementations have similar functionality.


Kdo
0
 
SalteCommented:
MFC also have an extension of an edit box that allows you to set a character to echo in the box, usually '*'. Whatever is typed a '*' is echoed (or whatever char is stored in that property).

Alf
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Borland uses a PasswordChar property on the TEditBox object.

Kdo
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
FrancozCommented:
In MFC you can set the property of edit field as password in the styles tab of properties. This will make the field behave as a normal password.

If you wanna do it progrmatically then capture the OnKeyDown() event for the edit field.

OnKeyField()
{
//Get the input char
//Display * instead of input char
//Add up characters to password variable each time a char
//is accepted
}
0
 
MarvelousAuthor Commented:
Sorry if I mislead you guys, but I should have told you guys that I wanted to do this in a console application.
0
 
SalteCommented:
Well, in a console application you don't have "input fields" as it is not a graphic application.

Anyway, for a console application it is easy enough, it depends on the platform though.

For unix systems you can turn off echo and if you want * to appear instead you also turn off 'wake up on return' mode so that your process can read each character as it is input without waiting for return. You then manually echo each character yourself as '*' regardless of the character read.

If you simply don't want any char to show up you can just turn off echo and that's it. Remember to turn echo back on when the password is read.

On windows you use a separate low level function getch() to read the char and if you want '*' to appear you type a '*' for every char read.

Alf
0
 
MarvelousAuthor Commented:
Thx, but how do i do it exactly? I'm new to c++.

just type in getch(); before the cin line or what?
0
 
SalteCommented:
no.

getch() is a low level C function (also available to C++) to read a character from keyboard and without waiting for return and without giving echo.

So you use it instead of cin.

if you still want to use iostream to read the password you can use a stringstream but it really isn't necessary:

std::string read_password()
{
   std::string pw;
   int k;

   while ((k = getch()) != '\n') {
      if (k < 255) {
         cout << '*' << flush; // echo the '*'
         pw.push_back(char(k));
      } else {
         cout << '\a' << flush; // echo a BEEP.
      }
   }
   return pw;
}

A few notes is important here.

It is a couple of years since I used the getch() function myself so I am not 100% sure of the details of it, in the code above I assume the following:

1. getch() pauses your program until the user hit a key (any key, not just return) and return with the key pressed.

2. If the key is an 'ascii key' or 'extended ascii' key such as 'A' or 'x' or some such the return is a value in the range 0..255 holding the character pressed.

3. If the key is an F1..F12 key or ALT+M or whatever code the return value is a value in the range above 255.

It is quite possible I am wrong in some of these assumptions and if I am you need to adjust the code accordingly. It is possible the return code also contain a bit mask indicating if CTRL, ALT or some such was pressed simultaniously with the character and if so you must check those bits as well, so if CTRL and C was pressed the code is really CTRL C or '\003' and not 'C' etc.

Check the documentation for getch() typing getch() in your IDE (if you have one) and pressing F1 (help key) you should get documentation for the function up. If you're not using an IDE you probably have the documentation available through some other means.

Alf
0
 
snabberCommented:
What library is getch in?
0
 
SalteCommented:
It's in the C library
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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