C++输入密码掩码
在windows下
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> using namespace std; #define length 6 int main() { char* password = NULL; password = (char*)malloc(sizeof(char)*(length + 1)); char* p = NULL; int count = 0; p = password; printf("Please input a six digit password:\n"); for (count = 0; count < length; count++) { *p = getch(); fflush(stdin); p++; } password[6] = {'\0'}; printf("Do you want to display the password?\ntrue(1) false(2)\n"); int select = 0; scanf("%d", &select); switch (select) { case 1: { printf("password is %s\n", password); break; } case 2: break; } free(password); system("pause"); return 0; }
在Linux下是没有conio.h这个头文件的所以
#include<iostream> using namespace std; #define length 4 int main() { char* password; password= new char[length + 1]; char* p = NULL; int count = 0; cout << "Input password : "<<endl; p = password; count = 0; system("stty -echo"); for (count = 0; count < length; count++) { *p = getchar(); fflush(stdin); p++; } cout<<endl; password[count] = '\0'; cout << "The password is " << password <<endl; delete password; system("stty echo"); return 0; }

浙公网安备 33010602011771号