Results 1 to 6 of 6
  1. #1
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool

    Char replace stuff

    Hi all,

    I'm developing a console app. What I want to do is to replace all the inputs from the keyboard (while using cin) with another char, for example '*' instead of the real pressed ones.

    In other words I want to have the same effect as when you are typing your password.


    Anyone know how to do it?

    Thanks in advance
    Last edited by Sixx93; 06-25-2013 at 05:34 AM.

  2. #2
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    Lol, it tooks me like 3 hours to make it works, but I finally got it! (sign it as Solved pls)

    If anyone is interested, here's the code (it may not be the best one, but at least it works well):



    Code:
    char * get_psw()
    {
    string strPass = "";
    char c = 0;
    int index = 0;
    
    while (c != 13)
    {
    	c = (char)_getch();
    	
    	
    	if(c == 13)
    		break;
    
    	strPass += c;
    	if(c != 8)
    	{
    		cout << "*";
    	}
    	else
    		cout<<"\b"<<" "<<"\b";
    	
    }
    
    char * psw = new char[k_lenght]; 
    
    unsigned int j = 0, num_bckspc = 0, num_bckspc_done = 0;
    bool flag = false;
    
    for(unsigned int cnt = 0; cnt<strPass.length(); cnt++)
    	{
    		if(strPass[cnt] == '\b')
    		{
    			num_bckspc++;
    		}
    	}
    
    while( j<strPass.length())
    	{
    
    		if(strPass[j] == '\b' && j>0)
    			{
    				for(unsigned int t = j; t<strPass.length()+1; t++)
    					strPass[t-1] = strPass[t];
    				flag = true;
    				num_bckspc_done++;
    		}
    
    		if(strPass[j] == '\b' && j==0)
    			{
    				for(unsigned int t = j; t<strPass.length(); t++)
    					strPass[t] = strPass[t+1];
    				flag = true;
    		}
    
    		if(num_bckspc_done == num_bckspc && num_bckspc != 0)
    		{
    			unsigned int i = 0;
    			bool flag1 = false;
    			while(i<strPass.length())
    			{
    				if(strPass[i] == '\b' && j>0)
    				{
    					for(unsigned int t = i; t<strPass.length(); t++)
    						strPass[t] = strPass[t+1];
    					flag1 = true;
    					num_bckspc_done++;
    				}
    
    				i++;
    
    				if(flag1)
    				{
    					i=0;
    					flag1 = false;
    				}
    
    			}
    		}
    
    		
    		j++;
    		if(flag)
    			{
    				j=0;
    				flag = false;
    		}
    }
    unsigned int f;
    for( f=0; f<strPass.length(); f++)
    	{
    		psw[f] = strPass[f];
    		
    }
    psw[f] = '\0';
    
    return psw;
    }

  3. #3
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,993
    Reputation
    374
    Thanks
    8,808
    My Mood
    Twisted
    Quote Originally Posted by Sixx93 View Post
    tooks me like 3 hours
    took you 3hours to figure out how google works and then copy the code provided on the first link? wow.

  4. #4
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    Quote Originally Posted by Biesi View Post


    took you 3hours to figure out how google works and then copy the code provided on the first link? wow.
    hahahah you mean this?
    Code:
    while (c != 13)
    {
    	c = (char)_getch();
    	
    	
    	if(c == 13)
    		break;
    
    	strPass += c;
    	if(c != 8)
    	{
    		cout << "*";
    	}
    	else
    		cout<<"\b"<<" "<<"\b";
    	
    }
    the rest has been written by itself?

    However, it's not the same that you can find on the net but it has benn modified by me.


    This is what I have found on the net (just the first piece of code): https://www.cplusplus.com/forum/general/3570/

    draw your own conclusions
    Last edited by Sixx93; 06-26-2013 at 06:10 AM.

  5. #5
    dimast's Avatar
    Join Date
    Aug 2011
    Gender
    male
    Posts
    6
    Reputation
    10
    Thanks
    1
    I see you are trying to detect and process backspace characters. You can do it without these horrible loops, just try to process chars in 'live' like it shown in cplusplus. btw: its a bad way to use std string and char * simultaneously. if you need to store your strings somewhere just choose one option.

  6. #6
    Kenshin13's Avatar
    Join Date
    May 2011
    Gender
    male
    Location
    Cloud 9
    Posts
    3,470
    Reputation
    564
    Thanks
    6,168
    My Mood
    Psychedelic
    @Horror, this is solved. Keeping it open = Spam.

Similar Threads

  1. Selling Account 3 Char Slots | 7 Vaults | Plane + other stuff | Red Star
    By sparzinrotmg in forum Realm of the Mad God Selling / Trading / Buying
    Replies: 7
    Last Post: 09-21-2013, 07:03 AM
  2. Selling Account 3 Char Slots | 7 Vaults | Plane + other stuff | Red Star
    By sparzinrotmg in forum Realm of the Mad God Selling / Trading / Buying
    Replies: 0
    Last Post: 06-07-2013, 08:14 AM
  3. [EU] Pricecheck for 4 High lvl Chars with decent stuff
    By Symbian in forum Vindictus Selling / Trading / Buying
    Replies: 0
    Last Post: 03-23-2012, 03:11 PM
  4. [Discussion] Good Stuff Lite Replacement?
    By HoLyNeSs in forum Vindictus Discussions
    Replies: 5
    Last Post: 06-03-2011, 03:57 PM
  5. Stupid People Doing Stupid Stuff!
    By Dave84311 in forum General
    Replies: 9
    Last Post: 08-22-2007, 11:54 PM