SolvedChar replace stuff

Posts 1–6 of 6 · Page 1 of 1
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
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;
}
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.
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): http://www.cplusplus.com/forum/general/3570/

draw your own conclusions
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.
@Horror, this is solved. Keeping it open = Spam.
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?