Hi
Is it possible to mask input gained using cin so that when a user type a different character (e.g. *) is displayed rather than the character they typed?
Cheers
Hi
Is it possible to mask input gained using cin so that when a user type a different character (e.g. *) is displayed rather than the character they typed?
Cheers
"Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
- Stewie Griffin
If I understand your question correctly, then yes.
Consider the following code
It will rotate your input by 15Code:#include <iostream> using namespace std; int main() { int i = 0; char string[7]; cout << "Please input a short string of characters: "; cin >> string; while (string[i] != NULL) { string[i] += 15; cout << string[i]; i++; } cout << "\n"; return 0; }
If I understand you correctly, as the user types input, you want a character they type to appear as a * rather than the key they actually typed. As far as I know, a C++ console program doesn't know what the user has done until they hit return. However, in other languages like javascript, you can detect keypresses and change their output, so I would think there would be something similar if your program was a windows program.
The answer is yes and no It really depends on your OS and your compiler. What you have to do is to take it out of "cooked" mode and put it into "raw" mode. You can not do this with cin. But it can be done with the buffer cin points to. What you are looking to do can be accomplished with getch() if your OS/Compiler supports it.
I don't believe there's a standard way to do this. You can read here for some possible ways to get input without the user pressing enter.
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
The basic thing is the user has to enter a "passcode" of 4 numbers which are stored in an integer when the user presses enter. I want a * to appear on screen when they press the numbers rather then showing the actual numbers on the console screen. Is this possible?
"Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
- Stewie Griffin
Again it depends on your OS and Compiler. If your compiler has getch() then you can read one character at a time into an array. Then each time you output a '*'. However you must then take care of things like the user hitting the backspace or hitting function keys or what not. One the user hit's return you then take your character array and convert it to an integer and compare it.
cheers, I'll give that a try. I think the compiler I have has getch() so everything should be grand. About 30 more minutes work and I should be able to get some kip, 17 hours programming (with a couple of short breaks for food and banging of head against wall) isn't too bad
"Yes, I rather like this God fellow. He's very theatrical, you know, a pestilence here, a plague there. Omnipotence. Gotta get me some of that."
- Stewie Griffin
As was mentioned before, you're looking for raw input rather than cooked. Also try searching for non-canonical (raw) vs canonical (cooked) input. If you're programming for windows/dos it's rather easy. For *nix systems it get's a bit more complicated.