即使密码/用户无效,程序仍会继续

Program continues even though password/user are invalid

本文关键字:程序 继续 无效 密码 用户      更新时间:2023-10-16

我已经实现了一个具有2种模式,管理员和用户的自动售货机。为了访问管理面板,请求用户和密码。我的问题是,即使用户和密码错误,管理面板仍然可以访问。这是我的代码文件:

    //Login class 
         class Login
         {
         public:
         Login(); // constructor initializes data members
         bool LoginUser(User &u); // attempts to authenticate user
     };
    //LoginInfo class
      class LoginInfo
     {
      private:
      string userID;
      string password;
      public:
       LoginInfo(); // constructor sets attributes 
       bool validateUser(string name, string password);
     };
     // User class
      class User {
      private:
      string id;
      string password;
      public:
      User(string a, string pass);
      User();
      string getId();
      string getPassword();
     };

     //Login.cpp
         Login::Login() {};
    bool Login::LoginUser(User &u)
{
    LoginInfo linfo;
    string Id = u.getId();
    string pass = u.getPassword();
    bool login = false;
    if (linfo.validateUser(Id, pass)) {
        cout << "nWelcome!";
        cout << "nAuthenticated!n";
        login = true;
    }
    else
        cout << "Invalid user ID or password. Please try again.";
    return login;

}; 
//LoginInfo.cpp
#include "LoginInfo.h"
#include <iostream> 
using namespace std;
LoginInfo::LoginInfo()
{
    userID = "Mark";
    password = "1234";
};
bool LoginInfo::validateUser(string name, string pass)
{
    bool validUser = false;
    if (!(name.compare(userID))) {
        if (!(password.compare(pass))) {
            validUser = true;
        }
    }
    return validUser;
}
 //User.cpp
#include "User.h" 
using namespace std;

User::User(string lId, string lpass) : id(lId), password(lpass) {}
User::User() {
    cout << "nEnter UserID :";
    cin >> id;
    cout << "nEnter password :";
    cin >> password;
}

string User::getId() { return id; };
string User::getPassword() { return password; };
  //main.cpp

 void Vending::adminMode()
{
    unsigned userInput = 0; // user has not chosen to exit
                             // loop while user has not chosen option to exit system
    while (!userInput)
    {
        User u;
        Login l;
        bool login = l.LoginUser(u);
        cout << "n Continue = 1 , Exit  = 0 : ";
        cin >> userInput;
        if (login = true)
        {
            continue;
        }

        else if (userInput == 0)
        {
            cout << "n You have exited Admin Mode" << endl;
            testMachine();
        }

    }
    int choice;
    cout << "ADMIN MODE" << endl << endl;

    while (1)
    {
        cout << "Please select an option: " << endl << endl;
        cout << "1 ----- Add new drink type" << endl;
        cout << "2 ----- Restock drink" << endl;
        cout << "3 ----- Edit existing Drink" << endl;
        cout << "4 ----- Print machine status" << endl;
        cout << "0 ----- Quit Maintenance mode" << endl << endl;
        cout << "User Input: ";
        cin >> choice;
        cout << endl;

如果输入错误的密码/用户,我仍然可以选择1个作为输入,然后转到"管理员"菜单。如果密码/用户不正确,则应提示用户再次插入它们,但我不知道问题所在。

预先感谢您。

错误在于:

if (login = true)
{
     continue;
}

您可能想使用:

if (login == true)
{
     continue;
}

第一种情况是指作业,并且将始终为真,第二种情况实际上是测试值是否为真。