Thread: Cin Masking

  1. #1
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23

    Cin Masking

    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

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    If I understand your question correctly, then yes.
    Consider the following code
    Code:
    #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;
    }
    It will rotate your input by 15

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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

  6. #6
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    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

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  8. #8
    Autobots Transform! Dunners's Avatar
    Join Date
    Feb 2005
    Posts
    23
    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

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. DIB masking
    By SAMSAM in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2003, 10:22 AM