不管输入什么密码都是密码错误,求看看哪里出错了(银行账户管理系统)(C++)
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
class Account {
protected:
string name;
string id;
string password;
double balance;
bool live;
char mark;
public:
Account() {}
Account(string nn,string ni,string np,double nb,bool nl,char nm) {
name = nn;
id = ni;
balance = nb;
live = nl;
mark = nm;
}
//查询余额
double query(){
return balance;
}
//存款
bool withdraw(double money) {
if (balance < money) {
return false;
balance -= money;
return true;
}
}
//存款
void deposit(double money) {
balance += money;
}
//成员对外窗口
string getName() {
return name;
}
string getID() {
return id;
}
string getPassword() {
return password;
}
double getBalance() {
return balance;
}
char getMark() {
return mark;
}
bool isLive() {
return live;
}
void setPassword(string np) {
password = np;
}
void setLive(bool nl) {
live = nl;
}
void setBalance(double nb) {
balance = nb;
}
};
class FileTools {
public:
static vector<Account> accounts;
// 读取文件中的账户信息
static void getAccounts() {
//打开文件读入流
ifstream in;
in.open("ID.txt", ios::in);
//读取数据到数组中
string id, password, name;
double balance;
char mark;
//标记是否为管理员
accounts.clear();
while (in >> mark >> id >> password >> name >> balance) {
accounts.push_back(Account(name, id, password, balance, true, mark));
}
in.close();
}
//将内存中的账户信息存储到文件
static void saveAccounts() {
//打开文件输出流
ofstream out;
out.open("ID.txt", ios::out);
// 输出到文件中(已经删除的账户就不存了)
for (int i = 0; i < accounts.size(); i++) {
Account a=accounts[i];
if (a.isLive())
out << a.getMark() << " " << a.getID() << " "
<< a.getPassword() << " " << a.getName() << " "
<< a.getBalance() << endl;
}
out.close();
}
//将日志信息写入到日志文件
static void addlog(string log) {
//打开文件输出流
ofstream out;
out.open("IOG.txt", ios::out | ios::app);
//输出到日志文件中
out << log << endl;
out.close();
}
//查找某个用户在数组中的位置
static int findAccountByID(string id) {
for (int i = 0; i < accounts.size(); i++) {
if (accounts[i].getID() == id) {
return i;
}
}
//没找到返回-1
return -1;
}
};
class Admin :Account {
public:
Admin(Account a) {
name = a.getName();
id = a.getID();
password = a.getPassword();
balance = a.getBalance();
live = true;
}
//添加用户
void addAccount(Account a) {
FileTools::accounts.push_back(a);
}
//根据账号删除账户
bool deleteAccountByID(string id) {
int index = FileTools::findAccountByID(id);
//没找到账号
if (index == -1)
return false;
//修改账户为已死亡
FileTools:: accounts[index].setLive(false);
return true;
}
//根据账号修改用户的余额
bool editPasswordByID(string id, string password){
int index=FileTools :: findAccountByID(id);
//没找到账号
if (index == -1)
return false;
//进行修改
FileTools:: accounts[index].setPassword(password);
return true;
}
private:
};
vector<Account> FileTools::accounts;
class Frame {
//当前用户(下标)
int nowAccount;
public:
Frame() {
int select;
while (true) {
// 一级菜单
cout << "请输入选项进行对应操作" << endl;
cout << "1,用户登录" << endl;
cout << "2,管理员操作" << endl;
cout << "3,注册新用户" << endl;
cout << "0,退出" << endl;
cin >> select;
cout << "-------------------------------------" << endl;
if (select == 0) {
//退出之前保存文件
FileTools::saveAccounts();
break;
}
string id, password;
int index;
switch (select) {
case 1: {
if (loginFrame()) {
accountFrame();
FileTools::saveAccounts();
}
break;
}
case 2: {
if (loginFrame()) {
if (FileTools::accounts[nowAccount].getMark() != '*') {
cout << "-----------不是管理员-------------" << endl;
}
else {
cout << "----------------------------------" << endl;
adminFrame();
FileTools::saveAccounts();
}
}
break;
}
case 3:
registFrame();
break;
}
}
}
bool loginFrame() {
string id, password;
int index;
cout << "输入账号密码" << endl;
while (true) {
cin >> id;
if (id == "0") { //登录失败
cout << "------ - 登求失败 - " << endl;
return false;
}
if ((index = FileTools::findAccountByID(id)) != -1) {
cin >> password;
if (password != FileTools::accounts[index].getPassword()) {
cout << "密码错误," << endl;
}
else {
//登录成功
cout << "- 登录成功 -" << endl;
nowAccount = index;
return true;
}
}
else {
//没找到账号
cout << "账号不存在,";
cout << "请重新输入账号密码或输入 0 退出" << endl;
}
}
}
//用户窗口
void accountFrame(){
int select;
double money;
while (true) {
cout << "1,存钱" << endl;
cout << "2,取钱" << endl;
cout << "3,查余额" << endl;
cout << "0,退出" << endl;
cin >> select;
if (select == 0)
return;
switch (select) {
case 1: {
cout << "输入金额:";
cin >> money;
double nowMoney = FileTools::accounts[nowAccount].getBalance();
FileTools::accounts[nowAccount].setBalance(nowMoney + money);
cout << "---存钱成功---" << endl;
break;
}
case 2: {
cout << "输入取钱金额:";
cin >> money;
double nowMoney = FileTools::accounts[nowAccount].getBalance();
if (nowMoney < money) {
cout << "----余领不足-- -" << endl;
}
else {
FileTools::accounts[nowAccount].setBalance(nowMoney - money);
cout << "--取钱成功--" << endl;
}
break;
}
case 3: {
cout << "当前余额为: " <<
FileTools::accounts[nowAccount].getBalance() << endl;
cout << "_" << endl;
break;
}
}
}
}
//管理员窗口
void adminFrame() {
int select;
string id, password, np, name;
Admin admin(FileTools:: accounts[nowAccount]);
while (true) {
cout << "1,添加账户" << endl;
cout << "2,删除账户" << endl;
cout << "3,修改密码" << endl;
cout << "0,退出" << endl;
cin >> select;
if (select == 0)
break;
switch (select) {
case 1: {
cout << "输入账号,密码,姓名" << endl;
cin >> id >> password >> name;
admin.addAccount(Account(name, id, password, 0, true, '#'));
FileTools::saveAccounts();
cout << "---- - 派加成功---- ------- - " << endl;
break;
}
case 2: {
cout << "输入账号" << endl;
cin >> id;
if (admin.deleteAccountByID(id)) {
FileTools::saveAccounts();
cout << "----------------删除成功----------------" << endl;
}
else {
cout << "-账号不存在-- - ucc" << endl;
}
break;
}
case 3: {
cout << "输入账号,和新密码" << endl;
cin >> id >> np;
if (admin.editPasswordByID(id, np)) {
FileTools::saveAccounts();
cout << ".----------- - 修改成功------ - " << endl;
}
else {
cout << "----------------账号不存在----------------" << endl;
}
break; }
}
}
}
// 注册窗口
void registFrame() {
string name, id, password;
cout << "输入姓名" << endl;
cin >> name;
cout << "输入账号" << endl;
while (true) {
cin >> id;
if (FileTools::findAccountByID(id) != -1) {
//有重复的账号
cout << "._----账号重复--" << endl;
}
else {
cout << "输入密码" << endl;
cin >> password;
FileTools::accounts.push_back(Account(name, id, password, 0, true, '#'));
FileTools::saveAccounts();
cout << "----------------往册成功-------- - " << endl;
return;
}
}
}
};
int main() {
//读取文件中的信息
FileTools::getAccounts();
//打开窗口
Frame frame;
return 0;
}
你注册用户,生成账户对象时,密码没有初始化
从你的代码中,我发现可能的问题在于loginFrame()
函数。这个函数中,你从输入中读取用户的id和密码,并尝试找到对应的账户。然后,你使用==
操作符来比较输入的密码和账户的密码。然而,在C++中,cin >> password;
这一行读取输入时,可能会包含额外的空白字符(例如换行符)。这可能是导致你的密码比较总是失败的原因。
你可以尝试在读取密码之后使用std::cin.ignore();
函数来丢弃任何多余的字符。这个函数将清除输入流中的字符,直到遇到换行符。
你的代码应该看起来像这样:
...
while (true) {
cin >> id;
if (id == "0") { //登录失败
cout << "------ - 登求失败 - " << endl;
return false;
}
if ((index = FileTools::findAccountByID(id)) != -1) {
cin >> password;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //添加这行
if (password != FileTools::accounts[index].getPassword()) {
cout << "密码错误," << endl;
}
...
}
...
}
...
另外,你也需要确认你在创建Account
对象时设置了正确的密码,并且在写入和读取文件时没有修改密码。
本篇文章属于开源库TransForms系列,该库主要用于C++坐标转换使用,以及一些标定算法的实现和优化,目前还在不断更新中,请持续关注。
开源地址:https://gitee.com/ohhuo/transforms3d_cpp
基础模块:
拓展模块:
针对输入密码出错的问题,有以下几个可能的原因和解决方法:
解决方法:请仔细检查输入的密码是否正确,区分大小写和特殊字符等。
解决方法:请检查密码验证函数的代码是否正确,包括函数名和参数、返回值等。可以针对密码验证函数进行单元测试,检查其验证逻辑是否正确。
以下是一个简单的密码验证函数示例:
bool validatePassword(string inputPassword, string correctPassword) { return inputPassword == correctPassword; }
解决方法:请检查密码保存或传递的代码是否正确。如果密码是从文件或数据库中读取的,则需要检查读取的代码是否正确。如果密码需要传递到其他函数或类中,则需要检查传递的参数是否有误。
以下是一个简单的密码保存和传递示例:
class BankAccount { public: BankAccount(string password) { this->password = password; }
void showBalance() {
if (validatePassword(getPassword())) {
cout << "Your balance is: " << balance << endl;
} else {
cout << "Invalid password" << endl;
}
}
string getPassword() {
return password;
}
private: string password; double balance; };
int main() { string password = "123456"; BankAccount account(password); account.showBalance(); return 0; }
解决方法:请仔细检查程序的其他部分,包括输入输出、函数调用、内存管理等。如果可能,可以对程序进行调试,以查找具体的错误原因。
需要注意的是,程序中的一些细节问题也可能导致密码验证失败,例如字符串处理、空格和换行符等。因此,需要对代码进行仔细的检查和测试,确保程序的正确性。
最后,建议在编程过程中,遵循良好的编程习惯,包括注释、代码规范、单元测试等,以提高程序的可读性和可维护性。