3

How to delete an element from the array of type structure? Let's say if I register an item and then want to delete it how can I do that? The delete function is at the end of the code. I want to delete the item by giving the varunummer (id number). Any one know how to do it?

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define WORDLENGTH 30
#define MAX 5
struct varor {
    int varunummer;
    char namn[WORDLENGTH];
    int lagersaldo;
};

int readLine(char s[], int length); 
int ifVarunummerExist(int varunummer, const struct varor reg[], int nrOfGoods);
void registerVaror(struct varor reg[], int *nrOfGoods);
void getPrint(const struct varor reg[], int nrOfGoods);
void avregristreraVaror(struct varor reg[], int nrOfGoods);

int main(void) {
    struct varor vRegister[WORDLENGTH];
    int nrOfGoods = 0;
    int run = 1;
    while (run) {
        char choice;
        printf("\n\t\tMeny - Lager Program\n\n\
        (1) Register\n\b\b\b\b\
        (2) Print\n\
        (3) Delete\n\
        (4) Quit\n");
        scanf(" %c%*c", &choice);

        if (choice=='1')
            registerVaror(vRegister, &nrOfGoods);
        if (choice=='2')
            getPrint(vRegister, nrOfGoods);
        if (choice=='3')
            avregristreraVaror(vRegister, nrOfGoods);
        else if (choice=='4')
            run = 0;
    }
    return 0;
}

int ifVarunummerExist(int varunummer, const struct varor reg[], int nrOfGoods) {
    int i;
    for (i = 0; i < nrOfGoods; i++)
        if(reg[i].varunummer == varunummer)
            return i;
    return -1;
}

int readLine(char s[], int length) {
    int ch, i=0;
    while (isspace(ch=getchar()));
    while (ch != '\n' && ch != EOF) {
        if (i < length)
            s[i++] = ch;
        ch = getchar();
    }
    s[i] = '\0';
    return i;
}

void registerVaror(struct varor reg[], int *nrOfGoods) {
    char namn[WORDLENGTH], tmp[WORDLENGTH];
    int varunummer, lagersaldo;
    if (*nrOfGoods == MAX) {
        printf("\nError! Finns inte plats kvar!\n");
        return;
    }
    printf("Ange varunummer: ");
    scanf("%d", &varunummer);
    if (ifVarunummerExist(varunummer, reg, *nrOfGoods) >= 0) {
        printf("\nVarunummer finns redan!\n");
        return;
    }
    reg[*nrOfGoods].varunummer = varunummer;
    printf("Ange namn: ");
    readLine(reg[*nrOfGoods].namn, WORDLENGTH);

    printf("Ange lagersaldo :");
    scanf("%d", &reg[*nrOfGoods].lagersaldo);
    //reg[*nrOfGoods]=createVara(varunummer,namn,lagersaldo);
    (*nrOfGoods)++; 
}

void getPrint(const struct varor reg[], int nrOfGoods) {
    int i;
    printf("\nVarunummer \t Namn \t\t\t Lagersaldo\n");
    for (i = 0; i < nrOfGoods; i++) {
        printf(" %d \t\t %s \t\t\t %d\n",reg[i].varunummer,reg[i].namn,reg[i].lagersaldo);
    }
}

void avregristreraVaror(struct varor reg[], int nrOfGoods) {
    int run = 1;
    while (run) {
        char choice;    
        printf("\n (1) Delete \n (2) Exit");
        scanf(" %c", &choice);
        //DELETE IF CHOICE 1---------
        if (choice == '1') {
            int i, varunummer;
            printf("Ange varunummer: ");
            scanf("%d", &varunummer);
            for (i = varunummer + 1; i < MAX; i++) {
                reg[i - 1] = reg[i];
            }
            reg[i] = 0;
        }
    }
    //QUIT TO MY MENU CHOICE 2--------
    if (choice == '2')
        run = 0;
}
3
  • You are shifting the values which will delete the respective element. Whats is the problem exactly ? Doesn't it work? If not what happens that shouldn't be happening? Commented Oct 20, 2017 at 0:33
  • @Isac reg[i]=0; assigning to struct *varor incompatible with type int- error from the compiler Commented Oct 20, 2017 at 0:38
  • That line is useless, there is no need to physically clear the last element contents, just decrement the number of goods. If you wanted to clear it though, remember that it is a struct so you can't just assign it to 0. Commented Oct 20, 2017 at 0:42

1 Answer 1

4

You can try iterating through the array in a for loop UNTIL your varunummer is matched with the struct's property. Something along these lines (let's say you are searching for the member with varunummer = varunummerToLookFor), this shift all the elements in the array from the point onwards of your deletion by 1, hence, producing an array with the same sequence as before but with your wanted element removed. Hope that helps!

for(int i = 0, i < varorArraySize, i++){
if(varunummerToLookFor == varorArray[i].varunummer){
for (i = pos; i <  varorArraySize - 1; i++)
            {
            varorArray[i] = varorArray[i + 1];


           } 
       }
}
Sign up to request clarification or add additional context in comments.

2 Comments

what stands pos for?
pos = position you want it to start from, or you could just do 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.