无法用c ++创建对象数组

时间:2015-12-29 15:45:35

标签: c++ arrays object

#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;
}

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

3 个答案:

答案 0 :(得分:0)

<rules>
   <rule name="BookingRedirect">
     <match url="Book" />
     <action type="Redirect" url="https://xyz.azurewebsites.net/abc" />
</rule>

这是你的问题 - 错误的索引变量。试试&#34;我&#34;

答案 1 :(得分:0)

在你的情况3中,你使用与其余迭代器不同的迭代器。这是设计还是什么?我错过了使用acc代替i的目的。这可能是你的问题,除非我忽略了你使用它的目的。

答案 2 :(得分:0)

不幸的是,您的代码和逻辑存在一些问题。让我们逐一解决:

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

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

更具体地说,getdata()是一个糟糕的方法名称,因为mehtods以&#34; get&#34;开头。通常应保留用于返回属于类实例的单个字段的方法。例如int getAccountNumber() 您的getdata()应该fillInData()关注我?

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

4-您用于&#34;输入循环的逻辑&#34;很乱,很难驾驭。可以显着改进设计,以提高代码的可读性和可维护性。考虑一下您声明并阅读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<<"\n\n";

        }

        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 choice\n";
        }
    }//end of while loop

return 0;
}