我做簡單的銀行程序,我的類名是銀行在對象數組中搜索操作?
它包括以下功能deposit()
,withdraw()
,show()
,getname()
,getbalance()
,默認的構造和參數的構造函數。
我試圖通過名稱和平衡在主程序中執行搜索操作。 但是,它並沒有顯示輸入的相應名稱或餘額的信息,而是顯示了賬戶持有人信息的完整列表。
請幫助解決此問題。
#include<iostream.h>
#include<string.h>
class bank
{
char name[20],acNo[20],typeOfAc[10];
double balance;
public:
bank()
{
strcpy(name,"NULL");
strcpy(acNo,"XXXX");
strcpy(typeOfAc,"XXXX");
balance=0;
}
bank(char nm[20],char acn[20],char tac[10])
{
strcpy(name,nm);
strcpy(acNo,acn);
strcpy(typeOfAc,tac);
balance=0;
}
double getbalance(){return balance;}
char* getname(){return name;}
void enter();
void deposit();
void withdraw();
void show();
};
void bank::deposit()
{
double d;
cout<<"\nEnter Amount To deposit:";
cin>>d;
cout<<"\n";
balance=balance+d;
cout<<"\nSuccessful Deposit.\n";
}
void bank::withdraw()
{
double w;
cout<<"\nEnter amount to withdraw:";
cin>>w;
cout<<"\n";
if((balance-w)<0)
{
cout<<"\nCan't withdraw!!";
cout<<"\nYour Balance:"<<getbalance();
return;
}
else
balance=balance-w;
cout<<"\nWithdrawal Successful.\n";
}
void bank::show()
{
cout<<"\nName:"<<getname();
cout<<"\nBalance:"<<getbalance();
}
void bank::enter()
{
cout<<"\nPlease enter the following info\n";
fflush(stdin);
cout<<"\nName:";
gets(name);
cout<<"\n";
cout<<"\nACcount Number:";
gets(acNo);
cout<<"\n";
cout<<"\nType of Account:";
gets(typeOfAc);
cout<<"\n";
}
int main()
{
int i;
double b1;
char n[20],nn[20];
bank b[10];
for(i=0;i<3;i++)
{
b[i].enter();
b[i].deposit();
}
//search by name
cout<<"\nEnter the name for search by name:";
fflush(stdin);
gets(n);
for(i=0;i<3;i++)
{
strcpy(nn,b[i].getname());
if(strcmp(nn,n)==0);
b[i].show();
}
cout<<"\nEnter the balance for search by balance:";
cin>>b1;
for(i=0;i<3;i++)
{
if(b[i].getbalance()==b1);
b[i].show();
}
return 0;
}
這聽起來像你可能需要學習如何使用調試器來逐步你的代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。進一步閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver
我也建議你使用'std: :string'而不是c-字符串。 – NathanOliver
'if(strcmp(nn,n)== 0);'結束if子句。無論條件的評估結果如何,以下行將被執行。從該行刪除';'。 'if(b [i] .getbalance()== b1);' –