利用C库的控制函数getch(),可以实现在console上输入密码时,不在屏幕上显示已输入的密码或用*号代替已输入的字符,就像linux那样。
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
char ch;
while((ch=getch())!=0x0d) cout<<"*";
return(0);
}
#include <stdio.h>
main()
{
char ch;
while ( (ch = getch()) != 13 )
{
printf("*");
}
}
c 有没有关注
如果想保存的话,还可以保存
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
char ch,str[100];
int i=0;
while((ch=getch())!=0x0d)
{
cout<<"*";
str[i ]=ch;
}
cout<<endl<<"The password is : "<<str<<endl;
return(0);
}
不用C库也可以,手工写成如下:
这样你想怎么控制就怎么控制,比如,用户输入一个字符时,在屏幕上显示一小段时间(只要在"cout<<fillchar;" 前调用_sleep( ntime)就可以实现,不过当输入的密码会发生自动换行时, 光标不能跑回上行进行替换字符,要实现就用win32 console api.
const SIZE = 32;
char c=' ';
char password[SIZE];
char fillchar = '*';
for(int i=0;i<SIZE; i )
{
cin>>c;
if ( c == '/r' ){
password[i]='/0';
break;
}
password[i]=c;
cout<<(char)8; // 输出backspace字符,删除已输入的字符显示
cout<<fillchar;
}
if(i>=SIZE)
throw {"too many input error "}; //可以自已定义错误类型。
cin