C 从结构数组中删除元素
我的任务是创建一个库存管理程序,用户可以在其中添加项目、编辑项目或删除项目。我几乎完成了代码,只是在删除一个项时遇到了问题,特别是从结构数组中删除一个元素 我已经找到了类似的问题,并尝试了建议的解决方案。我在代码中尝试的方法是通过将数组中的元素下移1来删除该元素。程序运行,假设代码应该工作,但每次我运行程序并输入“删除项目”选项时,程序都会停止(它不仅仅是退出,它说“程序停止工作”,意思是我违反了规则或其他东西)。我想我可能超出了数组大小或其他什么,但我无法指出问题的确切原因。是禁止移动结构数组中的元素,还是只是我的代码?请帮忙 这是我的结构的代码C 从结构数组中删除元素,c,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;
}
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);
}
这是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);
}
intdeleteItem(intn){
printheader();
int i=0,代码,位置;
bool cont=真;
printf(“\n输入要删除的项目的代码:”;
scanf(“%d”,代码);
做{
如果(代码==项目[i]。代码){
对于(pos=i;pos而言,一个问题是,在使用i
对数组进行索引后,完成了对i==n
的检查。要解决此问题,应在检查之前增加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
为零的情况。一般来说,我认为while(I
比do{…}while(…);
更好
还要注意,此代码(当前已注释掉)是错误的:
// item[pos].name = item[pos+1].name;
您不能使用赋值(即=
)复制字符串。您需要使用strcpy
此外,当一个项目被删除时,我看不到任何count
的更新。我想这是错误…我认为count
必须递减
最后,我没有看到函数返回任何值。这也是一个错误,因为您将函数定义为返回int
一个音符…
使用像cont
这样的标志来终止while
循环将很好地工作,因此不是一个bug。但是,您实际上不需要标志。您可以使用break
这样的标志:
do{
...
...
if(i==n){
printf("\nCode not found!\n\n");
break; //loop ends when there are no matches
}
i++;
}while(1);
或者简单地执行一个返回
,因为函数没有其他事情要做
编辑
OP发布了代码的第二次修订。此编辑地址为第二次修订的地址
第二个版本的一个问题是代码总是返回n-1
。即使找不到“code”,也会返回。这是一个bug
if(i==(n-1)){
的if也是错误的,因为它意味着永远不会测试项目编号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
}
intdeleteItem(intn){
printheader();
int i=0,代码,位置;
printf(“\n输入要删除的项目的代码:”;
scanf(“%d”,代码);
而在(iBtw)中,我将“item[pos].name=item[pos+1].name;”作为注释,因为当我包含它时程序没有运行。它说“数组分配无效”IDK为什么?请使用<代码>破解< /code >代替这个代码> CONT>假< /code >无用,甚至可能是<代码> >外迭代!而不是使用C编译器,而是C++编译器,因此您的代码是C++:/您是否听说过<代码>破解< /code >指令?<代码> SCANF(“%D”,代码)
需要是一个指针,看起来有很多错误,我的DeleteItem函数也被相应地设置了。编辑了主函数,使DeleteItem返回递减的count值。但仍然存在相同的问题。我还使用了item[pos]=item[pos+1]在我的第一次尝试中,程序崩溃了,所以我认为这就是问题所在,并决定逐个改变元素。