Using your last:
void getpassword(char password[], int maxlength)
...and
char mygetch(void)
...from above along with the following main():
Code:
#include <stdio.h>
#include <windows.h>
#define KEYLENGTH 15
void PAUSE(void){int c; while( (c=getchar()) != '\n' && c != EOF);}
void getpassword(char password[], int maxlength);
char mygetch(void);
int main(void)
{
int maxlength = 15;
char password[KEYLENGTH + 1] = {0};
void PAUSE(void){int c; while( (c=getchar()) != '\n' && c != EOF);}
getpassword(password, maxlength);
printf("\n\npassword= %s \n", password);
getchar();
return 0;
}
I get the following:
Code:
/* I type letters a thru q: */
Please input password (max. 15 characters): ***************
password= abcdefghijklmno
/*******************************************************/
/* I type digits 1 thru 0 twice (20 characters)
Please input password (max. 15 characters): ***************
password= 123456789012345
The cursor moves back after 15 chars, but nothing is erased.
(why are you doing that anyhow....)
Also, in getpassword() :
'i' may never reach maxlength because of all the decrementing.
I get same behavior with or without fflush(stdout):
program overruns the getchar() at end of main because of (stdin) leftovers.
Try that PAUSE() function up there instead of fflush() at end of your getpassword():
Code:
...snip...
/* fflush(stdout); */
}
password[i] = '\0';
PAUSE();
} /* end of getpassword() */
So... What's the problem?
I am on a '98 msdos console. Perhaps XP or Vista consoles behave differently.