C++ 保存c++;向文件和问题添加新元素的结构数组

C++ 保存c++;向文件和问题添加新元素的结构数组,c++,arrays,C++,Arrays,我的程序目前有两个主要问题。第一个问题是,在程序运行时,我无法将多个帐户添加到程序中(在添加另一个帐户之前,我需要关闭它并重新打开)。第二个问题是,当我没有向程序地址添加任何帐户时,保存到程序中,这就是我没有向程序添加任何帐户时文件的外观 123#John Smith#0#0###-1.07374e+008#-1.07374e+008# 文件的第一部分是正确的,但是地址来自内存中的其他地方。这就是我的代码的样子 #include <iostream> #include <i

我的程序目前有两个主要问题。第一个问题是,在程序运行时,我无法将多个帐户添加到程序中(在添加另一个帐户之前,我需要关闭它并重新打开)。第二个问题是,当我没有向程序地址添加任何帐户时,保存到程序中,这就是我没有向程序添加任何帐户时文件的外观

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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构帐户
{
串接;
字符串名;
浮动cBal;
浮球;
};
int菜单();
char子菜单();
客户(账户[]);
作废储蓄客户(账户[],int);
int新客户(账户[],int);
int删除客户(账户[],int);
int findCustomer(账户[],int);
无效存款(账户[],int);
作废取款(账户[],int);
无效余额(账户[],int);
无效银行余额(账户[],int);
int main()
{
会计科目[20];
int选择;
int numac=0;
整数搜索;
numAcc=装载客户(acc);
做
{
选择=菜单();
如果(选择==1)
{
新客户(acc、numAcc);
}
else if(选择==2)
{
删除客户(acc、numAcc);
}
else if(选择==3)
{
搜索=查找客户(acc、numAcc);
如果(搜索==-1)
{
在
main()
中,您在
acc[]
loadCustomers()
并在本地变量
numAcc
中记录客户数量。在
main()
的末尾,您可以
saveCustomers()
acc[]
中保存
numAcc

1.您的保存功能错误:

在(inti=0;i
的循环
中,首先将每个客户从0写到
numAcc-1
。这是正确的

但是之后你又写了一个额外的帐户,偏移量为
numAcc
。所以你写的帐户比你现在多了一个。这个帐户的值没有初始化,这就解释了你看到的奇怪数字


即使没有添加帐户,您也可以这样做。而且您知道您正在这样做,因为您已经编写了
。您是否尝试过使用调试器来逐步检查代码并查看出错的地方?
    if (selection == 1)
    {
        numAcc = newCustomer(acc, numAcc);   // update the number of accounts
    }