添加到数组

时间:2016-08-15 01:02:50

标签: c++ arrays

我很难搞清楚这一点。到目前为止,我的项目中的其他所有内容都在工作,但是当我尝试添加到我的对象数组时,它会崩溃。我得到一个无法读取字符串的错误。这是消息,我尝试过很多东西。这是错误,它在SetNewAccountInfo()函数输入后崩溃。我是编程新手,希望这段代码在这里格式化。这是一个学校项目,所以我不是要求答案,也许只是解释为什么我无法以外行的方式添加到我的阵列中。指针等等到目前为止令人困惑。

static _Elem *__CLRCALL_OR_CDECL copy(_Elem *_First1, const _Elem *_First2, size_t _Count)
{   // copy [_First2, _First2 + _Count) to [_First1, ...)
    **return (_Count == 0 ? _First1** : (_Elem *)_CSTD memcpy(_First1, _First2, _Count));
}
// TestClassC.cpp : main project file.

#include "stdafx.h"
#include "BankClassType.h"

#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>


using namespace std;
void MainMenu(BankClassType bo[],int num, int cAcct);
void AcctOptionsMenu(BankClassType bo[], int num, int cAcct);
int SetNewAccountInfo();
void PrintAccountInfo();
void AccountDeposit();
void AccountWithdrawal();
int main()
{
    const int num = 4;

    BankClassType *bo = new BankClassType[num];
    BankClassType b;
    BankClassType bo0("12345", 'P', "Gates", "Bill", 175253.99, 6875250.23);
    BankClassType bo1("12346", 'R', "Jones", "Tommy", 845.21, 2700.00);
    BankClassType bo2("12347", 'P', "Asimov", "Isaac", 300.67, 14750.29);
    bo[0] = bo0;
    bo[1] = bo1;
    bo[2] = bo2;
    int cAcct = 3;
    b.MainMenu(bo, num, cAcct);

    _getch();
    return 0;
}
//BankClassType.h File
#pragma once
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
#ifndef BankClassType_h
#define BankClassType_h

using namespace std;

class BankClassType
{
private:
    string AccountNumber;
    string FirstName;
    string LastName;
    double CheckingBalance;
    double SavingsBalance;
    char AccountType;
public:
    BankClassType()
    {
        FirstName = "";
        LastName = "";
        AccountNumber = "";
        AccountType = 'p';
        CheckingBalance = 0;
        SavingsBalance = 0;
    }
    BankClassType(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal);

    //~BankClassType();
    void SetNewAccountInfo(BankClassType bo[], int num, int cAcct);
    void PrintAccountInfo();
    void AccountDeposit(BankClassType bo[], int number, int num, int cAcct);
    void AccountWithdrawal(BankClassType bo[], int number, int num, int cAcct);
    void AcctOptionsMenu(BankClassType bo[], int num, int cAcct);
    void MainMenu(BankClassType bo[], int num, int cAcct);

    //void ExistingAccounts(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal);
    string getAccountNumber()
    {
        return AccountNumber;
    }
    char getAccountType()
    {
        return AccountType;
    }
    double getCheckingBalance(double amount)
    {
        CheckingBalance = CheckingBalance + amount;
        return CheckingBalance;
    }
    double getSavingsBalance(double amount)
    {
        SavingsBalance = SavingsBalance + amount;
        return SavingsBalance;
    }
    double getWithdrawalCheckingBalance(double amount)
    {
        CheckingBalance = CheckingBalance - amount;
        return CheckingBalance;
    }
    double getWithdrawalSavingsBalance(double amount)
    {
        SavingsBalance = SavingsBalance - amount;
        return SavingsBalance;
    }
    string getFirstName()
    {
        return FirstName;
    }
    string getLastName()
    {
        return LastName;
    }
};


#endif
//BankClassImp.cpp
#include "stdafx.h"
#include "BankClassType.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>


using namespace std;

BankClassType::BankClassType(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal)
{
    AccountNumber = acctNumber;
    AccountType = acctType;
    LastName = lname;
    FirstName = fname;
    CheckingBalance = cBal;
    SavingsBalance = sBal;
    /*cout << "\nName: " << LastName << ", " << FirstName;
    cout << "\nAccountNumber: " << AccountType << AccountNumber;
    cout << "\nChecking Balance: " << CheckingBalance;
    cout << "\nSavings Balance: " << SavingsBalance<<endl;
    */
}

/*BankClassType::~BankClassType()
{
delete[] FirstName;
delete[] LastName;
delete[] AccountNumber;
delete[] AccountType;
}*/

void BankClassType::SetNewAccountInfo(BankClassType bo[], int num, int cAcct)
{
    //BankClassType  * bt = new BankClassType;
    BankClassType c;

    string fName;
    string acctNum;
    string lName;
    char accType;
    double cBal, sBal;
    if (cAcct >= 5)
    {
        "Sorry the accounts are currently full, press any key to return to main menu: ";
        c.MainMenu(bo, num, cAcct);
        _getch();
    }
    else
    cout << "\nEnter the 5 digit account number: ";   cin >> acctNum;
    cout << "\nEnter the account type (p) or (r): ";      cin >> accType;
    cout << "\nEnter the first name: ";    cin >> fName;
    cout << "\nEnter the last name: ";    cin >> lName;


    cout << "\nEnter the checking balance: ";    cin >> cBal;
    cout << "\nEnter the savings balance: ";    cin >> sBal;
    BankClassType bo3(acctNum, accType, lName, fName, cBal, sBal);
    bo[cAcct + 1] = bo3;
}


void BankClassType::PrintAccountInfo()
{
    cout << "Name: " << LastName << ", " << FirstName;
    cout << "\nAccountNumber: " << AccountType << AccountNumber;
    cout << "\nChecking Balance: " << CheckingBalance;
    cout << "\nSavings Balance: " << SavingsBalance << endl << endl;

}

void BankClassType::AccountDeposit(BankClassType bo[], int number, int num, int cAcct)
{
    int choice;
    BankClassType b;
    cout << "\nEnter 1 for checking or 2 for savings: ";
    cin >> choice;
    if (choice == 1)
    {
        double amount;
        cout << "Enter amount to deposit into checking: " << endl;
        cin >> amount;
        bo[number].getCheckingBalance(amount);
        cout << "You added $" << amount << " to checking account number " << bo[number].getAccountNumber();
        _getch();
        b.MainMenu(bo, num, cAcct);
    }
    else if (choice == 2)
    {
        double amount;
        cout << "Enter amount to deposit into savings: " << endl;
        cin >> amount;
        bo[number].getSavingsBalance(amount);
        cout << "You added $" << amount << " to savings number" << bo[number].getAccountNumber();
        _getch();
        b.MainMenu(bo, num, cAcct);
    }
    else
    {
        cout << "Incorrect Input, try again: ";
        _getch();
        AccountDeposit(bo, number, num, cAcct);
    }
}

void BankClassType::AccountWithdrawal(BankClassType bo[], int number, int num, int cAcct)
{
    int choice;
    BankClassType b;
    cout << "\nEnter 1 for checking or 2 for savings: ";
    cin >> choice;
    if (choice == 1)
    {
        double amount;
        cout << "Enter amount to withdrawal from checking: " << endl;
        cin >> amount;
        bo[number].getWithdrawalCheckingBalance(amount);
        cout << "You withdrew $" << amount << " from checking account number " << bo[number].getAccountNumber();
        cout << "\nPress any key to return to main menu: ";
        _getch();
        b.MainMenu(bo, num, cAcct);
    }
    else if (choice == 2)
    {
        double amount;
        cout << "Enter amount withdrawal from savings: " << endl;
        cin >> amount;
        bo[number].getWithdrawalSavingsBalance(amount);
        cout << "You withdrew $" << amount << " from savings account number " << bo[number].getAccountNumber();
        _getch();
        b.MainMenu(bo, num, cAcct);
    }
    else
    {
        cout << "Incorrect Input, try again";
        _getch();
        AccountDeposit(bo, number, num, cAcct);
    }
}

void BankClassType::MainMenu(BankClassType bo[], int num, int cAcct)
{
    cout << "\nWelcome, here are the current accounts on file:\n" << endl;
    for (int i = 0; i < cAcct; i++)
    {
        cout << i + 1 << "--";
        bo[i].PrintAccountInfo();
    }
    int choice;
    BankClassType b;

    cout << "Enter number (1-5) of account you wish to make changes\n Enter 6 to add an account\nEnter 7 to exit\n --->";
    cin >> choice;
    if (choice < 6)
        b.AcctOptionsMenu(bo, num, cAcct);
    else if (choice == 6)
    {
        bo[cAcct + 1].SetNewAccountInfo(bo, num, cAcct);
        cout << "You have successfully added an account!" << endl;
        _getch();
        cAcct++;
        b.MainMenu(bo, num, cAcct);
    }
    else if (choice == 7)
    {
        cout << "You have exited, press any key to continue: ";
        _getch();
        system("cls");
    }
    else if(choice > 7)
    {
        cout << "Not a valid input, press a key to try again: "; 
        _getch(); 
        system("cls"); 
        MainMenu(bo, num, cAcct);
    }
}
void BankClassType::AcctOptionsMenu(BankClassType bo[], int num, int cAcct)
{
    char choice;
    int check = 0;
    int count = 0;
    int number;
    BankClassType b;
    while (check == 0) {
        cout << "\t\t\n\n" << "Main Menu";
        cout << "\t\n\n" << "Select by letter:";
        cout << "\t\n" << "(d) - Deposits";
        cout << "\t\n" << "(w) - Withdrawals";
        cout << "\t\n" << "(s) - Show Account Information.";
        cout << "\t\n" << "(q) - Quit Program.\n\n";
        cout << "\t" << "Choice: ";
        choice = _getche();
        switch (choice) {
        case 'd':
        case 'D':
            system("cls");
            cout << "\nChoose Account:\n ";
            for (int i = 0; i < cAcct; i++) cout << i +1 << "---" << bo[i].getAccountNumber()<< "\n\n" ;
            cin >> number;
            bo[number].AccountDeposit(bo,number, num, cAcct);
            system("cls");
            break;

        case 'w':
        case 'W':
            system("cls");
            cout << "\nChoose Account:\n ";
            for (int i = 0; i < cAcct; i++) cout << i + 1<< "---" << bo[i].getAccountNumber() << "\n\n";
            cin >> number;
            bo[number].AccountWithdrawal(bo, number, num, cAcct);
            system("cls");
            break;

        case 's':
        case 'S':
            system("cls");
            cout << "\nChoose Account:\n ";
            for (int i = 0; i < cAcct; i++) cout << i + 1 << "---" << bo[i].getAccountNumber() << "\n\n";
            cin >> number;
            bo[number].PrintAccountInfo();
            _getche();
            system("cls");
            break;

        case 'q':
        case 'Q':
            check = 1;
            break;

        default:
            cout << "\nInvalid selection. Press a key to return to main menu.";
            _getche();
        }

        if (check == 1)
            break;

    }
}

1 个答案:

答案 0 :(得分:1)

看起来在main中分配的数组太短了。你分配一个4个元素的块,传递'3'作为cAcct,然后bo [cAcct + 1] = bo3。索引将是4,即一次通过数组的结尾。结果未定义且可能发生崩溃