【问题标题】:im creating a c++ program where the user has 2 attempts in trying a password username combo. If they cant get it they program stops我正在创建一个 c++ 程序,其中用户有 2 次尝试密码用户名组合的尝试。如果他们不能得到它,他们的程序就会停止
【发布时间】:2016-01-16 02:23:24
【问题描述】:

这是我得到的错误。我刚刚从 python 转到 c++,这是一个巨大的调整。这是程序的描述:编写程序,使其创建 3 个或更多有效的用户名/密码组合,然后提示用户输入用户名和密码。如果组合有效,则打印确认消息。如果组合在第一次尝试时无效,则打印一条警告消息并让用户再试一次(再次提示他们输入用户名和密码)。如果第二次尝试正确,则打印确认消息。如果第二次尝试不正确,请打印一条指责消息。在任何一种情况下,在第二次尝试后停止程序。显示三种情况的输出:第一次尝试正确的用户名/密码;第二次尝试正确;两次尝试都不正确。

$ g++ username_password.cpp -o username_password
username_password.cpp: In function ‘int main()’:
username_password.cpp:34:2: error: ‘else’ without a previous ‘if’
  else if (username != "Veasy62" && username != "tveasy62" && username != "terriyon62" && password != "a65908" && password != "a1065908" && password != "Aa1065908");
  ^




#include <iostream>
#include <string>

using namespace std;

int main()

{

    int pswrd_attempts = 0;
    string username;
    string password;
    cout << "Enter your username: " << "\n";
    getline( cin, username, '\n' );
    cout << "Enter your password: " << "\n";
    getline( cin, password, '\n' );
    while (username != "Veasy62" && username != "tveasy62" && username != "terriyon62" && password != "a65908" && password != "a1065908" && password != "Aa1065908");
    {
        pswrd_attempts++;
        cout << "Enter your username: " << "\n";
        getline( cin, username, '\n' );
        cout << "Enter your password: " << "\n";
        getline( cin, password, '\n' );

        if (pswrd_attempts > 2)

        {
            cout << "You've ran out of attempts" << "\n";
        }

    }
    else if (username == "Veasy62" && username == "tveasy62" && username == "terriyon62" && password == "a65908" && password == "a1065908" && password == "Aa1065908");
    {
        cout << "Correct password!" << "\n";



    }





}

【问题讨论】:

  • 我希望这些不是你的真实密码...

标签: c++ while-loop passwords


【解决方案1】:

在定义while 语句的行的末尾有一个分号。这会造成各种破坏。

您报告的另一个问题是因为您的 else 子句超出了应该是 while 的范围,而不是附加到 if

【讨论】:

    【解决方案2】:

    如果你检查你的 IF 语句,对应的 else 它在错误的块级别:

        if (pswrd_attempts > 2)
            {
                cout << "You've ran out of attempts" << "\n";
            }
    

    }

        else if...
    

    您需要将右括号移到 els if 块后面。

    【讨论】:

      【解决方案3】:

      我相信这就是你想要达到的目标。

      #include <iostream>
      #include <string>
      #include <vector>
      #include <conio.h>
      
      void printAttempts( unsigned attempts, const std::string& messageTryAgain, const std::string messageOutOfAttempts ) {
          if ( attempts == 0 ) {
              std::cout << messageTryAgain << std::endl;
          } else {
              std::cout << messageOutOfAttempts << std::endl;
          }
      } // printAttempts
      
      int main() {
          std::vector<std::string> vUserNames;
          std::vector<std::string> vPasswords;
      
          vUserNames.push_back( "username1" );
          vUserNames.push_back( "username2" );
          vUserNames.push_back( "username3" );
      
          vPasswords.push_back( "pass1" );
          vPasswords.push_back( "pass2" );
          vPasswords.push_back( "pass3" );
      
          unsigned passwordAttempts = 0;
          std::string username;
          std::string password;
      
          const std::string tryAgain( "You have 1 attempt remaining.\n" );
          const std::string outOfAttempts( "You are out of attempts.\n" );
      
          while ( passwordAttempts < 2 ) {
              std::cout << "Enter your username: " << "\n";
              getline( std::cin, username, '\n' );
      
              std::cout << "Enter your password: " << "\n";
              getline( std::cin, password, '\n' );
      
              bool usernameFound = false;
              bool passwordFound = false;
      
              for ( unsigned u = 0; u < vUserNames.size(); ++u ) {
                  if ( username == vUserNames.at( u ) ) {
                      usernameFound = true;
                      continue;
                  }
              }
      
              for ( unsigned p = 0; p < vPasswords.size(); ++p ) {
                  if ( password == vPasswords.at( p ) ) {
                      passwordFound = true;
                      continue;
                  }
              }
      
              if ( usernameFound && passwordFound ) {
                  std::cout << "You have successfully entered a valid username(" << username << ") and password(" << password << ")" << std::endl;
                  break; // Uncomment this line to check out all possible combinations; The loop will continue until you run out of attempts.
      
              } else if ( usernameFound && !passwordFound ) {
                  std::cout << "You have entered a valid user name(" << username << ")" << std::endl
                            << "however, you have entered an invalid password(" << password << ")" << std::endl;
                  printAttempts( passwordAttempts, tryAgain, outOfAttempts );
      
              } else if ( !usernameFound && passwordFound ) {
                  std::cout << "You have entered an invalid username(" << username << ")" << std::endl
                            << "You did however entered a valid password(" << password << ")" << std::endl;
                  printAttempts( passwordAttempts, tryAgain, outOfAttempts );
      
              } else {
                  std::cout << "You have entered an invalid username(" << username << ") and password(" << password << ")" << std::endl;
                  printAttempts( passwordAttempts, tryAgain, outOfAttempts );
              }
              passwordAttempts++;
          }
      
          std::cout << "\nPress any key to quit" << std::endl;
          _getch();
      
      } // main
      

      让我知道这对你有什么影响。

      【讨论】:

      • 挑战尝试将每个用户名映射到特定密码。
      猜你喜欢
      • 1970-01-01
      • 2020-07-14
      • 2020-12-06
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多