为了实现输入一个密码不回显,使用了conio.h头文件里的_getch()函数,它能够不会回显输入的字符,但是当输入删除符和空格符会出现问题
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void) {
#define MAX_SIZE 30
char pwd[MAX_SIZE] = “\0”;
char ch = 0;
int index = 0;
while (1) {
ch = _getch();
if (ch == ‘\r’) {
pwd[index] = ‘\0’;
break;
} else if (ch == ‘\b’) {
printf(“\b \b”);
continue;
} else if(ch == ’ ‘) {
putchar(’ ');
pwd[index++] = ’ ';
continue;
}
pwd[index++] = ch;
printf(“*”);
}
printf(“\n”);
printf(“%s”, pwd);
getchar();
return 0;
}