linux:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *args[])
{
// 调用getpass函数
// 函数的参数是提示信息
// 函数的返回值是用户输入的内容
char *password = getpass("Input your password : ");
// 输出用户输入的信息
printf("password = %s\n", password);
return 0;
}
window:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {
char input_pwd[50], ch;
char pwd[50] = "123456";
int index = 0 ;
int tryTime = 3;
while (tryTime--) {
index = 0;
printf("Input your password:");
while ((ch = _getch()) != '\r') {
if (ch == '\b' && index > 0) {
printf("\b \b"); //空格+\b 是为了覆盖掉原来的字符
index--;
}
else if (ch != '\b') {
input_pwd[index++] = ch;
}
}
input_pwd[index] = '\0';
printf("\n输入的密码为%s\t",input_pwd);
if (strcmp(input_pwd, pwd) == 0) {
printf("密码正确");
return 0;
}
else
printf("密码不正确,请重试\n");
}
printf("连续输入3次密码都不正确");
}