You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++银行账户程序问题:文件覆盖与登录凭证遍历需求

C++银行账户管理程序问题修复方案

问题1:credentials.txt写入覆盖原有内容

原代码打开credentials.txt时未启用追加模式,默认会清空文件后写入新内容。修复时只需在打开文件时添加std::ios_base::app标志,让新内容追加到文件末尾。

问题2:登录仅校验首条凭证

原登录逻辑只读取record.txt的第一条记录就判断是否匹配,需改为循环遍历所有记录,直到找到匹配的账号密码组合或文件读取完毕。


修改后的完整代码

#include <iostream>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "HELLO!, WELCOME TO KINGS BANK" << endl;

    int User_Activity;

    cout << endl << endl << "1.Create Account   2.Log into existing account" << endl << endl;
    cin >> User_Activity;

    if((User_Activity < 1 || User_Activity > 2)){
        do{
            cout << endl << endl << "Input Error please try again " << endl;
            cout << endl << endl << "1.Create Account   2.Log into existing account" << endl << endl;
            cin >> User_Activity;
        } while (User_Activity < 1 || User_Activity > 2);
    }

    string FirstName;
    string LastName;
    string Email;
    string Digital_Address;
    string Nationality_Option;
    string DOB;
    string Occupation;
    string Password;
    string User_Account_Type;
    string Currency_type;
    string User_Title;
    string Nationality;
    string Telephone;
    string Marital_Status;
    string Pass_word;
    int ACN;
    int Currency;
    int Title;
    int Origin;
    int Status;
    int Register;
    int Account_Type;
    int Account_Number;

    if (User_Activity == 1){
        ofstream RecordFile;
        RecordFile.open("record.txt", std::ios_base::app);
        // 修复问题1:启用追加模式
        ofstream User_Credentials;
        User_Credentials.open("credentials.txt", std::ios_base::app);

        cout << endl << endl << "Please follow the On-Screen Questions to proceed" << endl << endl;
        cout << endl << "1.Savings Account   2.Money Market Account   3.Checking Account" << endl << endl;
        cin >> Account_Type;

        if (Account_Type == 1){
            User_Account_Type = "Savings Account";
        }
        if (Account_Type == 2){
            User_Account_Type = "Money Market Savings";
        }
        if (Account_Type == 3){
            User_Account_Type = "Checking Account";
        }
        if (Account_Type < 1 || Account_Type > 3){
            do{
                cout << endl << endl << "Input Error please try again " << endl;
                cout << endl << "1.Savings Account   2.Money Market Account   3.Checking Account" << endl << endl;
                cin >> Account_Type;
            } while (Account_Type < 1 || Account_Type > 3);
        }

        cout << endl << endl << "Currency Type" << endl;
        cout << endl << "1.USD   2.GHC   3.EUR" << endl << endl;
        cin >> Currency;

        if (Currency == 1){
            Currency_type = "USD";
        }
        if (Currency == 2){
            Currency_type = "GHC";
        }
        if (Currency == 3){
            Currency_type = "EUR";
        }
        if (Currency < 1 || Currency > 3){
            do{
                cout << endl << endl << "Input Error please try again " << endl;
                cout << endl << "1.USD   2.GHC   3.EUR" << endl << endl;
                cin >> Currency;
            } while (Currency < 1 || Currency > 3);
        }

        cout << endl << endl << "Specify title" << endl;
        cout << endl << "1.Mrs   2.Mr   3.Miss   4.Ms" << endl << endl;
        cin >> Title;

        if (Title == 1){
            User_Title = "Mrs";
        }
        if (Title == 2){
            User_Title = "Mr";
        }
        if (Title == 3){
            User_Title = "Miss";
        }
        if (Title == 4){
            User_Title = "Ms";
        }
        if (Title < 1 || Title > 4){
            do{
                cout << endl << endl << "Input Error please try again " << endl;
                cout << endl << "1.Mrs   2.Mr   3.Miss   4.Ms" << endl << endl;
                cin >> Title;
            } while (Title < 1 || Title > 4);
        }

        cout << endl << endl << "By completing this form you agree to the terms and conditions that govern your account and your relationship with the bank" << endl << endl;
        cout << endl << "1.Register   2.Cancel" << endl << endl;
        cin >> Register;

        if (Register == 1){
            cout << endl << endl << "Registration success" << endl << endl;
        }
        if (Register == 2){
            cout << endl << endl << "See you soon" << endl;
        }
        if (Register < 1 || Register > 2){
            do{
                cout << endl << endl << "Input Error please try again " << endl;
                cout << endl << "1.Register   2.Cancel" << endl << endl;
                cin >> Register;
            } while (Register < 1 || Register > 2);
        }

        cout << endl << "Create a password for logins" << endl <<endl;
        cin >> Password;

        Account_Number = 85;
        cout << endl << endl << "Your Account Number is " << Account_Number << endl << endl;

        RecordFile << Account_Number << " " << Password << endl;

        User_Credentials << "-------------------------" << endl; // 添加分隔线区分用户
        User_Credentials << "Name : " << User_Title << " " << FirstName << " " << LastName << endl;
        User_Credentials << "Email : " << Email << endl;
        User_Credentials << "Digital Address : " << Digital_Address << endl;
        User_Credentials << "Date Of Birth : " << DOB << endl;
        User_Credentials << "Occupation : " << Occupation << endl;
        User_Credentials << "Nationality : " << Nationality << endl;
        User_Credentials << "Marital Status : " << Marital_Status << endl;
        User_Credentials << "Contact : " << Telephone  << endl;
        User_Credentials << "Account Type : " << User_Account_Type << endl;
        User_Credentials << "Currency : " << Currency_type << endl;

        User_Credentials.close();
        RecordFile.close();
    }
    else if (User_Activity == 2){
        ifstream InputFile;
        InputFile.open("record.txt", std::ios_base::in);

        cout << endl << endl << "Enter Account Number to Login" << endl << endl;
        cin >> ACN;

        cout << endl << endl << "Enter Password to login" << endl << endl;
        cin >> Pass_word;

        bool loginSuccess = false;
        // 修复问题2:循环遍历所有账号记录
        while (InputFile >> Account_Number >> Password) {
            if (Account_Number == ACN && Password == Pass_word) {
                loginSuccess = true;
                break;
            }
        }

        if (loginSuccess) {
            cout << endl << "Welcome Back" << endl;
        }
        else {
            cout << endl << "Login Error, Please Create Account or check credentials" << endl;
        }

        InputFile.close();
    }
}

修改说明

  1. 文件追加修复:打开User_Credentials时添加std::ios_base::app参数,确保新用户信息不会覆盖原有内容;额外添加分隔线,方便区分不同用户的凭证信息。
  2. 登录遍历修复:将单次读取判断改为while循环,逐行读取record.txt中的账号密码,直到找到匹配项或读取完毕,用loginSuccess变量标记登录状态,最后输出对应提示。

内容的提问来源于stack exchange,提问作者Kingsford Quainoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 04:03:09