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

时间:2018-04-23 13:27:00

标签: c++

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

    //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作为输入并进入管理员菜单。如果密码/用户不正确,它应该提示用户再次插入它们,但我不知道问题出在哪里。

提前谢谢。

1 个答案:

答案 0 :(得分:4)

错误在于:

if (login = true)
{
     continue;
}

您可能想要使用:

if (login == true)
{
     continue;
}

第一种情况意味着赋值,并且始终为真,第二种情况实际上是测试值是否为真。