Assuming I have these two structures for a double linked list
Code:
typedef struct node_d
{
char city[40];
char state[3];
char zipcode[6];
double longitude;//double
double latitude;//double
long population;//long
struct node_d *previous;
struct node_d *next;
}Node;
typedef struct
{
struct node_d *psHead;
struct node_d *psTail;
struct node_d *current_node;
int node_count;
}Header;
I am trying to implement a search.
I figured this would work, but doesn't. Can anyone see any flaws in it ? I am confused on how to fix this problem
Code:
int Search_List(Header *psList, int z)
{
int x;
char answer[40], compare[40];
printf("\n\nWhat City or Zipcode would you like to search for?");
scanf("%s",&answer);
if (isdigit(answer[0]) != 0)
{
//search for zip
psList->current_node = psList->psHead;
for(x=0;x<psList->node_count;x++)
{
strcpy(compare, psList->current_node->zipcode);/////////it seems to crash right here
if(strcmp(answer, compare)== 0)
{
system("cls");
printf("\nThe Zipcode was found with %d comparisons",x+1);
printf("\nZipcode = %s City = %s State = %s",psList->psHead->zipcode, psList->psHead->city, psList->psHead->state);
printf("\nPopulation = %d Longitude = %-.2f latitude = %-.2f\n",psList->psHead->population, psList->psHead->longitude, psList->psHead->latitude);
x=z;
}
else
{
psList->current_node = psList->current_node->next;
}
}
printf("\nRecord was not found\n");
}
it seems to crash right at this command
strcpy(compare, psList->current_node->zipcode);
but I don't know why. Is there a problem with the way strcpy is called?
Any help would be appreciated