My C++ List[ArrayImplementation] Error

My C++ List[ArrayImplementation] Error

本文关键字:Error ArrayImplementation C++ List My      更新时间:2023-10-16

当列表为空或部分索引仍为空时,它包含默认元素0和1…当我要存款、取款、编辑或删除时,它接受0和1的账号,即使我没有创建。它应该输出accountnumber not Found…对不起,我的英语不好,这是我的代码,希望你能明白我的意思。

 #include<iostream>
 #include<cstdlib>
 #include<conio.h>
 using namespace std;
 class unsortedList{
public:
    unsortedList();
    void getLength() const;
    void insertItem(int item,string name,double balances);
    void deleteItemAt();
    void replaceItem();
    void makeEmpty();
    bool isFull()const;
    void show();
    void deposit();
    void withdraw();
private:
    int length;
    int info[10];
    string names[10];
    double balance[10];
   };
   unsortedList::unsortedList(){
length=0;
   }
   bool unsortedList::isFull() const{
return(length==10);
   }
   void unsortedList::insertItem(int item,string name,double balances){
if(isFull()){
    return;
}
names[length]=name;
info[length]=item;
balance[length]=balances;
length++;
    }
    void unsortedList::deleteItemAt(){
int location,n,found;
cout<<"Enter the Account number: ";
cin>>n;
    for(int i=0;i<10;i++){
    if(info[i]==n){
    found =1;
    location =i;
    }}
    if(found==1){
    for(int i=location;i<length-1;i++)
    info[i]=info[i+1];
    length--;
    cout<<"Account Deleted";}
    else{
    cout<<"Account Number Not Found";    }
getch();
    }
    void unsortedList::show(){
for(int i=0;i<length;i++){
    cout<<"Account name: "<<names[i]<<endl;
    cout<<"Account number: "<<info[i]<<endl;
    cout<<"Account balance: "<<balance[i]<<endl;
}getch();
    }
    void unsortedList::makeEmpty(){
length=0;
    }
    void unsortedList::replaceItem(){
int item,location,n,found;
string newName;
double balances;
cout<<"Enter the Account number: ";
cin>>n;
    for(int i=0;i<10;i++){
    if(info[i]==n){
    found =1;
    location =i;
    }}
    if(found==1){
    cout<<"Account Number Found!"<<endl;
    cout<<"Account Name: "<<names[location]<<endl;
    cout<<"Account Balance Amount: "<<balance[location]<<endl;
    cout<<"Enter new Account name: ";
    cin>>newName;
    cout<<"Enter new Balance Amount: ";
    cin>>balances;
    names[location]=newName;
    balance[location]=balances;
    }
    else{
    cout<<"Account Number Not Found!";    }
    getch();
         }
     void unsortedList::deposit(){
 int location,n,found;
 double deposit;
 cout<<"Enter the Account number: ";
 cin>>n;
     for(int i=0;i<10;i++){
     if(info[i]==n){
    found =1;
    location =i;
    }}
    if(found==1){
    cout<<"Account number Found!"<<endl;
    cout<<"Account Name: "<<names[location]<<endl;
    cout<<"Account Balance: "<<balance[location]<<endl;
    cout<<"Enter Amount to Deposit ";
    cin>>deposit;
    balance[location]+=deposit;
    }
    else{
    cout<<"Account Number Not Found!";    }
   getch();
       }
      void unsortedList::withdraw(){
  int location,n,found;
  double deposit;
  cout<<"Enter the Account number: ";
  cin>>n;
      for(int i=0;i<10;i++){
      if(info[i]==n){
    found =1;
    location =i;
    }}
    if(found==1){
    cout<<"Account number Found!"<<endl;
    cout<<"Account Name: "<<names[location]<<endl;
    cout<<"Account Balance: "<<balance[location]<<endl;
    cout<<"Enter Amount to Withdraw: ";
    cin>>deposit;
    balance[location]-=deposit;
    }
    else{
    cout<<"Account Number Not Found!";    }
    getch();
        }

    int main(){
unsortedList usl;
int num,choice,del,rep;
string name,newName;
double balance;
do{
system("cls");
cout<<"nMenu"<<endl;
cout<<"1.Register"<<endl;
cout<<"2.Deposit"<<endl;
cout<<"3.WithDraw"<<endl;
cout<<"4.Delete An Account"<<endl;
cout<<"5.Show All Accounts"<<endl;
cout<<"6.Edit An Account"<<endl;
cout<<"7.Delete All Accounts"<<endl;
cout<<"8.Exit"<<endl;
cin>>choice;
switch(choice){
    case 1:
        system("cls");
        cout<<"Enter Account name: ";
        cin>>name;
        cout<<"Enter Account number: ";
        cin>>num;
        cout<<"Enter the Initial Amount to be Deposited: ";
        cin>>balance;
        usl.insertItem(num,name,balance);
        break;
    case 2:
        system("cls");
        usl.deposit();
        break;
    case 3:
        system("cls");
        usl.withdraw();
        break;
    case 4:
        system("cls");
        usl.deleteItemAt();
        break;
    case 5:
        system("cls");
        usl.show();
        break;
    case 6:
        system("cls");
        usl.replaceItem();
        break;
    case 7:
        usl.makeEmpty();
        break;
  }}while(choice!=8);
  return 0;
      }

,当列表为空或索引的某些部分仍为空时包含默认元素0和1

您将数组声明为类的成员,但不初始化它们,因此期望在其中有任何内容。比如0或1或非常大的值。下面是初始化它的方法:

memset(info, 0, 10*sizeof(int)); // Consider putting 10 in a const.

你应该只循环到长度,而不是静态数组大小

cout<<"Enter the Account number: ";
cin>>n;
    for(int i=0;i<length;i++){

另一个问题:

int location,n,found;

你也不初始化它们,然后,

if(found==1){

你该庆幸你没有打

if(found)

否则你会得到很多假阳性,因为found是初始化的,它很可能永远不会像你想象的那样为0。

相关文章: