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;
}

You need to read a little more about pointers. 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.

This article has been dead for over six months. Start a new discussion instead.