1. 程式人生 > >☆ C/C++ 模擬輸入密碼(控制檯暗文輸出)

☆ C/C++ 模擬輸入密碼(控制檯暗文輸出)

偶然間遇到了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;
}

 

 

 

 

 

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

 

             最快的腳步不是跨越,而是繼續,最慢的步伐不是小步,而是徘徊。
 

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