Thread: function not working correctly

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    function not working correctly

    hi people, im making an address book with link lists and I think I have everything going well. The only thing that is messing up is when I try to exit (option 8) the program crashes and I am not sure why.

    3 files, main.c, process_functions.c, process_functions.h

    after going from main.c to process_functions.c and you edit info and save it and you hit option 8 you're supposed to be able to go back to main.c, but it just crashes, thanks for all the help.

    main.c, menu to enter address book or quit and save
    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "project_functions.h"
    
    int main(){
      char response;
      int option;
      char choice[2];
      c * addressBook = NULL;
      c * overwrite;
      c * current;
      c * next;
      int save = 0;
      int finalSave = 0;
      
      do{
         
          printf("Welcome to the Address Book\n\n");
          printf("Please pick an option\n\n");
          printf("1)  Enter Address Book\n");
          printf("2)  Quit\n");
          
         
          do{
               scanf("&#37;d",&option);       
          }while( !(option == 1 || option == 2) ); 
    
         
          if(option == 1){
               addressBook = addressBookMenu(addressBook);  
          }  
          
      }while(option != 2); 
      
      c * person;
      current = addressBook;
      overwrite = addressBook;
      while (addressBook != NULL){
            person = (c *) malloc(sizeof(c));
            person->nextContact = NULL;
            save = 0;
            FILE *in_file;
            in_file = fopen( "address.txt", "r" );
           
            while( fscanf( in_file, "%s%s%s%s", person->first, person->last, person->cell, person->home ) != EOF && save != 5){
                   printf("strcmp(%s, %s) = %d\n",person->first, addressBook->first ,strcmp(person->first, addressBook->first));
                   if (strcmp(person->first, addressBook->first) == 0){
                      if (strcmp(person->last, addressBook->last) == 0){
                         if (strcmp(person->cell, addressBook->cell) == 0){
                            if (strcmp(person->home, addressBook->home) == 0){
                               printf("%s is in the file and list\n",addressBook->first);
                               save = 5;
                               system("PAUSE");
                            }
                         }
                      }
                   }
            }
            if (save != 5){
               finalSave = 10;
            }
            free(person);
            addressBook = addressBook->nextContact;
            fclose(in_file);
      }
     
      if (finalSave == 10){
         printf("\nThe address book has changed.\n");
         printf("Would you like to save?\n\n");
         printf("  a) Yes\n");
         printf("  b) No\n");
         printf("Selection: ");
         
         do{
            scanf("%c",&response);       
         }while( !(response == 'a' || response == 'b') );
        
         if(response == 'a'){
            
            printf("Would you like to overwrite file with existing link list of contacts,\n");
            printf("or append the current contacts to the end of the file?\n\n");
            printf("  a) Append\n");
            printf("  w) Overwrite\n");
            printf("Selection: ");
            do{
               scanf("%s", choice);
            }while (strcmp(choice, "a") != 0 && strcmp(choice, "w") != 0);
         
            write_file(addressBook, choice);
            printf("File Saved\n");
            printf("Goodbye\n\n");
         }
         else {
              printf("Goodbye\n\n");
         }
      } 
      else {
      printf("Goodbye\n\n");
      
      }
      system("PAUSE");
     
      while(current != NULL){
          next = current->nextContact;
          free(current);
          current = next;                          
      }	
      return 0;
    }



    process_functions.h, header file that contains the structure and prototypes of functions in process_functions.c file
    Code:
      typedef struct contact {
        char first[20];
        char last[20];
        char cell[13];
        char home[13];
        struct contact *  nextContact;
        } c;
                            
    c* addressBookMenu(c*);
    c* add(c*);
    c* edit(c*);
    c* del(c*);
    c* insert(c*,c*);
    void display(c*);
    c* findPerson(c *);
    void write_file( c*, char[]);
    c* read_file( c*);
    c* insertRead(c *);
    c* delTop(c *);
    process_functions.c, the big guy, its option 4, viewing the contacts that messes everything up.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "project_functions.h"
    
    c* addressBookMenu(c* contacts){
        char response;
        int counter = 0;
        int index;
        char choice[2];
                    
        do{               
            printf("\n\n1) Add new contact\n");
            printf("2) Edit contact\n");
            printf("3) Delete a contact\n");
            printf("4) View Contacts\n");
            printf("5) Save Contacts\n");
            printf("6) Delete all contacts\n");
            printf("7) Read from a file\n");
            printf("8) Exit\n");
            printf("\n\nPlease enter your selection: ");
           
            do{
             
               scanf("%d",&response);
               
            }while( response != 1 && response != 2 && response != 3 && response != 4 && response != 5 && response != 6 && response != 7 && response != 8 );
          
                             
            
                            if(response == 1){
                            contacts = add(contacts);
                            }
                            
                            else if(response == 2){
                           edit(contacts); 
                           }
            
                           else if(response == 3){
                           contacts = del(contacts);
                           }
            
                           else if(response == 4){
                           display(contacts);               
                           }
                           else if(response == 5){
                      
                       
                                          printf("Overwrite file or add to end of file.\n\n");
                                          printf("  a) Add to end\n");
                                          printf("  w) Overwrite\n");
                                          printf("Selection: ");
                                          do{
                                             scanf("%s", choice);
                                             }while (strcmp(choice, "a") != 0 && strcmp(choice, "w") != 0);
                                             write_file(contacts, choice);
                                             printf("Information stored.\n");
                                             system("PAUSE");
                            }
      
                           else if(response == 6){
                 
                           int i;            
                               for(i = 0; contacts != NULL; i++){
                                     c * previous = NULL;
                                     c * current = contacts;
                 
                                     if(previous == NULL){
                                     contacts = contacts->nextContact;
                                     }
                                     else{
                                     previous->nextContact = current->nextContact;
                                     }
                                     free(current);
                                     }
                                     printf("List emptied.\n");
                                     system("PAUSE");
                                     }
                         else if(response == 7){
                         contacts = read_file(contacts);
                         }           
        
        }while(response != 8);                                                           
        return contacts;                  
    }
    
    c* add(c* addresses){
       c * contact = (c*) calloc(1,sizeof(c));
       c * current = addresses;
       c * previous = NULL;
       char name1[20];
       char name2[20];
       printf("\n\nFirst name: ");
       scanf("%s",contact->first);
       printf("Last name: ");
       scanf("%s",contact->last);   
       printf("Cell number: ");
       scanf("%s",contact->cell);
       printf("Home number: ");
       scanf("%s",contact->home);   
       
       printf("Information added.\n");
       system("pause");
          
       if(current == NULL)
            return insert(previous,contact);
          
       while(current != NULL){       
            strcpy(name1,contact->last); 
            strcat(name1,contact->first);
            strcpy(name2,current->last); 
            strcat(name2,current->first);    
                
            if( strcmp(name1,name2) < 0 ){
                if(previous == NULL){
                     contact->nextContact = current;
                     return contact;             
                }            
                else{
                     previous = insert(previous,contact);      
                     return addresses;
                }
            }
                    
            previous = current;            
            current = current->nextContact;                                                
       }   
    
       previous = insert(previous,contact);
       return addresses; 
    } 
    
    c* edit(c* contacts){
         c * person;
         person = findPerson(contacts);
         
         if(person == NULL){
             printf("Not found.\n");
             system("pause");
             return contacts;
         }
               
         printf("New cell number: ");
         scanf("%s",person->cell);
         printf("New home number: ");
         scanf("%s",person->home);
         printf("Information edited.\n");
         system("pause");      
    }
    
         
    c* del(c* contacts){
         c * person;
         c * previous = NULL;
         c * current = contacts;
         person = findPerson(contacts);
         
         if(person == NULL){
             printf("Not found.\n");
             system("pause");
             return contacts;
         }
            
         while(current != person){
             previous = current;
             current = current->nextContact;                            
         }              
    
         if(previous == NULL){
             contacts = contacts->nextContact;
         }
         else{
             previous->nextContact = current->nextContact;
         }
      
         free(current);
    
         printf("Deleted.\n");
         system("pause"); 
         return contacts;       
    }
    
    
    c* insert(c* loc,c* node){
         c * temp;
         if(loc == NULL){
             node->nextContact = NULL;
             loc = node;   
         }
         else{
             node->nextContact = loc->nextContact;
             loc->nextContact = node;    
         }
         
         return loc;
    }
    //somewhere in here------------------------------------------------------------------------------------
    void display(c* addresses){
       c * next = addresses;
       while(next != NULL){
           printf("%s %s\nCell Number: %s\nHome Number: %s\n\n",next->first,next->last,next->cell,next->home);
           next = next->nextContact;                            
       }
       system("pause");
    }
    //---------------------------------------------------------------------------------------------------
    
    c* findPerson(c * contacts){
         int i;
         char firstNameSearch[20], lastNameSearch[20];
         c * next = contacts;
             
         printf("First name: ");
         scanf("%s", firstNameSearch);
         printf("Last name: ");
         scanf("%s", lastNameSearch); 
    
         while(next != NULL){
             if( !strcmp(firstNameSearch,next->first) && !strcmp(lastNameSearch,next->last) )
                 return next;
             next = next->nextContact;                            
         }     
         return NULL;      
    }
    
    void write_file( c* contacts, char write[]){
         
         c * person = contacts;
         FILE * out_file;
         out_file = fopen( "address_book.txt", write );  
         
         int i;
         for(i = 0 ; person !=NULL ; i++){
            fprintf(out_file,"%s\t%s\t%s\t%s\n", person->first, person->last, person->cell, person->home );  
            
            fclose(out_file);  
            person = person->nextContact;
         }
    }
    
    c* read_file( c* contacts){
         int i;
         char blah[20];
         c * next;
         FILE *in_file;
         in_file = fopen( "address_book.txt", "r" );  
         
         if( in_file == NULL){
             printf("\naddress_book.txt isn't found.\n");
             return;
         }
    
         if(contacts == NULL){
                     contacts = (c*) malloc(sizeof(c));
                     contacts->nextContact = NULL;
         }
    
         while( fscanf( in_file, "%s%s%s%s", contacts->first, contacts->last, contacts->cell, contacts->home ) != EOF ) {
                next = (c*) malloc(sizeof(c));
                next->nextContact = contacts;
                contacts = next;
                
         }     
    
         contacts = delTop(contacts);
         printf("Information read.\n");
         system("PAUSE");
         fclose(in_file);
         return contacts; 
    }
    
    c* delTop(c * contacts){
                int i;
       for(i = 0; i < 1; i++){
            c * previous = NULL;
            c * current = contacts;
                 
            if(previous == NULL){
               contacts = contacts->nextContact;
            }
            else{
                 previous->nextContact = current->nextContact;
            }
                 free(current);
       }
       return contacts;
    }
    Last edited by mackieinva; 09-29-2007 at 08:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM