Thread: How to echo text as asterisks

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    11

    Question How to echo text as asterisks

    Just wondering how to echo user input (e.g. password) as asterisks?

    Any suggestions plz?

    Thom
    Before you criticise a man, walk a mile in his shoes. Then, when you criticise him, you're a mile away. And you have his shoes.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ive just answered this on the windows board. I hope you meant for windows....
    Dont cross post... pick the forum that best will solve the problem and stick with it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    Yeah sorry about that, I didn't actually mean to post it in the windows section.

    I meant in DOS, can it be done?

    I'm using Vis C++ 6, Win32 Console Application.

    Cheers.
    Before you criticise a man, walk a mile in his shoes. Then, when you criticise him, you're a mile away. And you have his shoes.

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can use getch() (in conio.h) to read characters without echo-ing them on the screen and then use cout<< or printf() to output an asterisk for every character read.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    As an example:

    Code:
    #include <iostream>
    #include <conio.h>
    
    const char ENTER = 13;
    
    int main()
    {
        char* str;
        char key;
        int i = 0;
        while(1)
        {
            key = getch();
            if(key == ENTER)
                break;
    
            str[i] = key;
            std::cout << '*';
            i++;
        }
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char* str;
    Uninitialised pointer alert!!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    Hehe I thought that, and I'm a newbeh!

    I'll give it a try though, thanks. Isn't there a simpler way?
    Before you criticise a man, walk a mile in his shoes. Then, when you criticise him, you're a mile away. And you have his shoes.

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Write some code to start if off. It might be worthwhile to scan their input twice using different methods and compare the results to each other. Than if they match, compare it to a password key.
    I compile code with:
    Visual Studio.NET beta2

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    Erm... sorry I don't follow.

    Also, what is that .NET thing all about?

  10. #10
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    	char buffer[12];
    	int i, ch;
    
    	printf("Enter a password (10 alphanumeric sequenc max):\n" );
    	printf("Password: ");
    	
    	/* Read password but no larger than 10 characters */
    	for( i = 0; i < 10 ; i++ )
    	{
    		//get character
    		ch = _getch();
    		//if user enters newline than quit
    		if(ch == '\r') break;
    		//if user enters backspace
    		if(ch == '\b')
    		{
    			//if there are no preceeding letters
    			if(i == 0) 
    			{
    				--i;
    				continue;
    			}else //if there are preceeding letters
    			{
    				printf("\b%c\b",' ');
    				i-=2;
    			}
    		}else //no backspace was entered
    		{
    			putc('*',stdout);
    			buffer[i] = (char)ch;
    	
    		}
    	}
    	/* Terminate string with null character: */
    	buffer[i] = '\0';
    	printf( "\n%s", buffer);
    
    	return 0;
    }
    A little bit tricky. Works on Microsoft but not on *NIX
    If you wanted is cross platform than this should do it, chang this line:
    Code:
    if(ch == '\r') break;
    To this:
    Code:
    if(ch == '\r' || ch == '\n') break;
    Last edited by Witch_King; 09-02-2001 at 10:46 AM.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Replies: 1
    Last Post: 09-01-2001, 10:33 AM