Thread: Searching a linked list for char

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Searching a linked list for char

    First off, I know everyone is going to tell me I don't need to use a linked list, but I don't know how to change it to Fprintf etc. although I would like to. My problem is I have 4 warnings in my code. I know that when my search function runs it's looking for an int, but I want it to look for char. ow is this done? My code is below. Thx.


    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define clrscr() system("cls")

    //structure for writing to and reading from file**
    struct fclient{
    char clientname[20];
    char street_address[20];
    char city[15];
    char country[10];
    char postalorzip_code[8];
    };
    //************************************************

    //structure for creating the linked list**********
    struct lclient{
    char clientname[20];
    char street_address[20];
    char city[15];
    char country[10];
    char postalorzip_code[8];
    int deleted; //used as a boolean (0=false, 1=true)
    struct lclient *next;
    }*head, *work;
    //************************************************

    /* FUNCTION PROTOTYPES*********************/
    char menu(void);
    void add_record(void);
    void delete_record(void);
    void search(void);
    void display(void);
    char print_label(void);
    void key_wait (void);
    /* END OF FUNCTION PROTOTYPES**************/

    void main()
    {
    FILE *dta_file;
    char choice;

    clrscr();
    dta_file = fopen("client.dat", "r+b"); //opening in binary mode for read/write

    if (dta_file == NULL)
    {
    printf("\n\nFile does not exist: Creating file");
    dta_file = fopen("client.dat", "w+b"); //creation in binary mode
    head=NULL;//empty list
    key_wait();
    }
    else
    {
    /*Load the file contents in a linked list*******************/
    head=work=(struct lclient *)malloc(sizeof(struct lclient));
    fread(work,sizeof(struct fclient),1,dta_file);
    while(!feof(dta_file))
    {
    work->deleted=0;
    work->next=(struct lclient *)malloc(sizeof(struct lclient));
    fread(work->next,sizeof(struct fclient),1,dta_file);
    if(!feof(dta_file))
    {
    work=work->next;
    }
    else
    {
    work->next=NULL;//end of list
    }
    }

    }
    fclose(dta_file);

    do
    {
    choice = menu();
    clrscr();
    switch (choice)
    {
    case '1': add_record(); break;
    case '2': delete_record(); break;
    case '3': search(); break;
    case '4': display(); break;
    case '5': print_label(); break;
    }
    } while (choice != '6');

    //open and overwrite the file
    dta_file = fopen("client.dat", "wb");
    /*Write the contents of the linked list into the file******/
    work=head;
    while(work!=NULL)
    {
    if(work->deleted==0)
    {
    fwrite(work,sizeof(struct fclient),1,dta_file);
    }
    work=work->next;
    }

    fclose(dta_file);

    }

    /************************************************** **********/
    char menu (void)
    /*
    Task: display a menu and return the choice of the user
    */
    /************************************************** **********/
    {
    char choice;

    clrscr();
    printf("Client Inventory Management \n\n\n");
    printf("1. Add a Client\n\n");
    printf("2. Delete a Client\n\n");
    printf("3. Search for a Client\n\n");
    printf("4. Display the inventory\n\n");
    printf("5. Print shipping label\n\n");
    printf("6. Exit the program\n\n");
    printf("Please enter your selection: ");

    do
    {
    choice = getch();
    } while (choice < '1' || choice > '6');

    return (choice);
    }
    /************************************************** **********/
    void add_record(void)
    /*
    Task: Read from keyboard and add the record in the list
    Note: The code must be unique and < 10000
    */
    /************************************************** **********/
    {
    char clientname; //temp var for validation
    int clientunique=1;//flag for client name validation
    struct lclient *valid;

    // Validate the client name************************
    while(!clientunique)
    {
    //reset the flag
    clientunique=1;

    clrscr();
    printf("Client name : ");
    scanf("%s", &clientname);
    fflush(stdin);
    //validate that the client name is unique
    valid=head;
    while(valid != NULL)
    {
    if (clientname == valid->clientname)
    {
    clientunique = 0;
    }
    valid=valid->next;
    }
    if (!clientunique)
    {
    printf("This client name is taken. Please enter a different name...Press a key\n");
    key_wait();

    }
    }//END of Validation


    if (head==NULL)
    {
    head=work=(struct lclient *)malloc(sizeof(struct lclient));
    }
    else
    {
    work->next=(struct lclient *)malloc(sizeof(struct lclient));
    work=work->next;
    }

    clientname=work->clientname;

    printf("Client name : ");
    scanf("%20[^\n]", work->clientname);
    fflush(stdin);
    printf("Street address : ");
    scanf("%20[^\n]", work->street_address);
    fflush(stdin);
    printf("City : ");
    scanf("%15[^\n]", work->city);
    fflush(stdin);
    printf("Country : ");
    scanf("%10[^\n]", work->country);
    fflush(stdin);
    printf("Postal or Zip code : ");
    scanf("%8[^\n]", work->postalorzip_code);
    fflush(stdin);
    work->deleted=0;
    work->next=NULL;
    }

    /************************************************** **********/
    void delete_record(void)
    /*
    Tasks: Read the client name input by the user
    Search for that name in the list
    Mark the record as DELETED or
    Advise user of its non-existence
    Note: Must check the value of DELETED field before

    */
    /************************************************** **********/
    {
    struct lclient *search;
    char clientname, found=0,out=1;

    clrscr();
    printf("Delete a client from the inventory\n\n");
    printf("Enter the Client Name: ");
    scanf("%s",&clientname);

    //search for the code in the list
    search=head;
    while(search != NULL)
    {
    if(search->clientname == clientname && search->deleted == 0)
    {
    found = 1;
    search->deleted=1;
    }
    search=search->next;
    }
    if (found)

    {
    printf("\n\nThe client has been deleted from the inventory...");
    }
    else
    {
    printf("\n\nThat client name does not exist...");
    }

    key_wait();
    }

    /************************************************** **********/
    void search(void)
    /*
    Tasks: Read the client name input by the user
    Search for that client name in the list
    Display the record or advise the user of its non-existence
    Note: Must check the value of DELETED field
    */
    /************************************************** **********/
    {
    struct lclient *search;
    char clientname, found=0;

    clrscr();
    printf("Search a client in the inventory\n\n");
    printf("Enter the Client name: ");
    scanf("%s",&clientname);

    //search for the code in the list
    search=head;
    while(search != NULL)
    {
    if(search->clientname == clientname && search->deleted == 0)
    {
    found = 1;
    printf("Client Name: %s\n",search->clientname);
    printf("Street Address: %s\n",search->street_address);
    printf("City: %s\n",search->city);
    printf("Country: %s\n",search->country);
    printf("Postal or Zip Code: %s\n\n\n",search->postalorzip_code);
    }
    search=search->next;
    }
    if (!found)
    {
    printf("\n\nThat client name does not exist...");
    }

    key_wait();
    }

    /************************************************** **********/
    void display(void)
    /*
    Task:
    SORT the list
    Display all records
    Note: Must check the value of DELETED field
    */
    /************************************************** **********/
    {
    struct lclient *hsort,*wsort,*nsort;

    int line=0;

    clrscr();
    if (head==NULL)
    {
    printf("The inventory is empty.\n");
    }
    else
    {
    clrscr();

    //CREATE a SORTED list
    work=head;
    hsort=wsort=nsort=(struct lclient *)malloc(sizeof(struct lclient));
    strcpy(wsort->clientname,work->clientname);
    strcpy(wsort->street_address,work->street_address);
    strcpy(wsort->city,work->city);
    strcpy(wsort->country,work->country);
    strcpy(wsort->postalorzip_code,work->postalorzip_code);
    wsort->deleted=work->deleted;

    wsort->next=NULL;
    while(work->next!=NULL)
    {
    wsort->next=(struct lclient *)malloc(sizeof(struct lclient));
    work=work->next;
    wsort=wsort->next;
    strcpy(wsort->clientname,work->clientname);
    strcpy(wsort->street_address,work->street_address);
    strcpy(wsort->city,work->city);
    strcpy(wsort->country,work->country);
    strcpy(wsort->postalorzip_code,work->postalorzip_code);
    wsort->deleted=work->deleted;
    wsort->next=NULL;
    nsort=wsort;
    wsort=hsort;
    }

    //DISPLAY the sorted LIST
    clrscr();
    wsort = hsort;
    while(wsort != NULL)
    {
    if(!wsort->deleted)
    {
    //display the current record
    printf("Client Name: %s\n",wsort->clientname);
    printf("Street Address: %s\n",wsort->street_address);
    printf("City: %s\n",wsort->city);
    printf("Country: %s\n",wsort->country);
    printf("Postal or Zip Code: %s\n\n",wsort->postalorzip_code);
    line+=5;
    }
    if (line > 14)
    {
    key_wait();
    clrscr();
    line=0;
    }
    wsort = wsort->next;
    }//END DISPLAY
    }
    if (line > 0)
    {
    key_wait();
    }

    //RELEASE the RAM taken by the sorted LIST (*hsort)
    wsort=hsort;
    while(wsort != NULL)
    {
    hsort=wsort->next;
    free(wsort);
    wsort=hsort;
    }
    }
    /************************************************** **********/
    char print_label (void)
    /*
    Task: Prints shipping information
    */
    /************************************************** **********/
    {
    FILE *printer = fopen("LPT1","w");
    FILE *dta_file;
    char str[256];
    char buf[BUFSIZ];
    printf("Client name: ");
    scanf("%s",str);
    dta_file = fopen(str,"r");
    if(!dta_file)
    {
    printf("File does not exist\n");
    return -1;
    }
    fgets(buf,sizeof buf,dta_file);
    while( fgets(buf,BUFSIZ,dta_file) != NULL )
    {
    fprintf(printer,"%s",buf);
    }

    fprintf(printer,"buf");
    return 0;
    }

    /************************************************** **********/
    void key_wait (void)
    /*
    Task: Pause until user presses a key
    */
    /************************************************** **********/
    {
    printf ("\n\nPress a key to continue...");
    getch();
    }

  2. #2
    Unregistered
    Guest
    Aaaahh, I see what is going wrong. You haven't used code tags!!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That's a lot of code, with no code tags! Can you edit your post, and add code tags (also, repaste your code from your editor so you get the indentation back).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not to mention the following common mistakes

    - void main()
    - while(!feof(dta_file))
    - fflush(stdin);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM