?
Solved

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

Posted on 2003-03-31
10
Medium Priority
?
856 Views
Last Modified: 2010-05-18
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
Comment
Question by:Marvelous
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8239001
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
 
LVL 12

Expert Comment

by:Salte
ID: 8239194
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8239230
Borland uses a PasswordChar property on the TEditBox object.

Kdo
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:Francoz
ID: 8239791
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
 

Author Comment

by:Marvelous
ID: 8243292
Sorry if I mislead you guys, but I should have told you guys that I wanted to do this in a console application.
0
 
LVL 12

Accepted Solution

by:
Salte earned 40 total points
ID: 8243987
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
 

Author Comment

by:Marvelous
ID: 8244001
Thx, but how do i do it exactly? I'm new to c++.

just type in getch(); before the cin line or what?
0
 
LVL 12

Expert Comment

by:Salte
ID: 8244598
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
 

Expert Comment

by:snabber
ID: 9073310
What library is getch in?
0
 
LVL 12

Expert Comment

by:Salte
ID: 9078136
It's in the C library
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question