gpt4 book ai didi

c++ - 如何使用 CLion 在 C++ 中显示输入密码的星号

转载 作者:行者123 更新时间:2023-11-28 05:20:22 25 4
gpt4 key购买 nike

网上有很多密码程序的示例代码,可以用星号隐藏输入。当我每次输入一个字母时输出一个 * 来使用我的 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 时它不起作用。

最佳答案

也许

#include <conio.h>

int main()
{
char s[10] = { 0 };
int i;
for (i = 0; i < 10;i++) {
s[i] = _getch(); _putch('*');
if (s[i] == 13) break;
};
printf("\nYour pass is %s", s);
getchar();
return 0;
}

关于c++ - 如何使用 CLion 在 C++ 中显示输入密码的星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com