Thread: Converting words to *'s

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Converting words to *'s

    Garanteed everyone here has seen this done. Where text entered is turned into *'s. Like when you signed up for this forum, as you entered your password.

    Does anyone know how this can be done? I am just experimenting with small programs figuring little things out. This seems like something that could be useful later, and will help me out now.

    Thanks for any responses.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Here's something. I'm sure there is (was) something in the FAQ about this, but I can't find it, so you get an example.

    Code:
    #include <iostream>
    #include <string>
    
    #include <windows.h> // I think. May not be needed, but I think VK_RETURN's there
    #include <conio.h> // doesn't make most people happy - unstandard
    
    
    std::string StarString ( void )
    {
    	std::string Password;
    	char ch;
    
    	while ( (ch=getch()) && ch!=VK_RETURN )
    	{
    		Password += ch;
    		
    		std::cout<< '*';
    	}
    
    	return Password;
    }
    
    int main( void )
    {
    	std::string pword = StarString(); // outputs stars ****
    	std::cout<< pword; // outputs what you typed
    
    	return 0;
    }
    NOTE: This is very basic! You'll have to (if you want it to be accurate), make it deal with backspaces and whatnot. I'll leave that to you.
    Last edited by twomers; 10-18-2006 at 04:23 PM.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Here's the FAQ twomers was looking for.

    The problem is: ANSI/ISO Standard C++ does not allow your program to accept input 'till you hit ENTER.

    Most operating systems do allow your program accept keyboard input without displaying it, and they usually allow your program to "see" the keystrokes before you hit ENTER. And, most C++ compilers also have a way of doing this.

    Just be aware that <conio.h> and getch() are common, but non-standard. There is nothing wrong with using non-standard libraries and functions... Most real-world programs do!

    It is important that you know when you are going outside the standard, and it's a good idea to document the fact with comments, just in case somebody (maybe you) wants to re-compile the program with a different compiler and/or for another platform.

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Just a little note, twomers, you should check for VK_BACKSPACE and if it is pressed you should pop off the last character of Password and std::cout << "\b \b".
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's something like that here, although it's written in C: http://board.theprogrammingsite.com/viewtopic.php?t=113
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> Just a little note, [...]

    I know this*. I have the equivalent function done out completely, but shaved most of the necessary stuff off so the OP could learn through improving the function. \b wrecks my head! Worst sound in the world


    * "this is very basic! You'll have to (if you want it to be accurate), make it deal with backspaces and whatnot. I'll leave that to you."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting digits into words
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 11-02-2008, 06:10 AM
  2. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  3. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM