getch函数简介:
getch函数是一个无回显的输入函数,只从键盘读取一个字符(只允许按一下),可以用于:
输入密码
任意键继续
...(作者脑补不出来)
getch函数必须要的头文件:
conio.h
getch函数没有参数
返回值是char类型
使用方法(C++代码):
#include<bits/stdc++.h> //可以换成其他的
#include<conio.h> //很重要
using namespace std;
int main(){
char a=getch();
cout<<a;
return 0;
}
那么,既然知道了getch函数的使用方法,我们就来实际运用吧:
C++密码程序
void clc() {
cout<<char(8)<<' '<<char(8);
}
bool ___password(string _password) {
bool ok=true,backspace=false;
char a[10001];
int len=0;
while(1) {
a[len]=getch();
if(a[len]==8) {
if(len==0)continue;
len--;
clc();
ok=true;
continue;
}
if(a[len]==13) {
len--;
break;
}
if(_password[len]!=a[len])
ok=false;
cout<<"*";
len++;
}
return ok;
}