Backspace not working

Hey guys. I'm trying to take a login program to the next level but by making the keys *'s. Heres the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
        string password;
	int temp;
	do
	{
		temp = _getch();
		if (temp != 0 && temp != 13 && temp != 8)
		{
			password += char(temp);
			cout << "*";
		}
		else if (temp == 8)
		{
			//I want to erase a "*" and
			//backspace the last key entered into password
		}
	}
	while (temp != 13);
	cout << endl << input << endl;
	system("pause");
	return 0;
}


I can't figure out how to make it to when I press backspace it deletes a * and the last key inputed. Your help is most appreciated :D
You can try cout << "\b" << " " << "\b"; which should (haven't tested it) move the pointer back one, overwrite it with a space, then move it back again (although if they are on the end of a line or something, it won't work).
use getc, this will not output the input on the screen and then you can from your side print a "*" for each input. simple.. :)

but then you have to handle backspaces, becuase while typing the password if the user types a backspace, then you have to remove the last character entered from your buffer.
The backspace part works with cout << "\b" << " " << "\b"; but It doesn't erase the input for password. For example:
****** //I typed in abcdef then I hit backspace twice...
**** //Then I hit enter and it says
abcdef // Instead of abcd

How do I get it to erase both the * and the data?
Ah, you would have to use string::erase(). Just search, it's pretty straightforward to use.
Last edited on
Topic archived. No new replies allowed.