![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Password Program
This is a program where you input a password, and for each char it prints a *.
But when the user presses backspace, it erases the unechoed char that was inputed, but not the *. #include <stdio.h>
char pass[10];
int count=0;
int main(void)
{
printf("Enter new password (10 characters max):");
while ( (pass[count++]=getch()) != '\r')
{
if(count > 10)
{
for (count=0; count<10; count++)
{
pass[count] = '\0';
}
printf("\n\nYour password is too long, enter it again:");
count = 0;
continue;
}
else
{
if (pass[count-1] == '\b' && count==1)
{
count=0;
continue;
}
else
{
if (pass[count-1] == '\b')
{
printf("\b");
count -= 2;
}
else
{
printf("*");
}
}
}
}
puts("\n\nYour new password is:");puts(pass);
printf("\n");
system("pause");
return 0;
}So what should I do so it erases the * when the user presses backspace? |
|
|
|
|
|
#2 |
|
Newbie
|
What software have you been doing this programmer in? Also why do you have so many braces in the program?
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Well thanks for registering just to anwser me
![]() I'm using Dev-C++, the braces are for the ifs. A switch is usally a better way of using ifs but, meh |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Print this: "\b \b". That'll move the cursor back one, print a space (thereby erasing whatever's there), and move it back again.
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
if this is a unix box try:
#include <unistd.h>
int main()
{
char *pass=NULL;
pass=getpass("Enter password");
return 0;
} |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
printing "\b" only erases the char gotten by getch (the unechoed char).
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Do as Ooble suggests. Printing \b moves the cursor back to the previous character on the screen. Printing space overwrites it. Printing \b again repositions you to that character on the screen. You'll also need to discard the character you read when you see the backspace from the keyboard.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
ahh
got it, just wasn't printing the space to erase it. Thanks. Solved. |
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
if(count>7)
{
..
.. |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Why 7?
He says in the prompt that the password is 10 characters, maximum.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|