即使密码/用户无效,程序仍会继续
Program continues even though password/user are invalid
我已经实现了一个具有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;
}
第一种情况是指作业,并且将始终为真,第二种情况实际上是测试值是否为真。
相关文章:
- 我正在尝试使用 c++ 创建一个货币转换程序,我不知道如何继续
- C++ 在信号处理程序后继续执行
- 为什么我的Qt程序在断言失败后继续运行?
- 在程序继续C++时保持进程运行
- 继续运行程序而无需任何干预,直到要求退出为止
- 选择"Y"继续后,我的程序一直处于关闭状态
- C++,程序输出无效输入,但继续通过循环
- 即使密码/用户无效,程序仍会继续
- 测试回文时程序不会继续运行 (C++)
- 程序终止,而不是继续整数输入验证循环
- 即使有例外,希望程序继续继续
- C++,我怎样才能在继续整个程序的同时延迟程序的一部分
- QT/C++,OS X:退出应用程序时可选择隐藏(),以便应用程序继续运行,并在单击dock中的图标后再次显示
- 在程序继续执行的同时,如何使代码的两部分之间存在时间间隔或延迟?C++
- 提升ASIO.当我的程序继续发送数据时,异步接收不起作用
- 有没有办法在我的程序继续时"grow"我的数组?
- 当cin不是int类型时,c++程序继续循环
- 程序继续运行,没有任何输出
- 如何从已经停止的程序继续使用lldb
- 获得由内存泄漏引起的内存溢出,应用程序继续运行和分配