C++ 我的C++;列表[ArrayImplementation]错误
当列表为空或某些索引仍然为空时,它包含一个默认元素0和1…当我准备存款、取款、编辑或删除时,它接受帐户号0和1,即使我没有创建。它应该输出找不到的帐户号…抱歉4我的英语不好,这是我的代码希望你明白我的意思..tnxC++ 我的C++;列表[ArrayImplementation]错误,c++,c++11,C++,C++11,当列表为空或某些索引仍然为空时,它包含一个默认元素0和1…当我准备存款、取款、编辑或删除时,它接受帐户号0和1,即使我没有创建。它应该输出找不到的帐户号…抱歉4我的英语不好,这是我的代码希望你明白我的意思..tnx #include<iostream> #include<cstdlib> #include<conio.h> using namespace std; class unsortedList{ public: unsortedLi
#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;
}
#包括
#包括
#包括
使用名称空间std;
类未分类列表{
公众:
未分类列表();
void getLength()常量;
无效插入项(整数项、字符串名称、双余额);
void deleteItemAt();
void replaceItem();
void makeEmpty();
bool isFull()常量;
void show();
无效存款();
无效撤回();
私人:
整数长度;
国际信息[10];
字符串名[10];
双平衡[10];
};
unsortedList::unsortedList(){
长度=0;
}
bool unsortedList::isFull()常量{
返回(长度==10);
}
void unsortedList::insertItem(int项、字符串名称、双余额){
如果(isFull()){
回来
}
名称[长度]=名称;
信息[长度]=项目;
天平[长度]=天平;
长度++;
}
void unsortedList::deleteItemAt(){
int位置,n,找到;
coutn;
对于(int i=0;i
当列表为空或某些索引仍然为空时,它将被删除
包含默认元素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++){
你也不需要初始化这些
if(found==1){
你很幸运没有打字
if(found)
否则,由于found
被单位化,你会得到很多假阳性,它很可能永远不会像你想象的那样为0。一个问题可能是,当你删除一个项目时,你只移动info
字段,而不是name
和balance
字段。你可能还想更好地格式化代码,对吗具体来说,压痕是坏的。我把int集代码放在int INFO(10)之后,但是我得到了这个错误:[错误] INFO不是一个类型[错误]预期的标识符,在数字常量[错误]之前,期望“,”或“…”在数字常数[Orrist] ISO C++禁止声明没有类型的MeMeSt [ -fPrime]。顺便说一句,我尝试了uve所说的我启动了所有变量和循环顺便说一句,我的最后一个问题是我如何删除一个数组元素,与此不同,我的删除是假的。用户可以看到,我只是删除了数组长度,这就是为什么它没有在屏幕上输出,但事实是它仍然在那里。最后我移动了它。在这段代码中(i)地点;i@user3052514MeMeSET部分需要,但在构造函数中。为了实现删除,我只将信息变量放到-1,然后在需要添加另一个时再重用这个点。如果它帮助了你,请考虑支持和接受答案。
if(found)