2

好的,我已经学习 C++ 大约 4 天了,它是我的第一门编程语言。所以这真正意味着我只有大约 8 小时的编程经验,其中很多是阅读我的 C++ 书的介绍并弄清楚如何使用 XCode。

无论如何,我的初学者 C++ 书要求我执行以下操作:“编写一个密码提示,只给用户一定次数的密码输入尝试,这样用户就不能轻易地编写密码破解程序。”

唯一的问题是我刚刚学习了循环,我认为这本书甚至还没有涵盖如何限制尝试。任何人都可以帮忙吗?我见过这个,但它对我来说太先进了,我不明白。这是代码:(真的是基本的新代码......如果它侮辱了你的智力,对不起)

#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." << "\n" <<
        endl;
    }

    else
    {
        break;
    }
}

cout << "Access granted." << endl;
}
4

9 回答 9

8

除非您明确地从循环中,否则该构造会重复无穷大while ( 1 ) { }内部的任何内容。这是一个循环,顺便说一句。{}break

经过多次尝试,你怎么能摆脱它?你可以有一个计数器,每次尝试都会增加一个计数器,并在极限处从循环中中断:

if ( ++counter >= limit )
    break;

或者只是将条件移动到 while

while ( ++counter < limit )

或使用简单的for循环或do {} while().

于 2013-03-05T21:05:06.187 回答
0

取一些变量,比如尝试计数,它跟踪尝试的次数。将其初始化为 0 并在每次尝试失败时将其递增 1。将条件放入 while 循环中,检查尝试计数是否小于允许的尝试次数(在下面的代码中取 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." << "\n" <<
    endl;

    attemptCount++;
}

else
{
    break;
}
}

cout << "Access granted." << endl;
}
于 2013-03-05T21:08:06.120 回答
0

您的while(1)循环将永远持续下去,除非您还有一些计数器,每次尝试失败都会增加计数器。

但坦率地说......为什么有一个while循环和一个单独的计数器?你有一个已知的最大迭代次数;这就是制作循环的那种情况for

for (int attempts = 1; attempts <= 3; ++attempts) {
    ... get login info ...

    if (...username and password are correct...) {
        cout << "Access granted.\n";
        return 0;
    }
    else {
        cout << "Invalid login.\n";
    }
}

// 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.\nExiting.\n";
return -1;
于 2013-03-05T21:08:43.080 回答
0

想想这是如何工作的:

#include <iostream>

using namespace std;
int main()
{
  const int MAXTRYS = 4;
  int numTrys = 0;

  while(numTrys != MAXTRYS)
  {
    cout << "Attempting login" << endl;
    ++numTrys;
  }
return 0;
}
于 2013-03-05T21:11:10.010 回答
0

您应该使用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也要构造,因为它通常有点hacky。为了练习,您应该尝试编写可以转换为简单数据流的漂亮代码。

我猜你怀疑它,但这根本不安全(你将用户名和密码以纯文本形式存储在可执行文件的代码中)。

于 2013-03-05T21:42:41.720 回答
0

我很难重新熟悉 C++,因为高中(@8 年前)发生了很大变化,或者我的信息数学老师很糟糕......我也发现“for”循环更适合这种练习,但是“返回0”不是真的吗?和“打破;” 做同样的事?这就是我用我在这里看到的和我已经“知道”的东西得出的结论:)。奇迹般有效。

#include <iostream>
#include <string>

using namespace std;
int main ()
{
int attempts = 0;
string password;

for (int attempts = 0; attempts < 5; ++attempts )
{
    cout << "enter your password! \n";
    cin >> password;
    ++attempts;
    if ( password == "boo123" )
    {
        cout << "granted!";
        return 0;
    }
    else
    {
        cout << "denied! \n";
    }
}

}

还有一件事:所有循环都是无限的,直到你“中断”;它或“返回0;” 它...

于 2013-05-11T07:06:37.603 回答
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" << "\n" << x << " " << "wrong attempts" << "\n";
       ++x;

       if ( x > 5 ) // This is the counter limit portion. Limit set to 5 attempts

       {
           break;
       }

       }

    }

}
于 2014-07-01T10:13:48.630 回答
0
#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;
}
于 2015-11-04T10:15:56.440 回答
0

包括

使用命名空间标准;

int main() { 字符串密码 = "set"; //声明密码字符串输入;//声明一个字符串供以后输入

for (int attempt = 1; attempt <= 3; attempt++) {        //if you fail the password 3 times you get kicked out
    cout << "enter password " << flush;
    cin >> input;
    if (input == password) {                            //checks to make sure the user input matches set password
        cout << "granted";
        return 0;                                       //once correct password is put in the program ends
    }
    else {                                              //if password is wrong repeat till right or 3 trys
        cout << "you fail" << endl;
    }
} 

}

于 2020-05-11T23:07:47.827 回答