Thread: help with adding fields

  1. #1
    Unregistered
    Guest

    Question help with adding fields

    Hello, I am making an address book using linked lists. I must read in a few (5-10) addresses from a file that I am supposed to make. Then, I must be able to add to this list by using user input within my program, and print out the entire address book. Here is my code so far, sorry for the beginner syntax, not to familiar with C:


    /*MY QUESTION IS, HOW DO I CREATE THE FILE I MENTIONED ABOVE, READ IT INTO MY PROGRAM, AND THEN ADD USER INPUTTED ADDRESSES ALSO? THANKS FOR ANY HELP.*/


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

    struct node {

    int data;
    char name[25];
    char address [40];
    char phone [20];
    struct node *next;
    struct node *link;

    };

    /*Prototyping the necessary functions*/
    void show_list(struct *node);
    void enter_selection(struct *node);

    main()
    {
    while (1)

    { struct node *pstart, *pprev, *pnow;

    int cont;
    pstart=pprev=NULL;

    {pnow=(struct node *)malloc(sizeof(struct node));

    if (pnow==NULL)

    printf("Unable to allocate memory");

    if (pstart==NULL)

    pstart=pnow;

    else pprev->next=NULL;

    enter_selection(pnow);
    pnow->next=NULL;

    pprev=pnow;
    show_list(pstart);

    }

    printf("\n Would you like to enter another? (1=YES/0=NO)");
    scanf("%d", &cont);
    while (cont!=1 && cont!=0)
    { printf ("Sorry, 1 for YES, 0 for NO\n");
    scanf("%d", &cont);
    }
    if (cont==0)
    return 0;
    }


    return 0; }


    /*A function that asks the user to input the data into the address book*/

    void enter_selection (struct node *pnow)

    {
    int choice;

    printf("\t\t\tMAIN MENU\n\n");
    printf("1. Search for a person's address\n");
    printf("2. Add a person to your address book\n");
    printf("3. Delete a person from your address book\n");
    printf("4. View the names of people between two last names\n");
    printf("5. View your entire address book\n");
    printf("6. End the program\n");
    printf("Please enter your selection (1-6):");
    scanf("%d", &choice);

    if (choice==2)
    {fflush(stdin);
    printf("Enter the data: Last Name, First Name:");
    gets(pnow->name);
    printf("Enter a street address, without hitting return:");
    gets(pnow->address);
    printf("Enter a phone number (xxx-xxx-xxxx):");
    gets(pnow->phone);
    printf("Enter an integer:");
    fflush(stdin);
    scanf("%d", &pnow->data);}
    }

    /*A function that displays the contents of the address book*/
    void show_list(struct node *ps)
    {
    printf(" NAME\t\t\tADDRESS\t\t\t\t\t PHONE\n");
    do {
    printf("%d %s \t%s \t\t %s \n", ps->data, ps->name, ps->address, ps->phone);
    ps=ps->next;
    }while (ps !=NULL);
    return;

    }

    /* A function that sorts the data that is within*/
    struct node *search(struct node *pfirst, char *nm)

    { struct node *p;

    p=pfirst;
    while (p!=NULL)
    {if (strcmp(nm, p->name)==0)
    return p;

    p=p->next; }
    return NULL;

    }

    /*A function that will allow the user to insert a new item to the address book*/
    struct *insert_node(struct node *p, int item)
    {

    struct node *pnew, *pprev, *pcur;

    pnew=(struct node*) malloc(sizeof(struct node));

    if (p==NULL)

    {
    p=pnew;
    p->data=item;
    p->link=NULL;

    }

    else if (p->data>item)
    { pnew->link=p;
    p=pnew;
    p->data=item;

    }

    else

    {
    pcur=p;

    while (pcur->data <item)
    {pprev=pcur;
    pcur=pcur->link;
    if(pcur==NULL)
    break;

    }
    pprev->link=pnew;
    pnew->link=pcur;
    pnew->data=item;

    }
    return p;

    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    main() 
    { 
        while (1) 
        {
            struct node *pstart, *pprev, *pnow; 
            int cont; 
    
            pstart=pprev=NULL; 
            { /* <---- Why do you have this? It is not needed or useful.*/
                pnow=(struct node *)malloc(sizeof(struct node)); 
                if (pnow==NULL) 
                    printf("Unable to allocate memory"); 
    
                if (pstart==NULL) 
                    pstart=pnow;
    You don't need that set of braces.
    Additionally, if 'pnow' is NULL, shouldn't you terminate the program? You generally just don't want to merrily continue when you are out of memory.

    For creating a file, you want to use the fopen function for openting; fwrite function for writing; fread for reading; and fclose for closing.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    Thanks Qazah,

    I am looking for any hints on how to start the code to write the file and then print it out in my program. The text I am using for assistance is very hard to follow when it comes to FILES. Thanks anyone for any additional help.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FILE *fp;

    fp = fopen( "myfile.dat", "wb" ); /* create a new file */
    if( fp == NULL ) { exit(0); /* unable to create, terminate */

    Then use 'fwrite' to write one node at a time.

    Close the file when you're done writing.
    You can open it for reading and display them or whatever.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    thanks quzah,

    its helping already.

    -b

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  2. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  3. Adding Fields to your OpenGL GUI
    By Shamino in forum Game Programming
    Replies: 18
    Last Post: 10-09-2005, 01:47 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM