【问题标题】:Password Authentication c++密码认证 c++
【发布时间】:2014-03-31 13:21:17
【问题描述】:

您好,这是我第一次使用课程,因此为我糟糕的解释深表歉意。基本上我正在为电梯程序制作密码功能。 LogIn 是我的班级的名称,其中包含字符串“john”,即密码。一切似乎都工作正常,除了错误密码尝试的循环。

如果密码第一次尝试正确,则代码可以正常工作,但是如果密码输入错误,则在接下来的两次尝试中将出现"Incorrect name. Try again" 行,无论密码是否输入正确。我希望有人能看出我哪里出错了。 name 是存储的密码,nameAttempt 是用户尝试输入的密码。

#include "stdafx.h"
#include "LogIn.h"
#include <iostream>
#include <iostream>
#include <string>

using namespace std;


bool password() {

    string name;
    string nameAttempt; 
    int attempts = 0;   

    cout << "nameAttempt: " << endl;
    cin >> nameAttempt; 

    LogIn Authenticate(name, nameAttempt);


    if (Authenticate.getName() == Authenticate.getNameAttempt()) 
    {
            return true;
    }
    else
            while (Authenticate.getName() != Authenticate.getNameAttempt())
            {
                    if (attempts++ ==2)
                    {
                            return false;
                    }       
                    cout<<"Incorrect name. Try again"<< endl;
                    cout<< "" << endl;

                    cout << "Enter Name:"<< endl;
                    cin >>nameAttempt;
            }
}



int main()
{

    bool password();

    bool loggedin = password();

    if(loggedin) {
        cout << "Password Correct" << endl;
    }

    if(!loggedin) {
        cout << "Incorrect Password" << endl;
        cout << "Program will now terminate" << endl;
        system("pause");
        return 0;   
    }

    cout << "you are now free to enter lift" << endl;

    system("pause");
    return 0;
}

【问题讨论】:

  • 你试过使用调试器吗?
  • 您也应该发布您的LogIn 定义,因为您有一些令人困惑的陈述:LogIn Authenticate(name, nameAttempt) vs. LogIn 是我的班级名称,其中包含字符串“john”是密码。 name 是干什么用的? LogIn::LogIn 的参数类型是什么?您是按值复制还是 LogIn 存储引用?

标签: c++ function class


【解决方案1】:

在重试循环中,您仍然需要验证尝试的名称,如果名称被接受则中断循环。

【讨论】:

    【解决方案2】:

    你初始化局部函数变量

    int attempts = 0; 
    

    所以 while 循环中的退出条件将在代码的第三次触发

     if (attempts++ ==2)
    

    已运行,因此您将打印两次:

    while (Authenticate.getName() != Authenticate.getNameAttempt())
    {
        if (attempts++ ==2) // increment attempts
    {
        return false;
    }      
    

    看起来是故意在第二次打印后退出,所以你的困惑很难理解。使用调试器,这种错误很容易排查。

    【讨论】:

      【解决方案3】:

      我觉得代码应该是这样的:

      while (1)
          {
              if (Authenticate.getName() == Authenticate.getNameAttempt())
              {
                  return true;
              }
              else        
              {
                  if (attempts++ == 2)
                  {
                      return false;
                  }
                  cout << "Incorrect name. Try again" << endl;
                  cout << "" << endl;
      
                  cout << "Enter Name:" << endl;
                  cin >> nameAttempt;
      
                  Authenticate.setNameAttempt(nameAttempt);
              }
          }
      

      【讨论】:

        【解决方案4】:

        试试这个,甜蜜又简单:

        cout << "nameAttempt: " << endl;
        cin >> nameAttempt; 
        
        LogIn Authenticate(name, nameAttempt);
        attempts = 0;
        while (attempts<2)
        {
            if (Authenticate.getName() == Authenticate.getNameAttempt()) 
            {
              return true;
             }
        
             cout<<"Incorrect name. Try again"<< endl;
             cout<< "" << endl;
        
             cout << "Enter Name:"<< endl;
             cin >>nameAttempt;
             attempts++;
             LogIn Authenticate(name, nameAttempt);
         }
        return false;
        

        【讨论】:

        • 仍然遇到同样的问题。如果我第一次猜错了,即使输入正确,接下来的两次也会说不正确
        • 你能告诉我什么吗:LogIn Authenticate(name, nameAttempt);有吗?
        • 另外,在您的代码中,您声明但未实例化变量“名称”
        • 登录验证(姓名,姓名尝试);就是使用name变量,字符串“john”存储的是LogIn类。 nameAttempt 是来自该类的空字符串。我相信我可以在没有课程的情况下更轻松地做到这一点,但不幸的是我必须使用课程
        • 好吧,我明白了,1,为了比较字符串,最好使用 strncmp,参见cplusplus.com/reference/cstring/strncmp,2. 确保将 nameAttempt 设置为作为参数传递的 nameAttempt。 3. 检查 Authenticate.getName() 和 Authenticate.getNameAttempt() 持有什么值,只需使用 cout
        猜你喜欢
        • 2013-11-06
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 2015-04-03
        • 1970-01-01
        • 2023-02-23
        • 1970-01-01
        • 2017-07-26
        相关资源
        最近更新 更多