Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2005, 5:53 PM   #1
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
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?
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 6:09 PM   #2
noliviawalton
Newbie
 
Join Date: Jun 2005
Location: Washington,DC
Posts: 8
Rep Power: 0 noliviawalton is on a distinguished road
Send a message via AIM to noliviawalton Send a message via Yahoo to noliviawalton
What software have you been doing this programmer in? Also why do you have so many braces in the program?
noliviawalton is offline   Reply With Quote
Old Jun 28th, 2005, 6:16 PM   #3
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
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
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 6:27 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Print this: "\b \b". That'll move the cursor back one, print a space (thereby erasing whatever's there), and move it back again.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 28th, 2005, 6:31 PM   #5
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
if this is a unix box try:
#include <unistd.h>
int main()
{
     char *pass=NULL;
     pass=getpass("Enter password");
     return 0;
}
jim mcnamara is offline   Reply With Quote
Old Jun 28th, 2005, 6:38 PM   #6
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
printing "\b" only erases the char gotten by getch (the unechoed char).
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 7:05 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 28th, 2005, 9:17 PM   #8
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
ahh
got it, just wasn't printing the space to erase it. Thanks.
Solved.
Navid is offline   Reply With Quote
Old Jun 28th, 2005, 11:07 PM   #9
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by navnav

if(count > 10)
{
   ..
   ..
i think it should be

if(count>7)
{
  ..
  ..
InfoGeek is offline   Reply With Quote
Old Jun 29th, 2005, 8:19 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC