#include<iostream>
#include<stdio.h>
#include<conio.h>
usingnamespace std;
void main(){
char ch;
char password[50];
int i=0;
//13 is ascii value for "Enter" key
printf("Enter Password: ");
while((ch=_getch())!=13)
{
printf("*");
password[i] = ch;
i++;
}
password[i] = '\0';
//here pass will contain your password.
cout <<"Your password: "<<password<<"";
It worked all right but if I press <- Backspace button, example I type Edward and then press <- Backspace button, I suspect output is "Your password: Edwar, but the output is Edward, and when type E output is * , type d = *, type w = *, type a = *, type r = *, type d output is *, then total output is ****** (6 stars based 6 words on " Edward)but when I press <- Backspace button I suspect the output is *****(5 stars based on Edwar), but that did not happen, the ouput is ******* Wew... why can this be ? maybe you've got the right way ... please help me
See, the backspace is treated as a character (ASCII (dec) = 8), so you'll need to create some sort of way of detecting the backspace and moving your i back one step and reseting the data at that position, as well as making it look as if one star was deleted from the console. The console's readout wasn't meant to be backspaceable (if you know what I mean), so I'd recommend just clearing the screen (preferably using a large number of newlines) and printing out the appropriate number of stars to make it look as if the backspace happened.
Hmm... hmm, that means I need to change the letter array [] with null,
example, if d in array[5] and then when I press <- Backspace I need to change array[5] = "d" to array[5] = ' \0' Null ?? Ohh I am trying something new so I mix that heheh
I just make the program, I have successfully make program know when I press backspace, computer delete the password letter, but I don't know how to delete output,
print ***** press backspace = result = ****
This can be done if I use system("cls");, but do you have a better way?
Not a good solution but you ask for it so.. here's quick and dirty code. Also there is a problem when the user prints too long password. You might want to some checking with that.