无法在 c++ 中创建对象数组

Cant create array of objects in c++

本文关键字:创建对象 数组 c++      更新时间:2023-10-16
#include <iostream>
using namespace std;
class Bank
{
private:
    char account_holder[50];
    int accnum;
    int balance;
    int dep_amount;
    int with_amount;
public:
   void getdata();
   void putdata();
   void deposit();
   void withdraw();
 };
void Bank::getdata()
{
cout << "Enter the account holders name : " << endl;
cin >> account_holder;
cout << "Enter the account number : " << endl;
cin >> accnum;
cout << "Enter the balance in your account : " << endl;
cin >> balance;
}
void Bank::putdata()
{
cout << "The account holders name is : " << account_holder << endl;
cout << "The account number is : " << accnum << endl;
cout << "The balance in your account is : " << balance << endl;
cout << endl;
}
void Bank::deposit()
{

cout << "Enter the amount to be deposited : " << endl;
cin >> dep_amount;
balance = balance + dep_amount;
cout << "Your current balance is : " << balance << endl;
}
void Bank::withdraw()
{
cout << "Enter the amount to be withdrawn : " << endl;
cin >> with_amount;
balance = balance - with_amount;
cout << "Your current balance is : " << balance << endl;
}
int main(){
Bank ram[5];
int ch, a, n, acc;
cout << "How you account holders you want to add : " << endl;
cin >> n;
do
{
    cout << "Enter 1.To insert data" << endl;
    cout << "Enter 2.To display data" << endl;
    cout << "Enter 3.To deposit amount" << endl;
    cout << "Enter 4.To withdraw amount" << endl;
    cout << "Enter your choice : " << endl;
    cin >> ch;
    switch (ch)
    {
    case 1:
        for (int i = 0; i < n;i++)
        ram[i].getdata();
        break;
    case 2:
        for (int i = 0; i < n; i++)
        ram[i].putdata();
        break;
    case 3:
        cout << "Enter the account you want to deposit money into " << endl;
        cin >> acc;
        for (int i = 0; i < n; i++)
        ram[acc].deposit();
        break;
    case 4:
        for (int i = 0; i < n; i++)
        ram[i].withdraw();
        break;
    }
    cout << "Enter 6. To Continue" << endl;
    cin >> a;
} while (a == 6);
return 0;
}

我正在使用此代码,我的问题是当我想存入或提取一些金额时,我想从用户那里获取帐号,然后仅从该对象中存入/提取金额。如何使用从用户处获取的帐号输入该对象?请帮忙。

不幸的是,

你的代码和逻辑有一些问题。让我们一一解决它们:

1-您正在使用char数组来保留帐户持有人姓名。如果你正在编程C++你应该在几乎所有情况下都使用std::string

2-您在Bank的公共方法中使用非常规名称,这充其量是一个坏习惯。

更具体地说,getdata() 是一个糟糕的方法名称,因为以"get"开头的 mehtods 通常应该保留给返回属于类实例的单个字段的方法。例如int getAccountNumber() 你的getdata()应该是fillInData()跟着我?

3-您在main中使用了一系列Bank,以持有帐户数量。虽然这是可能的,但远非理想。你应该努力在有意义的时候使用std::vector(比如这里(。为什么裸数组不好?因为如果你使用数组,数组的大小必须在编译时知道,这意味着你不能在程序运行时增加帐户的数量。如果您在堆栈上声明一个大数组,您可能有很多空间,但您正在浪费宝贵的堆栈空间。

4-您用于"输入循环"的逻辑混乱且难以导航。可以显著改进设计,以提高代码的可读性和可维护性。考虑这样一个事实,即您声明并读取了int n;但您从未在程序中使用它。

5-来自未声明变量(如i(的编译时错误

6-逻辑和语义不正确的程序行为:您遍历所有记录的for循环并提取/存入所有记录,而不是选择您需要的记录。

7-没有任何形式的边界检查。要求坏事发生。

我给你一个最低限度调整的代码,这是有意义的。请注意,这仍然不是完成此任务的理想方法,但至少它没有语法和语义错误:

int Bank::getAccountNumber()
{
    return this->accnum;
}
int getIndexByAccountNumber(Bank allAccounts[], int size, int accountNumber) //returns index or -1 in case account number is not found
{
    for(int i=0; i<size; ++i)
    {
        if (allAccounts[i].getAccountNumber()==accountNumber) return i;
    }
return -1;
}

int main(){
    const int NumberOfAccounts=5;
    Bank ram[NumberOfAccounts];
    int nextIndex=0;
    int ch, a, acc;
    while(true)
    {
        cout << "Enter 1.To insert data for a new account" << endl;
        cout << "Enter 2.To display data of all existing accounts" << endl;
        cout << "Enter 3.To deposit to an existing account" << endl;
        cout << "Enter 4.To withdraw from an existing account" << endl;
        cout << "Enter 5.To terminate program" << endl;
        cout << "Enter your choice : " << endl;
        cin >> ch;
        if(ch==1)
        {
            if(nextIndex>=NumberOfAccounts)
            {
                cout<<"error: you have space for only "<<NumberOfAccounts<<" accounts! terminating program";
                break;//breaks out of while loop
            }
          ram[nextIndex].getdata();//gets all fields for the account
          nextIndex++;
        }
        else if(ch==2)
        {
         cout << "showing information for all accounts: " << endl;
         for (int i = 0; i < nextIndex; i++) ram[i].putdata();
            cout<<"nn";
        }
        else if(ch==3)
        {
            cout << "Enter the account you want to deposit money into " << endl;
            cin >> acc;
            int index = getIndexByAccountNumber(ram,NumberOfAccounts,acc);
            if(index==-1)
            {
                cout<<"the account number you entered could not be found, terminating program!n";
                break;//breaks out of while loop
            }
             ram[index].deposit();
        }
        else if(ch==4)
        {
            cout << "Enter the account you want to withdraw from " << endl;
            cin >> acc;
            int index= getIndexByAccountNumber(ram,NumberOfAccounts,acc);
            if(index==-1)
            {
                cout<<"the account number you entered could not be found, terminating program!n";
                break;//breaks out of while loop
            }
            ram[index].withdraw();
        }
        else if(ch==5)
        {
            break;
        }
        else
        {
            cout<<"you entered invalid choicen";
        }
    }//end of while loop
return 0;
}
    cout << "Enter the account you want to deposit money into " << endl;
    cin >> acc;
    for (int i = 0; i < n; i++)
    ram[acc].deposit();
    break;

这是你的问题 - 错误的索引变量。尝试"i">

在您的案例中,3 您使用的迭代器与其他迭代器不同。 这是设计使然还是什么? 我错过了使用acc而不是i的目的. 这可能是你的问题,除非我忽略了你使用它的目的。