-
password masking
I designed a program in C that I would like to use passwords for, but I can't seem to make a function for masking the passwords. I figure that it has something to do with kbhit(); in <conio.h>, but I can't figure out how to properly use that function in this case. Anybody have an answer to this problem?
-
kbhit() shouldn't be necessary but you'd probably want to write your own fgets() using getch().
Read a character, then gotoxy(wherex()-1, wherey()) and putchar('*') and repeat until a newline is found.
-
That would depend on your OS and compiler.
Particularly, since you mention conio.h, are you programming for real DOS or merely using a DOS compiler in a win32 console, or perhaps even a proper 32 bit compiler in a win32 console.
-
OS and Compiler
I'm using Digital Mars C/C++ compiler for Windows, and I'm on Windows XP.
-
ok...
heres some code I came up with for a function for this crap. Maybe you can tell me what im doing wrong:
Code:
#include <stdio.h>
#include <string.h>
#inlcude <conio.h>
void passgets(char password[], int maxlength)
{
int n, a = 0;
char s[1];
for (n = getch(); a < maxlength; ++a)
{
itoa(n, s);
if (s[1] == '\n' || s[1] == '\r')
{
a = maxlength;
}
strcat(password, s);
printf("*");
s[1] = '\0';
}
}
can anyone tell me what the heck is wrong with this code?
-
Going out of bonds and unnecessarily converting an int to a char array.
You can compare n with '\n' and '\r' without problem.
-
well...
how do i do that? I dont really understand what you mean. Since getch() produces an int value, how am i supposed to get the characters in the password to use or display them? all ill have is a bunch of numbers!
-
also...
whats going out of bounds? one of the characters, i assume. what do i do to avoid the use of itoa and things like that?
-
You made an array of 1 char. The first and also last element of it is s[0] but in the code you used s[1].
The fix would be to change s to an array of 2 characters and change the if-statement comparson to use s[0] and have the s[1] = '\0'; come before the strcat(). And itoa() doesn't make much sense either. You probably just wanted s[0] = n;
Try this:
Code:
#include <stdio.h>
#include <string.h>
#inlcude <conio.h>
void passgets(char password[], int maxlength)
{
int n, a;
for (a = 0; a < maxlength; ++a)
{
n = getch();
if (n == '\n' || n == '\r')
break;
password[a] = n;
putchar('*');
}
password[a] = '\0';
}
[edit] No wait, getch() didn't output the character so it's unnecessary.
-
thanks
well, you definitely showed me my error, but what is gotoxy()? is it a function defined in <conio.h>? I've never used it before.
-
Yes, it's a conio.h function. It allows you to move around in the console screen. wherex() and wherey() are also conio functions and will return the position the console cursor is for those axises.
Also, look at the edit. You don't have to use it.
-
ok
well, thats a big help. thanks a ton.
-
what s wrong now?
Code:
#include <stdio.h>
#include <string.h>
#inlcude <conio.h>
void passgets(char password[], int maxlength)
{
int n, a;
for (a = 0; a < maxlength; ++a)
{
n = getch();
if (n == '\n' || n == '\r')
break;
password[a] = n;
putchar('*');
}
password[a] = '\0';
}
this compiled with no errors, but now, whenever I type the password, no characters are displayed until you press enter. what do i do about that?
-
i think i got it:
Code:
#include <stdio.h>
#include <string.h>
#inlcude <conio.h>
void passgets(char password[], int maxlength)
{
int n, a;
for (a = 0; a < maxlength; ++a)
{
n = getch();
if (n == '\n' || n == '\r')
break;
password[a] = n;
putchar('*');
}
password[a] = '\0';
}
password[a] = n;
putchar('*');
this was being included in the if statement.
-
nope
well, that didnt work. any other ideas, because this is really starting to make me mad....