My C++ List[ArrayImplementation] Error
My C++ List[ArrayImplementation] Error
当列表为空或部分索引仍为空时,它包含默认元素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。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 为什么我会"error: expected expression"?
- CMake Error at modules/videoio/cmake/detect_ffmpeg.cmake:16
- 如果两个静态库使用相同的另一个静态库,如何避免"LNK2005 Already Defined error"?
- 在 Objective-C++ -> EXC_BAD_ACCESS Error 中使用"new"关键字