2

わかりました、私は約 4 日間 C++ を学んでおり、それが私の最初のプログラミング言語です。つまり、これが実際に意味することは、私は約 8 時間のプログラミング経験しかなく、その多くは C++ の本の紹介を読み、XCode の使用方法を理解することだったということです。

とにかく、初心者向けの C++ の本では、次のことを行うように求められています。

唯一のことは、ループを学んだばかりで、この本では試行を制限する方法についてもまだカバーされていないと思います。誰でも助けることができますか?これを見たことがありますが、私には高度すぎてわかりません。コードは次のとおりです:(本当に基本的な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." << "\n" <<
        endl;
    }

    else
    {
        break;
    }
}

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

9 に答える 9

8

ループから明示的に除外しない限り、構造は無限大while ( 1 ) { }の内側にあるものをすべて繰り返します。それはループです、ところで。{}break

何度か試みた後、どうやってそれから抜け出すことができますか?試行のたびにインクリメントされ、制限でループから抜けるカウンターを持つことができます。

if ( ++counter >= limit )
    break;

または単に条件をしばらくの間移動します

while ( ++counter < limit )

または、単純なforループまたはを使用しdo {} while()ます。

于 2013-03-05T21:05:06.187 に答える
0

行われた試行回数を追跡する変数、たとえば、attemptCount を使用します。0 に初期化し、失敗するたびに 1 ずつ増やします。条件を while ループに入れ、attemptCount が許可された試行回数 (以下のコードでは 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

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構成も一般的に一種のハッキーであるため、回避するようにしてください。練習するには、簡単なデータフローに変換される素敵なコードを書くようにしてください。

あなたはそれを疑っていると思いますが、これはまったく安全ではありません (実行可能ファイルのコードにプレーンテキストでユーザー名とパスワードを保存します)。

于 2013-03-05T21:42:41.720 に答える
0

高校 (@8 年前) が大きく変わったので、C++ に再び慣れるのに苦労しています。または、私の情報数学の先生がただ悪いだけでした。 「return 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";
    }
}

}

もう 1 つ: すべてのループは「壊れる」まで無限です。それまたは「リターン0;」それ...

于 2013-05-11T07:06:37.603 に答える
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

含む

名前空間 std を使用します。

int main() { string password = "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 に答える
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 に答える