不能在C++中创建对象数组
我使用的是这个代码,我的问题是,当我想存款或取款时,我想从用户那个里取账号,然后只从那个对象那个里存/取款。如何使用从用户处获取的帐号输入该对象?请帮忙不能在C++中创建对象数组,c++,arrays,object,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;
#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中,您使用的迭代器与其他迭代器不同。这是故意的还是怎么的?我失去了使用acc而不是I的目的。这可能是您的问题,除非我忽略了您使用它的目的。不幸的是,您的代码和逻辑有一些问题。让我们逐一解决这些问题: 1-您正在使用字符数组来保留帐户持有人名称。如果你正在编程C++,你应该在几乎所有的情况下使用STD::String。p> 2-你在银行的公共方法中使用非常规名称,这充其量只是一个坏习惯 更具体地说,getdata是一个糟糕的方法名,因为以get开头的方法通常应该保留给返回属于类实例的单个字段的方法。例如int getAccountNumber 你的getdata应该是fillInData跟我来 3-您正在使用一个银行阵列在您的主要目的是为了持有帐户的数量。虽然这是可能的,但还远远不够理想。当std::vector在这里有意义时,您应该尽量使用它。为什么裸阵列不好?因为如果使用数组,则必须在编译时知道数组的大小,这意味着在程序运行时不能增加帐户数。如果在堆栈上声明一个大数组,可能会有很多空间,但这是在浪费宝贵的堆栈空间 4-输入循环所采用的逻辑混乱且难以导航。可以显著改进设计,以提高代码的可读性和可维护性。请考虑声明和读取int n的事实;但是你从来没有在程序中使用它 5-来自未声明变量(如i)的编译时错误 6-逻辑和语义不正确的程序行为:您迭代所有记录的for循环,并在所有记录中提取/存放,而不是选择您需要的记录 7-无任何类型的边界检查。要求坏事发生 我给你一个最小调整的代码,这是有意义的。请注意,这仍然不是完成此任务的理想方式,但至少没有语法和语义错误:
cout << "Enter the account you want to deposit money into " << endl;
cin >> acc;
for (int i = 0; i < n; i++)
ram[acc].deposit();
break;
我不明白。你的问题与创建对象数组有什么关系?请用。另外,花点时间看看一个好问题。我想,n中的问题-你输入了多少帐户?你有5个。再看看这个字符串:ram[acc].deposit;你有我的循环,但使用acc作为索引。我使用acc作为数组的索引,以便我可以从该特定帐户提取或存款金额,因此我在其中传递变量acc,并从存款功能中的用户获取其值。不幸的是,你的代码中有许多错误。在考虑循环应该做什么之前,我建议不要写for循环。我使用acc作为数组的索引,这样我可以从特定帐户中提取或存入金额,所以我在其中传递变量acc,并从存款功能中的用户处获取其值–Fusionist 7分钟ago@Fusionist:所以你不需要那个循环,请注意,我添加了两个函数:一个是getAccountNumber,因为您的银行字段是私有的;另一个是帮助您根据帐户编号在数组中查找帐户索引的函数。我仍然鼓励您使用字符串和向量,而不是数组
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;
}