0

The system lets the user to delete a customer by entering customer's IC Number. I am using queue with linked list.I have assigned front (headptr) to thisptr (current). The problem is at if-else statement [the comparison of thisptr->ic with ic_no]. It keeps execute else statement although the statement is true. May i know how to solve it and why is this happened? Really appreciated for helping.

void delete_node(){
system("cls");
char ic_no[20];

printf("Enter Customer IC: ");
scanf(" %s", &ic_no);

thisptr = front;

while(thisptr !=NULL){
    if(thisptr->ic == ic_no){
        printf("true\n");
        break;
    }
    else{
        printf("f\n");
        thisptr = thisptr->nextptr;
    }
}
if(thisptr == NULL){
    printf("No Customer is found!. ");
    getch();
    return;
}else{
    deletenode = thisptr;
    printf("Customer Name %s has been removed!.\n", deletenode->name);
    thisptr = thisptr->nextptr;
    free(deletenode);
}

}

Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • That code isn't written in C#. Please don't spam irrelevant tags, it could put people off from wanting to help you. – Some programmer dude Sep 12 '22 at 06:32
  • Also please take some time to read or refresh [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And don't forget how to create a [mre] or how to [edit] your questions. – Some programmer dude Sep 12 '22 at 06:35
  • 1
    Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – H.S. Sep 12 '22 at 06:40
  • 1
    1) You haven't provided the description of the struct, but 2) from the looks of it you are trying to compare strings against each other. That doesn't work like that in C, well not how you like it to be done. You need to use a function like strcmp to do the work for you – Randommm Sep 12 '22 at 06:41
  • Instead of this `if (thisptr->ic == ic_no)`, try this `if (strcmp (thisptr->ic, ic_no) == 0)`. You don't need to give `&` in `scanf()`. Additional - limit `scanf()` to read max `19` characters, otherwise your program will lead to UB, - `scanf(" %19s", ic_no)`. – H.S. Sep 12 '22 at 06:41

0 Answers0