-
Function Problem
Code:
void passgets(char pword[],int maxlength){
int counter=0;
char ch;
while((ch = getch())!='\r'&&counter<maxlength-1){
if(ch =='\b'&&counter>0){
printf("\b \b");
counter--;
}
else{
pword[counter++]=ch;
putchar('*');
}
}
}
I have put that together from combining code from a few of the previous threads about masking passwords. The problem is that when I test it it ends up with a bunch of random characters at the end. How do I fix this?
~Sven
-
Null terminate the string.
-
You need to append the \0 yourself.
-
OK I tried adding
Code:
strcat(pword,'\0');
to the end of the function but then once it hits that the progam crashes. How else should I do it?
~Sven
-
pword[counter] = '\0';
Easy innit?
-
Code:
strcat(pword,'\0');
1) strcat needs the first to be null terminated in the first place
2) It expects a null terminated character pointer as the second
-
Thanks Salem I wasn't thinking that it was that simple.
~Sven