将结构的 c++ 数组保存到文件和问题中以向其添加新元素

Saving c++ array of struct to file and issue for adding new elements to it

本文关键字:添加 新元素 元素 c++ 结构 数组 问题 存到文件      更新时间:2023-10-16

我的程序目前有两个主要问题。首先是我无法在程序运行时向程序添加多个帐户(我需要关闭它并重新打开,然后才能添加另一个帐户(。第二个问题是当我不向程序地址添加任何帐户时,会保存到程序中,这就是我不向程序添加任何帐户时文件的样子。

123#John Smith#0#0###-1.07374e+008#-1.07374e+008#

文件的第一部分是正确的,但地址来自内存中的其他地方。这就是我的代码的样子。

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cmath> 
#include <fstream> 
#include <sstream>
using namespace std;
struct account
{
    string acctNum;
    string name;
    float cBal;
    float sBal;
};
int menu();
char subMenu();
int loadCustomers(account[]);
void saveCusomers(account[], int);
int newCustomer(account[], int);
int deleteCustomer(account[], int);
int findCustomer(account[], int);
void deposit(account[], int);
void withdrawl(account[], int);
void balance(account[], int);
void bankBalance(account[], int);
int main()
{
    account acc[20];
    int selection;
    int numAcc = 0;
    int search;
    numAcc = loadCustomers(acc);
    do
    {
        selection = menu();
        if(selection == 1)
        {
            newCustomer(acc, numAcc);
        }
        else if(selection == 2)
        {
            deleteCustomer(acc, numAcc);
        }
        else if(selection == 3)
        {
            search = findCustomer(acc, numAcc);
            if (search == -1)
            {
                cout << "That account doesn't exist." << endl;
                system("pause");
                system("cls");
            }
            else
            {
                cout << right << setw(3) << acc[search].acctNum << "" << left << setw(15) << acc[search].name << acc[search].cBal << acc[search].sBal << endl;
                system("pause");
                system("cls");
            }
        }
        else if(selection == 4)
        {
            deposit(acc, numAcc);
        }
        else if(selection == 5)
        {
            withdrawl(acc, numAcc);
        }
        else if(selection == 6)
        {
            balance(acc, numAcc);
        }
        else if(selection == 7)
        {
            bankBalance(acc, numAcc);
        }
        else if(selection == 8)
        {
            break;
        }
    } while (selection != 8);
    saveCusomers(acc, numAcc);
    return 0;
}
int menu()
{
    int select;
    cout << "Main Menu" << endl;
    cout << "=============" << endl;
    cout << "1. New Account" << endl;
    cout << "2. Delete Account" << endl;
    cout << "3. Find Customer" << endl;
    cout << "4. Deposit" << endl;
    cout << "5. Withdrawl" << endl;
    cout << "6. Balance" << endl;
    cout << "7. Bank Balance" << endl;
    cout << "8. Exit" << endl;
    cout << "=============" << endl;
    cout << "Enter choice: ";
    cin >> select;
    while (select < 1 || select > 8)
    {
        cout << "Invalid input, select a number between 1 and 8: ";
        cin >> select;
    }
    system("cls");
    return select;
}
char subMenu()
{
    char choice;
    cout << "Which account? <C>hecking or <S>aving: ";
    cin >> choice;
    while(choice != 'C' && choice != 'c' && choice != 'S' && choice != 's')
    {
        cout << "Invalid choice, choose either checking or saving: ";
        cin >> choice;
    }
    return choice;
}
int loadCustomers(account acc[])
{
    ifstream inFile;
    int numCustomers = 0, i = 0;
    string ctemp, stemp;
    inFile.open("customer.dat");
    if (!inFile)
    {
        cout << "No customer file found." << endl;
    }
    else
    {   
        cout << "Customer file found..." << endl << endl;
        while (getline(inFile, acc[i].acctNum, '#'))
        {
            getline(inFile, acc[i].name, '#');
            getline(inFile, ctemp, '#');
            getline(inFile, stemp, '#');
            istringstream(ctemp) >> acc[i].cBal;
            istringstream(stemp) >> acc[i].sBal;
            i++;
            numCustomers++;
        }
        cout << "Number of customers found in file: " << numCustomers << endl;
    }
    system("pause");
    system("cls");
    inFile.close();
    return numCustomers;
}
void saveCusomers(account acc[], int numCustomers)
{
    ofstream outFile;
    outFile.open("customer.dat");
    for (int i = 0; i < numCustomers; i++)
    {
        outFile << acc[i].acctNum;
        outFile << '#';
        outFile << acc[i].name;
        outFile << '#';
        outFile << acc[i].cBal;
        outFile << '#';
        outFile << acc[i].sBal;
        outFile << '#';
    }
    outFile << acc[numCustomers].acctNum;
    outFile << '#';
    outFile << acc[numCustomers].name;
    outFile << '#';
    outFile << acc[numCustomers].cBal;
    outFile << '#';
    outFile << acc[numCustomers].sBal;
    outFile << '#';
    cout << numCustomers + 1 << " accounts saved into the file." << endl;
    outFile.close();
}
int newCustomer(account acc[], int numCustomers)
{   
    cout << "New Customer" << endl;
    cout << "============" << endl;
    cout << "Enter account number: ";
    cin >> acc[numCustomers].acctNum;
    cout << "Enter name: ";
    cin.ignore();
    getline(cin, acc[numCustomers].name);
    acc[numCustomers].cBal = 0;
    acc[numCustomers].sBal = 0;
    numCustomers++;
    return numCustomers;
}
int deleteCustomer(account[], int)
{
    return 0;
}
int findCustomer(account acc[], int numCustomers)
{
    string target;
    cout << "Enter the account number you are looking for: ";
    cin >> target;
    for (int i = 0; i < numCustomers; i++)
    {
        if (acc[i].acctNum == target)
        {
            return i;
        }
    }
    return -1;
}

如何更改此设置,以便在程序运行时添加多个帐户,以及如何使程序在未添加任何内容时不会将地址保存到我的文件中?我还想知道为什么这些地址被这样保存。

main()中,您loadCustomers() acc[],并在局部变量numAcc中记录客户数量。 在main()结束时,您将saveCustomers() acc[]中的numAcc

1. 您的保存功能错误:

在你的循环for (int i = 0; i < numCustomers; i++) { ...}中,你首先把每个客户从0写到numAcc-1。 这是正确的。

但之后你又写了一个额外的账户,偏移量为 numAcc . 所以你写的账户比你拥有的多一个。 并且此帐户的值没有初始化,这解释了您看到的奇怪数字。

即使未添加帐户,也可以执行此操作。你知道你正在这样做,因为你已经写了cout << numCustomers + 1 << " accounts saved into the file."

更正: 只需在循环中保存您拥有的帐户即可。

2.您的添加功能是正确的,但您没有按计划使用它:

当您添加具有newCustomer()的客户时,您的函数会递增其局部变量,以便它知道还有一个客户。 您的函数正确返回此新数字。

不幸的是,在 main(( 中,你不对这个返回值做任何事情,所以 numAcc 保持不变。

更正

:更正main()中的以下语句:

    if (selection == 1)
    {
        numAcc = newCustomer(acc, numAcc);   // update the number of accounts
    }

3. 其他备注:

如果 acc[] 数组已满,则无需签入newCustomers()。 如果您添加第 21 个帐户,这将是一个段错误!

这同样适用于加载。 您无法确定没有人使用文本编辑器向文件添加行。因此,也存在阅读更多行的风险,您的 arry 中有空间。

deleteCustomer()什么都没有发生,我想你还没有写过。 认为此功能可以更改帐户数量,就像newCustomer()一样。

由于大多数银行都有二十多个客户,我认为这是学校的练习。 我不知道您是否被允许这样做,但我强烈建议用vectors替换数组。