Hello
I am having trouble with my project crashing for every case. We are told to read in a data text file and store into structs and create a linked list of data and allow a user to choose from 4 menu options. I am completely new to linked lists and am having a hard time understanding how to string compare user entered names and find them amongs the text file given.

If anyone could give some aid or help as to what I am doing incorrect or why it is crashing, it would be much appreciated. There are no compiler errors so i am completely at a standstill of what to do

#include <stdio.h>
#include <stdlib.h>
#define Max 100

struct account {
    char *firstname;
    char *lastname;
    int *digits;
} ;

struct node {
    char *firstname;
    char *lastname;
    int *digits;
    struct account emp;
    struct node* Pnext;
    struct node* Pprev;
};

    int count=0;
    struct node* Phead;
    struct node* Ptail;

struct node* create(struct account emp);
struct node*add(struct account emp);
struct node*searchbyfirstname (char *firstname);
struct node*searchbylastname (char *lastname);

void main(void)
{
    FILE *infile= NULL;
    FILE *outfile= NULL;

    int choice=4, i, *digits, x;
    char *infilename, *outfilename, *firstname, *lastname;
    struct account emp;
    struct node*ppt;
    struct node*ptr;
    struct node*account;
    struct node*z;
    struct node* create(struct account emp);
    struct node*add(struct account emp);
    struct node*searchbyfirstname (char *firstname);
    struct node*searchbylastname (char *lastname);

    infilename=(char *)malloc(Max* sizeof(char ));
    outfilename=(char *)malloc(Max* sizeof(char ));
    firstname=(char *)malloc(Max* sizeof(char ));
    lastname=(char *)malloc(Max* sizeof(char ));
    digits=(int *)malloc(Max* sizeof(int ));

    do
    {
        printf("Input filename:");
        scanf("%s",infilename);
        if((infile = fopen(infilename, "r")) == NULL)
        {
            printf("ERROR: file %s can not be opened!\n",infilename);

        }
    }while(infile == NULL);

    for(i=0 ; *(infilename + i) != '.'; i++)
    {
        *(outfilename + i) = *(infilename + i);
    }
        *(outfilename + i) = '\0';
        strcat(outfilename, "_out.txt");

    if((outfile = fopen(outfilename, "w")) == NULL){
        {
            printf("ERROR: file %s can not be opened!\n",outfilename);

        }
    }while(outfile == NULL);


    while(fscanf(infile, "%s\t%s/t%s", &firstname, &lastname, &digits)!=EOF)
    {
        fscanf(infile, "%s\t%s\t%s", &firstname, &lastname, &digits);
    }



    while(choice!=0)
    {                                                                                               
        printf("\nA Program to Calculate\n\nWould you like to Add, Subtract, or Divide?\n\n");
        printf("(0) Write output file and exit \n");
        printf("(1) Print the phone book\n");
        printf("(2) Add new account at head of the phone book\n");
        printf("(3) Find by last name\n");
        printf("(4) Find by first name\n\n");
        scanf("%d", &choice);

        switch(choice)
        {
            case 0: while(digits != NULL)
                    {
                        fprintf(outfile, "%s    %s  %s", &firstname, &lastname, &digits);
                    }
                    break;

            case 1: 

                    while(digits == NULL)
                    {
                        printf("%s  %s  %s", &firstname, &lastname, &digits);
                    }
                    break;
            case 2:
                    printf("Enter the First name");
                    scanf("%s", &firstname);
                    printf("Enter the Last name");
                    scanf("%s", &lastname);
                    printf("Enter the phone number");
                    scanf("%s", &digits);
                    ptr= create (emp);
                    struct node* ptr;
                    ptr = (struct node*)malloc(sizeof(struct node));
                    strcpy(ptr->firstname,firstname);
                    ptr->lastname=lastname;
                    ptr->digits=digits;
                    ptr->Pnext= account emp;

                    break;

            case 3:

                    printf("Type the first name of the account");
                    scanf("%s", emp.firstname);
                    ppt=searchbyfirstname(emp.firstname);

                    if(ppt==NULL)
                    {
                        printf("Name is not found");
                    }
                    else
                    {
                        printf("account last name is : %s", ppt->emp.lastname);
                        printf("account number is %ld", ppt->emp.digits);
                    }
                    break;

            case 4:

                    printf("Type the last name of the account");
                    scanf("%s", emp.lastname);
                    ppt=searchbylastname(emp.lastname);

                    if(ppt==NULL)
                    {
                        printf("Name is not found");
                    }
                    else
                    {
                        printf("account first name is : %s", ppt->emp.firstname);
                        printf("account number is %ld", ppt->emp.digits);
                    }
                    break;


        }
    }
}
      /*create a node*/
struct node* create(struct account emp)
{
    struct node*ptr;
    ptr=(struct node*)malloc(sizeof(struct node));

    if (ptr)
    {
        ptr->emp=emp;
        ptr->Pprev=NULL;
        ptr->Pnext=NULL;
    }
    return(ptr);
}
struct node*searchbyfirstname (char *firstname)
{
    struct node*ptr;
    struct node*Pmid;
    int i;
    int mid;
    int low;
    int high;

    ptr=NULL;
    low=0;
    high=count+1;

    while((low<=high))
    {
        mid=(low+high)/2;
        Pmid=Phead;

        for(i=0;i<mid;i++)
        {
            Pmid=Pmid->Pnext;
        }

        if(strcmp(Pmid->emp.firstname,firstname)==0)
        {
            ptr=Pmid;
            break;
        }
        else
        {
            if(strcmp(Pmid->emp.firstname,firstname)>0)
            {
                high=mid-1;
            }
            else
            {
                    if(strcmp(Pmid->emp.firstname,firstname)<0)
                    {
                    low=mid+1;
                    }
            }
        }

    }

    return ptr;
}

struct node*searchbylastname (char *lastname)
{
    struct node*ptr;
    struct node*Pmid;
    int i;
    int mid;
    int low;
    int high;

    ptr=NULL;
    low=0;
    high=count+1;

    while((low<=high))
    {
        mid=(low+high)/2;
        Pmid=Phead;

        for(i=0;i<mid;i++)
        {
            Pmid=Pmid->Pnext;
        }

        if(strcmp(Pmid->emp.lastname,lastname)==0)
        {
            ptr=Pmid;
            break;
        }
        else
        {
            if(strcmp(Pmid->emp.lastname,lastname)>0)
            {
                high=mid-1;
            }
            else
            {
                    if(strcmp(Pmid->emp.lastname,lastname)<0)
                    {
                    low=mid+1;
                    }
            }
        }

    }

    return ptr;
}

Dani AI

Generated

The crashes are classic pointer/format mismatches rather than a mysterious runtime bug. was right: several fields (the emp strings and digits) are used before they hold valid memory, and scanf/fscanf calls pass the wrong kinds of pointers. Fixes are straightforward and incremental.

Immediate, high-impact fixes:

  • Use fixed buffers in the account struct while learning (avoids many malloc mistakes):

    struct account { char firstname[100]; char lastname[100]; char phone[32]; };
  • When reading strings with scanf/fscanf pass the buffer (not &buffer) and include width limits:

    fscanf(infile, "%99s %99s %31s", firstname, lastname, phone);
  • Don’t use int *digits for phone numbers. Either store phone as a string (char phone[]) or as a numeric type (long) and use matching format specifiers (%s vs %ld).

  • Never strcpy into a char * that hasn’t been allocated. Either make the destination an array or allocate and copy (or use strdup).

Algorithm and structure notes:

  • Binary search on a linked list is fragile and only valid if the list is sorted and count is maintained correctly. Use a simple linear scan for correctness and clarity:

    for (p = head; p; p = p->Pnext)
        if (strcmp(p->emp.firstname, name) == 0) return p;
  • Keep the list API consistent: create() should allocate the node and perform a deep copy of strings (or move ownership explicitly). Maintain count, Phead, and Ptail reliably.

Quick debugging checklist:

  • Compile with warnings and debug info: gcc -Wall -Wextra -g (or add -fsanitize=address / run under Valgrind).
  • Check every malloc/fopen return.
  • Remove accidental redeclarations inside main and avoid shadowing variables (the original code redeclares ptr and prototypes).
  • Replace odd loops like while(digits != NULL) with proper node traversals when printing or writing the list.

These changes address the main causes of crashes and make string comparisons reliable.

Recommended Answers

All 2 Replies

You need to read a little more about . For example in line 117 you attempt to create an new node by using emp variable before you were assigning any values to emp members. Because emp members (account structure) are pointer and you have to assign a variable.
Pointers are useful in C but dengerous. Be careful ;)

Ok thank you. I thought i was storing my read in strings in emp members but i guess i got lost in the thought process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.