c - 从结构数组中删除元素
问题描述
我的任务是创建一个库存管理程序,用户可以在其中添加项目、编辑项目或删除项目。我几乎完成了代码,我只是在删除一个项目时遇到了问题,特别是从结构数组中删除一个元素。
我已经寻找过类似的问题并尝试了建议的解决方案。我在代码中尝试的方法是通过将数组旁边的元素向下移动 1 来删除数组中的元素。程序运行并且假设代码应该可以工作,但是每次我运行程序并输入“删除项目”选项时,程序停止(它不只是退出,它说“程序停止工作”意味着我违反了规则或其他东西)。我在想我可能超出了数组大小或其他东西,但我无法指出问题到底是什么。是否禁止移动结构数组中的元素,还是只是我的代码?请帮忙。
这是我的结构的代码。
struct details {
char name[30];
double price;
int code;
int qty;
};
details item[SIZE];
这是主要功能:
int main (){
int choice; //gets the choice of user from the menu
bool condition = 1; //loops the menu
int count=0; //counts the number of items in the inventory
do{
printheader(); //prints the title of the program
printmenu(); //prints the menu (list of commands)
scanf("%d", &choice);
switch(choice){
case 1: system("cls");
AddItem(count); //function in adding record
count++; //increments every time a new item is added
system("PAUSE");
system("cls");
break;
case 2: system("cls");
EditItem(count); //function in editing a record
system("PAUSE");
system("cls");
break;
case 3: system("cls");
count = DeleteItem(count); //function in deleting a record
system("PAUSE");
system("cls");
break;
case 4: system("cls");
//ViewItem(); //function in viewing a record
system("PAUSE");
system("cls");
break;
case 5: system("cls");
DisplayInventory(count); //function in displaying inventory
system("PAUSE");
system("cls");
break;
case 6: system("cls");
SaveFile(count); //function in saving the records to a file
system("PAUSE");
system("cls");
break;
case 7: system("cls");
count = LoadFile(); //function in loading the records from a saved file
system("PAUSE");
system("cls");
break;
case 8: printf("\nThank you!");
exit(0); //ends the program
break;
default: printf("\nInvalid Input!\n");
getch();
system("cls");
}
}while(condition = 1);
return 0;
}
这是 DeleteItem() 函数。它接受 n ,这是项目/记录的数量。
int DeleteItem (int n){
printheader();
int i=0, code, pos;
bool cont = true;
printf("\nEnter the code of the item you want to delete: ");
scanf("%d", code);
do{
if(code==item[i].code){
for (pos=i; pos<(n-1); pos++){
// item[pos].name = item[pos+1].name; //this basically deletes the i'th element and shifts the remaining ones
item[pos].price = item[pos+1].price;
item[pos].code = item[pos+1].code;
item[pos].qty = item[pos+1].qty;
}
printf("\nItem deleted!");
cont = false; //loop ends once the input of the user matches the data in the inventory
}
if(i==n){
printf("\nCode not found!\n\n");
cont = false; //loop ends when there are no matches
}
i++;
}while(cont);
}
当在程序中输入删除项目选项时,程序会要求用户输入项目的代码。然后程序扫描结构数组以查找与代码匹配的元素 (item[i].code)。理想情况下,如果代码匹配,程序应该删除元素并移动其他元素。但是,发生的事情是程序崩溃并停止。我需要帮助找出问题所在。非常感谢!
编辑 DeleteItem 功能:
int DeleteItem (int n){
printheader();
int i=0, code, pos;
//bool cont = true;
printf("\nEnter the code of the item you want to delete: ");
scanf("%d", code);
while(i<n){
if(code==item[i].code){
for (pos=i; pos<(n-1); pos++){
item[pos] = item[pos+1];
}
printf("\nItem deleted!");
break;; //loop ends once the input of the user matches the data in the inventory
}
i++;
if(i==(n-1)){
printf("\nCode not found!\n\n");
break;; //loop ends when there are no matches
}
}
return (n-1);
}
我是 C 的新手,所以如果您看到一些有问题的代码,我真的很抱歉。我在做这个工作。
解决方案
一个问题是您的检查i == n
是在您使用i
. 要解决这个问题,您应该在检查i
之前增加。喜欢:
i++; // Increment first
if(i==n){ // then check
printf("\nCode not found!\n\n");
cont = false; //loop ends when there are no matches
}
另一个问题是你不处理n
零的情况。一般来说,我认为 awhile(i < n) { ... };
比 a 更好do { ...} while(...);
。
另请注意此代码(当前已注释掉)是错误的:
// item[pos].name = item[pos+1].name;
您不能使用赋值(即=
)复制字符串。你需要使用strcpy
我也看不到count
删除项目的任何更新。我想那是错误......我认为count
必须减少。
最后,我没有看到该函数返回任何值。这也是一个错误,因为您将函数定义为返回int
.
一张纸条...
使用像cont
终止while
循环这样的标志可以正常工作,因此不是错误。但是,您实际上并不需要标志。您可以使用以下任一方法break
:
do{
...
...
if(i==n){
printf("\nCode not found!\n\n");
break; //loop ends when there are no matches
}
i++;
}while(1);
或者干脆做 areturn
因为函数没有更多的事情要做。
编辑
OP 已发布代码的第二次修订。此编辑解决了第二次修订。
第二个版本的一个问题是代码总是返回n-1
。即使没有找到“代码”,也会这样做。那是一个错误。
这if(i==(n-1)){
也是错误的,因为这意味着n-i
永远不会测试项目编号。
尝试这样的事情:
int DeleteItem (int n){
printheader();
int i=0, code, pos;
printf("\nEnter the code of the item you want to delete: ");
scanf("%d", code);
while(i<n){
if(code==item[i].code){
for (pos=i; pos<(n-1); pos++){
item[pos] = item[pos+1];
}
printf("\nItem deleted!");
return n-1; // End the function and return n-1 as an item was deleted
}
i++;
}
printf("\nCode not found!\n\n");
return n; // End the function and return n as no item was deleted
}
顺便说一句:您应该始终检查返回的值scanf