Invisible input for passwords? [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Read a password from std::cin
I want to cin>>input but when they input something i don't want it to be visible to them. Like when you use passwd in unix to change your password where it doesn't show what you typed. i hope it is clear what i am asking. Thank you in advance.

From C++ FAQ Lite
This is not a standard C++ feature — C++ doesn't even require your system to have a keyboard or a screen. That means every operating system and vendor does it somewhat differently.
Please read the documentation that came with your compiler for details on your particular installation.

cin isn't the way to do this, since the OS (usually) echoes standard input. What you need to do is handle the key events at the OS-level.

There is getpass(3) for Unix-like systems, which will do what you want.
However,
It's not portable (Unix systems only).
It's deprecated ("Present in SUSv2, but marked LEGACY. Removed in POSIX.1-2001.").
There is no way to do it with standard C or C++.

If what you want is something like what you do when you enter the password to login to a linux box, that behavior cannot be achieved in C or C++. You will have to use a platform specific library to do that. More specifically, console input in C and C++ is always echoed to the console.

Have it detect key presses. Think of it this way: When a user presses the 'A' key in a video game, the program runs code; don't think of it as receiving input. Think of it as detecting keystrokes and adding each keystroke to some data structure (e.g. an array).
I am familiar with the functionality in UNIX terminal that you are talking about and it doesn't allow you to backspace or anything. If you want that functionality, just do what I said above.
If you want to be able to backspace, although the user won't be able to see what they backspace, you can write code that removes the last element of the array when the user clicks backspace.
Also, everyone else is correct when they say that you will need to program this specific to the system you're running it on.
Lastly, this might not be secure if you are using this for a password and just throwing it into an array.

Related

Prompt user to fill in a template?

I've never been able to find anything about this in any language, but what I want to do seems rather simple to me.
I want to prompt the user for input, but have them fill in a sort of template. Let's use a simple DD-MM-YYYY date as an example.
[█ - - ]
█ is where the user writes.
As they write, the [ and - stay where they are, with the cursor jumping past them, and hitting enter submits.
I'm not expecting anyone to write all of this for me, but cin and cout seem to be too crude a tools to do this, and so I'm wondering where I should even start, if not there.
Is there something that already does this, or at least something that gives me more control of the cursor to allow me to write it myself?
I do not want to be drawing a full-on TUI with windows and boarders and colors, so ncurses would just get in the way. I just want a single-line text field that returns the input.
EDIT: The most promising non-ncruses alternative for a helpful library seems to be Terminal.
Generally what you want is a text-based GUI library, such as ncurses. Doing console work is platform-specific, and every system has its own console API to do this. If you want to implement this yourself, you would have to examine what options does your target operating system give you in terms of console API, and build a custom solution based on that.

C++ Console only show input when asking for it

I have made some simpel application which uses alphabetic keys to navigate (WASD).
The problem is whenever I use alpabhetic keys it gets displayed on the console.
I usually want the input to display only when specifically asking for it via std::cin, but else it disturbs. I could use arrow keys, but it was interesting how to solve it with alphabetic.
It depends on your operating System. So, I think that you have to use OS functions to set your console's parameters.

Password in c++ | Deleting a character after printing it

When we type password in websites ,we see * or . instead of characters.How can this be made possible with c++.Suppose I am trying to input password through stdin and at the same time I am storing it in a char array and deleting the character input by the user and outputing * instead.I think deleting the character is the trick here.But how do we delete a character after having printed it via stdout.
Read the key with _getch() (header <conio.h>), save/process it, and print '*' character instead of it. <conio.h> is available only on Windows afaik, though.
It depends upon the operating system and relevant libraries (the C++ standard does not define anything related to that).
On Linux, you might use ncurses or readline libraries (on the terminal). And if coding a GUI application, your toolkit (eg Qt) provides a relevant widget.
On this page C++ password in console you can find the answer. They use conio.h library, but in the middle of this page there is a better solution for Windows.

how to process arrow, pageUp, pageDown keys in C or C++ on linux

How do I get the key codes so I can process arrow, pageUp, pageDown, etc, keys using simple C or C++?
I can get the regular keys, I do not know how to get these special keys.
Ncurses should be able to handle that. There is lots of tutorials out there
Linux based systems follow the UNIX tradition, in the sense that those keys are special and their values depend on the terminal settings.
This is so, because in the old days each UNIX system had a complete different type of keyboard. As such it is somehow complex to be able to write generic code to handle those special keys.
The best way is to make use of a terminal handling library, like curses or its successor ncurses.
Here you can get a nice introduction about keyboard usage,
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html

(C++) Can you add a direct link into a C++ program without a GUI?

I was wondering if you can add a link to a website in a C++ program running in the CMD Prompt type window (No GUI)
If it's possible can some one please give me a few examples?
You mean output text in the command prompt that the user can then click on? No, not unless the terminal supported it. Linux terminals usually autolink text that matches a URL pattern, so you could just printf("http://stackoverflow.com/\n"); and it would be clickable, but that's up to the terminal, not your program
When you write 'direct link' it is not clear if you mean clickable text or a means to open a url. At any rate, command line programs usually respond to command line parameters. Your program could open a url in the default browser in response to a command line flag. On Windows, you could call ShellExecute - on other systems, system might be appropriate.
It depends. In Windows, for example, yes, it's entirely possible, though somewhat non-trivial. You can receive mouse events via ReadConsoleInput, so in theory it's a fairly straightforward matter of reading the input event, and if it's a mouse click over the area you've defined as a link, you direct the user to the link as you see fit -- if you want to display the web site in text mode, that's possible (though, again, distinctly non-trivial). If you want to start up the user's normal web browser, that's a lot simpler (normally just ShellExecute the URL).
In reality, the details get a bit ugly. You have to enable mouse input for it to work at all. ReadConsoleInput gives you an INPUT_RECORD, which is a union of a number of different input record types, one of which is a mouse input record. By the time you get to react to a mouse click, your code is nested fairly deeply. None of it is unmanageable by any means, but unless you already have a fair amount of experience with Windows console programming, it might easily take most of a day (maybe even a bit more) before you have it working, rather than the hour or two you'd initially guess.
That, of course, is strictly for Windows -- if you ever want to port the code to another system, I'd guess there's a pretty good chance you'd be looking at a complete rewrite. For GUIs there are a fair number of cross-platform libraries, but text mode mouse operations aren't nearly so well supported.