在不同编译环境中如何实现密码的隐藏

来源:互联网 发布:js image onload 编辑:程序博客网 时间:2024/06/10 08:38

为了输入密码后打印出*,即把密码隐藏起来,可以用下面的方法
在dev-c++,vc下使用密码,可以用下面的方法
使用#include <conio.h>中的getch()函数
#include <iostream>
#include <conio.h>
using namespace std;
main()
{
int i=0;
char str[10]="\0";
cout<<"Please input password:";
while (1)
{
str[i]= getch();
if (str[i++]=='\r')
break;
cout<<"*";
}
cout<<"\nYour password is:"<<str<<endl;

getchar();
}
为了能在linux中也能实现上述方法,可以用system("stty -echo");
system("stty -echo");是不显示输入内容的意思
#include<iostream>
using namespace std; 
int main
     {
        int i=0;
char str[10]="\0";
cout<<"Please input password:";
while (i<7)
{       system("stty -echo");
str[i]= getchar();
      system("stty -echo");
if (str[i++]=='\n')
break;
cout<<"*";
}
cout<<str;
         
     }