☆ C/C++ 模拟输入密码(控制台暗文输出)

版权声明:转载请注明出处 https://blog.csdn.net/qq_42292831/article/details/83145102

偶然间遇到了getch()函数,突然间真的有点茫然~

于是,一不做二不休,了解它的详细使用方法和它类似的函数的使用:

****************************************************************************************************************************************

一:getch()

(标题功能所使用的函数)

★直接附上代码: 

解析见下方~

#include <iostream>
#include <conio.h>     //使用getch() 
using namespace std;

int main()
{
	char c[20];
	int i = 0;
	cout << "Input your password: " ;
	while(true)
	{
		c[i] = getch();     //只能接收一个动作 
		//cout << c[i-1];
		if(c[i] =='\r')     //回车键表示\r\n 
		{
			break;
		}
		cout << "*" ;
		i++;
	}
	cout << "\n\nShow your password : " << c;
} 

★代码分析:

1:声明库函数conio.h才可以使用getch()函数;

:2:getch()函数输入时不显示在控制台上,为了测试方便可以在其后加上一个输出语句;

       而且getch()每次只能读取一个字符,像键盘上的回车键实际上是由两个功能字符组成的:\r(回车,回到该行开头) \n(换             行),这就需要在上述代码的while循环中加入对‘\r’的判断,而不是常用的‘\n’;

        

二:getchar()

getchar()的功能与getch()功能类似,

相比较getch()函数,getchar()多了两个字母,可以理解为多了回显的功能

当然该函数也只能读取一个字符

下面写个代码测试下:

#include <iostream>
using namespace std;

int main()
{
	char str;
	str = getchar();
	cout << str << endl;
    return 0;
} 

★当然getchar()比较常见的用法还是来判断输入是否用回车进行截断

#include <iostream>
using namespace std;

int main()
{
	char str;
	while((str = getchar())!='\n')
	{
		cout << "***" << endl;
	}
    return 0;
} 

三:gets()

get()函数有回显的效果,而且gets()函数可以保存输入的字符串(与上面的不同);

字符串中可以包含空格

之所以提到字符串中的空格问题,在使用scanf()函数输入的时候,如果遇到空格就会自动停止读取输入内容,

而使用该函数可以很好地解决这个问题;

当然也可以使用上面的两个函数来进行循环输入,将函数得到的每个字符进行拼接进数组中即可。

#include <iostream>
using namespace std;

int main()
{
	char str[20];
	gets(str);
	puts(str);
	return 0;
} 

★在这里还要提一点就是gets()可以携带参数,需要赋值的变量可以直接放入括号内即可;

* 最后附上自己调试时候使用的代码(参考使用):

#include <iostream>
#include<iomanip>
using namespace std;

struct stu        //typedef不能和结构体数组一起使用
{
	char name[20];
	float score;
}stu_info[3];

void Init_info()    //初始化的姓名内可以带空格 
{
	int i;
	char c,d[20];
	cout << "Please enter student's infomation: " << endl;
	/* 
	for(int j=0;j<3;j++)
	{	
		cout << "****" << endl;
		int i=0;
		cout << "The " << j+1 << " info: " << endl;
		while((c = getchar())!='\n')    //这里不能用getch()-->>这里的名称中可以有空格
		{
			//d[i] = c;
			stu_info[j].name[i]=c;
			i++;
		}
		stu_info[j].name[i] = '\0';
		scanf("%f",&(stu_info[j].score));
		cout << stu_info[j].score << endl;    //cout不会像printf那样输出%f的时候输出一大堆0 
		getchar();    //吸收回车 
	}
	*/
	
	/*
	for(i=0;i<3;i++)
	{
		gets(stu_info[i].name); 
		scanf("%f",&(stu_info[i].score));
		cout << stu_info[i].name << endl;
		cout << stu_info[i].score << endl;
		getchar();
	}
	*/
	
	/*这个和上面的是一样的 
	for(i=0;i<3;i++)
	{
		//gets(stu_info[i].name);    //getchar()实现单个字符的输入,gets()实现字符串的输入,均有回显
		//c = getch();               //getche()/getch()输入后不需要敲回车,直接进行下面语句,没有回显
		//cout << c;
		//scanf("%s %d",stu_info[i].name,&(stu_info[i].score));
		//scanf("%s %d",stu_info[i].name,stu_info[i].score);
		scanf("%s",stu_info[i].name);
		scanf("%d",&(stu_info[i].score));
		cout << stu_info[i].name << endl;
		cout << stu_info[i].score << endl;
	}
	*/
}

int main()
{
	Init_info();
	return 0;
}

****************************************************************************************************************************************

             最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。
 

****************************************************************************************************************************************

猜你喜欢

转载自blog.csdn.net/qq_42292831/article/details/83145102
今日推荐