c++输入密码以星号代替

#include <iostream>
#include <string>//注意这里的头文件!
#include<conio.h>
using namespace std;

void main( void )
{
 cout<<"请输入密码: "<<endl;
 string password;
 int i=0;
 char ch;
 while ((ch=_getch())!=13)
 {
  password+=ch;//string对象重载了+=
  cout<<"*";
 }
 cout<<endl<<"输入完毕!您输入的是: "<<password<<endl;
}



带有退格键的密码输入

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char password[100];
int index=0;
while(1)
{
char ch;
ch=getch();
if(ch==8) //退格键
{
if(index!=0)
{
cout<<char(8)<<" "<<char(8);
index--;
}
}
else if(ch=='\r') //回车键
{
password[index]='\0';
cout<<endl;
break;
}
else
{
cout<<"*";
password[index++]=ch;
}
}
cout<<"password:"<<password<<endl;
return 0;
}


posted @ 2014-01-18 20:14  IT专业户  阅读(2704)  评论(0编辑  收藏  举报