1

I got a few problems when writing this program. While searching in the list, only the first result can be shown. the program cant show more than one result even if there are other data matching the requirement.

The second problem is that when the user tries to search by name there will be a runtime error.

Thanks for helping.

Here is the code:

int namesearch(LIST *list,STUDENT **pPre,STUDENT **pLoc,char target[]){
    int n;
    *pLoc = list->head;
    for (;*pLoc!=NULL && n!=0;){
        n=strncmp(target,(*pLoc)->name,MAXNUM);
        *pPre = *pLoc;
        *pLoc = (*pLoc)->next;
        printf("1");
    }
    if(*pPre==NULL)
        return 0;
    else{
         printf("2");
        if (n==0){
            printf("%s%d    | %-18s|  %0.1f  |  %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
            return 1;
            fflush(stdin);getch();
        }
        else
            printf("3");
            return 0;
    }
}
  • For your first issue, why not just stop with the first match?? – Hot Licks May 05 '13 at 13:37
  • 1
    (I'd suggest you get together with your classmate: http://stackoverflow.com/questions/16384641/looping-through-recursive-list-in-c) – Hot Licks May 05 '13 at 13:39
  • Hm.. Sorry I cant get what you say. The complete code has been posted or it can help you understand more about what i am trying to do. Thanks anyway. – user2351825 May 05 '13 at 14:55

1 Answers1

0

firstly the reason that you aren't getting more than one result is because of the structure of the loop. The following change can print the results.

int namesearch(LIST *list,STUDENT **pPre,STUDENT **pLoc,char target[]){
    int n;
    int found = 0;
    *pLoc = list->head;
    for (;*pLoc!=NULL;){
        n=strcmp(target,(*pLoc)->name);
        if (n==0){
                printf("%s%d    | %-18s|  %0.1f  |  %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
            found++;
            fflush(stdin);getch();
        }
        *pPre = *pLoc;
        *pLoc = (*pLoc)->next;
    }

    return found;
}

but these will just print the data that is found. if you want the locations where the data is found you would have to modify it a little more

int namesearch(LIST *list,STUDENT **pPre,STUDENT **pLoc,char target[],STUDENT *foundat){
    int n;
    int found = 0;
    *pLoc = list->head;
    for (;*pLoc!=NULL;){
        n=strcmp(target,(*pLoc)->name);
        if (n==0){
                printf("%s%d    | %-18s|  %0.1f  |  %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
            foundat[found] = *pLoc;
            found++;
            fflush(stdin);getch();
        }
        *pPre = *pLoc;
        *pLoc = (*pLoc)->next;
    }

    return found;
}

to tell why the runtime error happens please provide the complete code... just taking a guess i have removed the strncmp and changed it to strcmp ( Which may be one of the reason either the target or the *pLoc->name is failing to provide MAXNUM number of characters)

DarthCoder
  • 354
  • 1
  • 11