c++密码程序
Program for Password in C++
密码程序不工作....请帮助……对于正确输入,也会显示密码错误
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
void main()
{
clrscr();
int ctr=0;
int o;
char pass[5];
cout<<"enter password";
for(int i=0;i<5 && (o=getch())!=13 ;i++)
{
pass[i]=o;
putch('*');
}
ctr=strcmp(pass,"luck");
cout<<ctr;
if(ctr==0)
{
cout<<"welcome";
}
else
{
cout<<"wrong password";
}
getch();
}
我想知道为什么这个密码程序不工作....
为了能够使用strcmp()
,您需要以空终止pass
。您还需要确保pass
足够大以容纳NUL。
由于<conio.h>
正在使用中,我假设正在使用Windows。对于那些感兴趣的人,这里有一个正确的方法来做这件事。我输入一行作为密码,在按enter键时结束,并且不显示星号,因为它们很容易泄露长度。
//stop echoing input completely
HANDLE inHandle = GetStdHandle(STD_INPUT_HANDLE); //get handle to input buffer
DWORD mode; //holds the console mode
GetConsoleMode(inHandle, &mode); //get the current console mode
SetConsoleMode(inHandle, mode & ~ENABLE_ECHO_INPUT); //disable echoing input
//read the password
std::string password; //holds our password
std::getline(std::cin, password); //reads a line from standard input to password
//compare it with the correct password
std::cout << (password == "luck" ? "Correct!n" : "Wrong!n"); //output result
//return console to original state
SetConsoleMode(inHandle, mode); //set the mode back to what it was when we got it
当然,您可以做一些事情来改进它(硬编码的密码字符串从来都不是一件好事),如果您愿意,可以继续这样做,但关键是它可以作为基本的密码输入系统,并且具有易于遵循的结构。在获得密码输入时,您仍然可以使用您喜欢的东西,而不是一次输入一个字符并求助于C字符串和代码。
相关文章:
- 密码登录程序将永远循环并显示不正确的结果
- 如何在C++的凯撒密码程序中包含空格?
- 即使密码/用户无效,程序仍会继续
- 程序输入密码并解锁窗口7,8,10
- 凯撒密码程序中"Debug Assertion Failed!"问题
- Atbash 密码程序 A = Z, B = Y, C = X
- 密码验证程序
- C++应用程序连接到网站服务器并根据密码检索文件
- 如何制作一个从文件中读取密码并与用户编写的密码进行比较的 c++ 程序
- 简单的密码哈希程序 c++
- 在我的C++凯撒密码程序中出现错误
- 凯撒密码程序
- 在CPP应用程序中保存密码
- 制作需要接受字母和数字组合的密码类型程序
- 在c++登录程序中屏蔽密码
- 程序的密码
- 转录密码加密程序崩溃
- 密码检查程序-匹配密码失败-循环失败
- C++ 密码程序上的计数循环
- 密码验证程序