网上有很多密码程序的示例代码,它用星号隐藏输入。当我使用我的 CodeBlocks IDE 编译它们时,这些程序可以工作,每次我输入一个字母时都会输出 *
。
Enter Password : ******
You entered : iiiiii
Process returned 0 (0x0) execution time : 6.860 s
Press any key to continue.
但是,当我使用 CLion IDE 时,我可以看到我输入的字母:
Enter Password :iiiiii
******
You entered : iiiiii
Process finished with exit code 0
有人可以解释为什么两个 IDE 之间存在这种差异吗?
我正在使用的代码(我在网上找到的)是:
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std; //needed for cout and etc
int main()
{
START:
system("cls");
cout<<"\nEnter Password : ";
char pass[32];//to store password.
int i = 0;
char a;//a Temp char
for(i=0;;)//infinite loop
{
a=getch();//stores char typed in a
if((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'))
//check if a is numeric or alphabet
{
pass[i]=a;//stores a in pass
++i;
cout<<"*";
}
if(a=='\b'&&i>=1)//if user typed backspace
//i should be greater than 1.
{
cout<<"\b \b";//rub the character behind the cursor.
--i;
}
if(a=='\r')//if enter is pressed
{
pass[i]='\0';//null means end of string.
break;//break the loop
}
}
cout<<"\nYou entered : "<<pass;
//here we can even check for minimum digits needed
if(i<=5)
{
cout<<"\nMinimum 6 digits needed.\nEnter Again";
getch();//It was not pausing :p
goto START;
}
return 0;
}
//Lets check for errors.
//You can even put file system.
我知道有很多类似的问题,但是,没有一个可以解释为什么在使用 CLion IDE 时它不起作用。
原文由 BusyProgrammer 发布,翻译遵循 CC BY-SA 4.0 许可协议
也许