在C ++中限制登录尝试次数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在C ++中限制登录尝试次数相关的知识,希望对你有一定的参考价值。
好吧,我学习C ++已有大约4天了,这是我的第一门编程语言。因此,这的真正含义是,我只有大约8个小时的编程经验,其中很多是阅读我的C ++书籍简介并弄清楚如何使用XCode。
无论如何,我的C ++初学者书要求我执行以下操作:“写一个密码提示,该提示仅向用户提供一定次数的密码输入尝试,以使用户无法轻松编写密码破解程序。”
唯一的是,我现在才学到循环,而且我认为这本书还没有介绍如何限制尝试。有人可以帮忙吗?我看过this,但对我来说太高级了,我不明白。这是代码:(实际上是基本的newb代码,如果侮辱了您的智慧,请抱歉)
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
string password;
while ( 1 )
{
cout << "Enter your username: " << endl;
cin >> username;
cout << "Enter your password: " << endl;
cin >> password;
if ( username != "billy" && password != "bob" )
{
cout << "Incorrect username/password combination. Please try again." << "
" <<
endl;
}
else
{
break;
}
}
cout << "Access granted." << endl;
}
while ( 1 ) { }
构造将{}
内部的所有内容重复到无穷大,除非您从循环中显式break
。顺便说一句,循环。
经过多次尝试,您如何才能摆脱困境?您可能有一个计数器,每次尝试都会增加一个计数器,并在极限处中断循环:
if ( ++counter >= limit )
break;
或简单地将条件移动到while内
while ( ++counter < limit )
或使用简单的for
循环或do {} while()
。
采用一些变量,例如tryCount,它跟踪进行的尝试次数。将其初始化为0,并在每次尝试失败后将其递增1。将条件放入while循环中,检查一下tryCount是否小于允许的尝试次数(在下面的代码中为3)。因此,代码将是:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
string password;
int attemptCount = 0;
while ( attemptCount < 3 )
{
cout << "Enter your username: " << endl;
cin >> username;
cout << "Enter your password: " << endl;
cin >> password;
if ( username != "billy" && password != "bob" )
{
cout << "Incorrect username/password combination. Please try again." << "
" <<
endl;
attemptCount++;
}
else
{
break;
}
}
cout << "Access granted." << endl;
}
考虑其工作原理:
#include <iostream>
using namespace std;
int main()
{
const int MAXTRYS = 4;
int numTrys = 0;
while(numTrys != MAXTRYS)
{
cout << "Attempting login" << endl;
++numTrys;
}
return 0;
}
您的while(1)
循环将永远持续下去,除非您还有一些每次尝试失败都会增加的计数器。
但是坦白说...为什么要有一个while
循环和一个单独的计数器?您具有已知的最大迭代次数;这就是for
循环的一种情况。
for (int attempts = 1; attempts <= 3; ++attempts) {
... get login info ...
if (...username and password are correct...) {
cout << "Access granted.
";
return 0;
}
else {
cout << "Invalid login.
";
}
}
// Note as well, the default case (what happens if they make it through the loop)
// should be no access. Otherwise, someone could just succeed by inputting three
// bad passwords. :P
cout << "Too many invalid login attempts.
Exiting.
";
return -1;
您应该使用for
循环,而不是while
循环。类似于以下内容:
bool bSuccess = false;
for (int i = 0 ; i < maxAttemps ; ++i)
{
// your stuff
// set bSuccess = true when relevant
}
if (bSuccess)
{
// ok, successfully logged in
}
无限循环通常仅限于真正的无限循环(永远等待网络消息直到退出,等等)。作为良好实践的经验法则,请尝试尽可能避免无限循环,并且break
也会构造,因为它通常有点怪异。为了锻炼身体,您应该尝试编写漂亮的代码,将其转换为简单的数据流。
我想您对此表示怀疑,但这根本不是安全的(您将用户名和密码以纯文本格式存储在可执行文件的代码中。)>
我在重新熟悉C ++时遇到了困难,因为高中(@ 8年前)发生了很多变化,或者我的信息老师很糟糕...我还发现“ for”循环更适合这种练习,但是“ return 0”不是真的吗?和“休息”;做同样的事情?这就是我在这里看到的内容以及已经“知道”的内容的结果:)。就像魅力一样。
/*Write a password prompt that gives a user only a certain number of password entry attempts—
so that the user cannot easily write a password cracker*/
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string password;
int x = 1;
while (1) //This creates an overall top level infinite loop
{
cout << "Input password here: ";
cin >> password;
if ( password == "teddy") //This sets the condition for success
{
cout << "Access Granted!!!!";
break; //The break is applied here to stop the cycle after success is made
}
else if ( password != "teddy") //This sets the condition for failure
{
cout << "Wrong username/password" << "
" << x << " " << "wrong attempts" << "
";
++x;
if ( x > 5 ) // This is the counter limit portion. Limit set to 5 attempts
{
break;
}
}
}
}
#include <iostream>
using namespace std;
int main()
{
string password;
int pCounter = 0;
cout << "Enter Password here: ";
getline(cin, password);
while(pCounter <= 4){
if(password != "winner"){
cout << "Count: " << pCounter << endl;
cout << "Try again..wrong entry.." << endl;
cout << "Enter Password here: ";
getline(cin, password);
++pCounter;
if((password != "winner") && (pCounter == 4)){
cout << "The End..No more tries!!" << endl;
break;
}
}
else{
cout << "Welcome In Bro" << endl;
break;
}
}
return 0;
}
包括
以上是关于在C ++中限制登录尝试次数的主要内容,如果未能解决你的问题,请参考以下文章