2020-11-03

A way to know if element in array of struct is empty/NULL in C

I would really appreciate help with this piece of code.

So I have defined a structure, and then an array of structures:

#define MAX_ITEMS  30
struct item{
    char itemName[30];
    int identification;
    float sale;
};
struct item itemArray[MAX_ITEMS];

Then I have a function which takes the index of the array structure and is supposed to delete that element, and update the new array without the deleted element to the local .txt file

printf("item's index please: ");
char input[30];
gets(input);
for (int i=input; i<MAX_ITEMS;i++){ 
    itemArray[i] = itemArray[i + 1];
}

FILE *fp
fp = fopen ("file.txt", "w");
int index = 0;
while(itemArray[index].name != ""){
fprintf(fp,"%s\n",itemArray[index].name);
fprintf(fp,"%d\n",itemArray[index].identification);
fprintf(fp,"%.2f\n",itemArray[index].sale);
index++;
}
fclose(fp);

So what I am expecting: enter image description here

Instead, I get

enter image description here

Finishing after thousands of lines, like this: enter image description here

I would appreciate any help!



from Recent Questions - Stack Overflow https://ift.tt/381DhhL
https://ift.tt/3jR4ndQ

No comments:

Post a Comment