Thread: Help with menu & Linked list

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Post Help with menu & Linked list

    I'm having a dilemna with a program that I'm working on:

    I'm supposed to:

    Write a C program to manage the customers’ information. The program should allow the user to add and remove a customer, display all customers, and search a customer's information by his/her name. The program should be menu-based with the following options:
    Main Menu:
    (a) Add a customer
    (r) Remove a customer
    (s) Search a customer
    (d) Display all customers’ information
    (e) Exit
    Your choice?

    Note:
    For all submenus, please return to the main menu after an action is done.
    Each customer entry should include FirstName, LastName, PhoneNumber, Company/Organization, and Email. All entries must be stored in a text file customers.txt using the following format (“:” is the separator.):
    Feng:Gu:1-404-6555578:Georgia State University:[email protected]
    Using link list data structure to store each customer entry
    Append the new entries to the end of the file; insert the new entries to the end of link list

    So far I've come up with this:

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    
    char choice, firstname, lastname, phonenumber, comporg, email, search;
    
    // Definition of node for holding customer info
    struct customer {
                char firstname;
                char lastname;
                char phonenumber;
                char comporg;
                char email;
                struct customer *next;
    };
    
    typedef struct customer Customer;
    typedef Customer *Customerptr;
    
    
    // Function prototypes:
    void loadmainmenu ();
    void add( Customerptr *headOfList, char firstname, char lastname, char phonenumber, char comporg, char email );
    char delete( Customerptr *headOfList, char fname, char lname );
    void loadsearchmenu ();
    void print( Customerptr *headOfList);
    void printlist( Customerptr *headOfList );
    int isEmpty( Customerptr cptr );
    
    
    // Main program:
    int main(void)  {
        
        Customerptr startptr = NULL;
          
        loadmainmenu();
        printf("Your choice? \n");
        scanf("%c", choice);
        
        while ( choice != 'e' || choice != 'E' )  {
              
                  // Evaluates menu options
                  switch (choice) {
                  
                  // Displays add customer submenu:
                  case 'A':
                  case 'a':
                       printf("Add Submenu: \n\n");
        
                       printf("Please input First Name: \n");
                       scanf ("%c \n", &firstname);
                       printf("Please input Last Name: \n");
                       scanf ("%c \n", &lastname);
                       printf("Please input Phone Number: \n");
                       scanf ("%c \n", &phonenumber);
                       printf("Please input Company/Organization: \n");
                       scanf ("%c \n", &comporg);
                       printf("Please input Email: \n\n");
                       scanf ("%c \n", &email);
                       
                       add(&startptr, firstname, lastname, phonenumber, comporg, email);
                       printf("A new customer has been added \n");
                       
                       break;
                  
                  // Displays remove customer submenu:     
                  case 'R':
                  case 'r':
                       printf("Remove Submenu \n");
                       
                       if ( !isEmpty( startptr )) { 
                           printf("Please input First Name: \n");
                           scanf ("%c \n", &firstname);
                           printf("Please input Last Name: \n\n");
                           scanf ("%c \n", &lastname);
                       }
                       
                       if ( delete( &startptr, firstname, lastname ) ) {
                            printf("%c %c has been removed from the system. \n", firstname, lastname);
                       }                    
                       
                       break;
                       
                  case 'S':
                  case 's':
                       printf("Search Submenu \n");
                       
                       if ( !isEmpty( startptr )) {
                           printf("Please input the customer’s First or Last name:  \n");
                           scanf ("%c \n", &search);
                       }    
                       else
                           print (&startptr); 
                                    
                       break;
                       
                  // Displays display customer submenu:
                  case 'D':
                  case 'd':
                           if ( !isEmpty( startptr )) { 
                           printf("Display Submenu \n");
                           printlist( &startptr );
                           }
                           
                           printf("All customers are displayed:  \n");
                           
                       break;
                       
                  case 'E':
                  case 'e':
                       exit(0);
                       break;
                  default:
                       printf("You entered an invalid choice! \n");
                       printf("Please try again ... \n");
                       loadmainmenu();
                       break; 
                    }
              
        }
       
    }
    
    
    // Displays main menu options:
    void loadmainmenu() {
          
        printf("Main Menu: \n\n");
            
        printf("(a) Add a customer \n");
        printf("(r) Remove a customer \n");
        printf("(s) Search a customer \n");
        printf("(d) Display all customers' information \n");
        printf("(e) Exit \n\n");
    }
    
    // Adds customer name to database
    void add( Customerptr *headOfList, char firstname, char lastname, char phonenumber, char comporg, char email ) {
    
        Customerptr newPtr;
        Customerptr previousPtr;
        Customerptr currentPtr;
     
        newPtr = ( Customer * ) malloc( sizeof( Customer ) );
    
        if ( newPtr != NULL ) {
            newPtr->firstname = firstname;
            newPtr->lastname = lastname;
            newPtr->phonenumber = phonenumber;
            newPtr->comporg = comporg;
            newPtr->email = email;
            newPtr->next = NULL;
        
            previousPtr = NULL;
            currentPtr = *headOfList;
        
            if ( currentPtr != NULL ) {
                previousPtr = currentPtr;
                currentPtr = currentPtr->next;
            }
        
            if ( previousPtr == NULL ) {
            newPtr->next = *headOfList;
            *headOfList = newPtr;    
            }
        
            else {
            previousPtr->next = newPtr;
            newPtr->next = currentPtr;
            }
            
        }
    
        else {
        printf( "%c %c not inserted. There is no memory available.\n", firstname, lastname);
        }
    }
    
    // Deletes customer from database
    char delete( Customerptr *headOfList, char fname, char lname ) {
        Customerptr previousPtr;
        Customerptr currentPtr;
        Customerptr tempPtr;
        
        if ( ( fname == ( *headOfList )->firstname ) && ( lname == ( *headOfList )->lastname ) ) {
            tempPtr = *headOfList;
            *headOfList = ( *headOfList )->next;
            free( tempPtr );
            tempPtr = *headOfList;
            *headOfList = ( *headOfList )->next;
            free( tempPtr );
            return fname, lname;
        }
        else {
            previousPtr = *headOfList;
            currentPtr = ( *headOfList )->next;
            
                while ( ( currentPtr != NULL && currentPtr->firstname != fname ) && ( currentPtr != NULL && currentPtr->lastname != lname ) ) {
                    previousPtr = currentPtr;
                    currentPtr = currentPtr->next;
                }
            
                if ( currentPtr != NULL ) {
                    tempPtr = currentPtr;
                    previousPtr->next = currentPtr->next;
                    free( tempPtr );
                    tempPtr = currentPtr;
                    previousPtr->next = currentPtr->next;
                    free( tempPtr );
                    return fname, lname;
                }
        }
        return '\0';
    }
    
    void searchlist () {
    }
     
    void print( Customerptr *headOfList) {
       printf("First name: ->%s \n", ( *headOfList )->firstname );
       printf("Last name:  ->%d \n", ( *headOfList )->lastname );
       printf("Phonenumber:   ->%d \n", ( *headOfList )->phonenumber );
       printf("Company/Organization:   ->%d \n", ( *headOfList )->comporg );
       printf("Email:   ->%d \n\n", ( *headOfList )->email );
    }
    
    
    void printlist( Customerptr *headOfList ) {
         Customerptr currentPtr;
         while( currentPtr != NULL )           
         {
              print( headOfList );           
              *headOfList = ( *headOfList )->next;            
         }
    }
    
    
    int isEmpty( Customerptr cptr ) {
    return cptr == NULL;
    }
    
    writefile(Customerptr *headOfList ) {
         Customerptr currentPtr;
         FILE *fp;
         char c;
         
         fp = fopen("customers.txt", "w");
         
         if (fp == NULL) {                                                        // If file doesn't exist
            printf("Cannot open file. \n");
            return(0);
         }
         
         while( currentPtr != NULL )           
             {
                  fprintf (fp, "%s \n" , print( headOfList ));           
                  *headOfList = ( *headOfList )->next;            
             }
    }
    My question is, in adding the linked list to the file, what am I doing wrong? & Also when searching the linked list to display the results with either first or last name, say for example "Feng", displays all results with first or last name "Feng".

    Please help

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The only kind of scanf call you seem make is this type:

    scanf( "%c" , whatever );

    You think that this will properly grab all the input you'll ever need, such as entire strings? You need to think harder about how scanf works. In brief, scanf can grab words (%s), integers (%d or %i), integers in different bases (say %o for octal) floating point and double floating point numbers (%f and %lf). There's a lot more to know about it though.

    Without getting good data in your lists, all the other things you need to do, like compare names with "Feng", will go wrong.

    print( ) also only prints to your screen, or whatever stdout happens to point at. If you want to successfully write a file, you need to provide fprintf with the data it needs directly, as that seems to be the function you want to use for this.
    Last edited by whiteflags; 08-04-2010 at 08:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using strcmp and strlen in a Linked List
    By matrixx333 in forum C Programming
    Replies: 4
    Last Post: 11-23-2009, 04:13 AM
  2. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  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

Tags for this Thread