help with program thats killing me

This is a discussion on help with program thats killing me within the C Programming forums, part of the General Programming Boards category; thanks for the help all, i had to do a phonebook program and it just worked me. basically the main.c ...

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

    help with program thats killing me

    thanks for the help all, i had to do a phonebook program and it just worked me.

    basically the main.c file is for the menu and call functions.

    the main.h file holds the struct.

    the process.c file has the actual functions.

    process.h holds the prototypes for those functions.

    this program is a phonebook that allows the user to add entries, edit entries, and print the file. thanks for the help, i need it.

    main.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "main.h"
    
    
    int find(c*, int);
    
    int main() {
    
        int entry;
        c * newc;
        int choice;
        int counter = 0;
    
    
        
        while (choice != 4) {
        printf("Welcome to the Phonebook\n\n");      
        printf("1)Add entry.\n");
        printf("2)To edit entry.\n");
        printf("3)Print Phonebook.\n");
        printf("4)Quit.\n");
        scanf("%d", &choice);
    
    
                     if (choice == 1){
                                if(counter == 0)  {
                                  newc = (c*) calloc(1,sizeof(c));   }
                                  else
                                      newc = (c*) realloc(newc, (counter+1) * sizeof(c));
                                      
                                add (newc, counter);
                                counter++;                                                            
                     }
       
               
                     if (choice == 2) {
                                 int i;
                                 char firstNameSearch[20], lastNameSearch[20];
             
       
                                       printf("Enter the first name: ");
                                       scanf("%s", firstNameSearch);
                                       printf("Enter the last name: ");
                                       scanf("%s", lastNameSearch); 
            
                                                   for( i = 0; i < counter; i++)
                                                   {
                                                        if( strcmp(firstNameSearch,newc[i].first) && strcmp(lastNameSearch,newc[i].last) )
                                                        return i;
                                                        
                                                          else {                                                                                                                        
                                                                 printf("That person is not a contact.\n");
                                                               }                                                                                  
                                                                                            edit(newc,entry);                           
                                                   }
                   }
                                            
                     if (choice == 3)
                                {
                             write(newc, counter);
                             printf("Phonebook written to file.\n");
     
                                    
                                    }
                          
                          }
    
       
      system("PAUSE");
                     
      return 0;
                     
                     }
    main.h
    Code:
    typedef struct contact {
        char first[20];
        char last[20];
        char email[20];
        char phone[12];
                            } c;
    process.h
    Code:
    #include "main.h"
    
    void add( c*, int );   
    void edit( c*, int ); 
    void write_file( c*, int );
    process.c
    Code:
    #include "main.h"
    
    
    
    
    void add(c* contacts, int entry)  {
         printf("Enter first name: ");
         scanf("%s", contacts[entry].first);
    
         printf("Enter last name: ");
         scanf("%s", contacts[entry].last);
         
         printf("Enter email: ");
         scanf("%s", contacts[entry].email);
          
         printf("Enter phone: ");
         scanf("%s", contacts[entry].phone);
       }
                                                               
    void edit(c* contacts, int entry) {
                     printf("Please enter the new email: ");
                     scanf("%s",contacts[entry].email);
                                     
                     printf("Please enter the new phone number: ");
                     scanf("%s",contacts[entry].phone);
                                     
                     printf("Thank you for editing the info.");
    }
    
    void write(c* contacts, int num) {
                            FILE * out_file;
                            out_file = fopen( "H:\\school\\myprogram\\phonebook.txt", "w" );
                            int i;
         
                            for (i = 0; i < num; i++){
                                 fprintff (out_file,"%s \t%s \t%s \t%s\n",contacts[i].first, contacts[i].last, contacts[i].email, contacts[i].phone);
                                 }
                                 fclose(out_file);
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    main.c line 3:
    Code:
    #include <string.h>
    main.c line 4:
    Code:
    #include "process.h"
    process.c line 2:
    Code:
    #include <stdio.h>
    process.c line 38:
    Code:
     fprintf (out_file,"&#37;s \t%s \t%s \t%s\n",contacts[i].first, contacts[i].last, contacts[i].email, contacts[i].phone);
    process.h line 4:
    Code:
    void write( c*, int );
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So what's the question?

    "ffprintf" doesn't exist; doubtlessly you meant "fprintf".

    Code:
    out_file = fopen( "H:\\school\\myprogram\\phonebook.txt", "w" );
    If phonebook.txt is in the same directory as your program, you can just use
    Code:
    out_file = fopen( "phonebook.txt", "w" );
    since fopen() takes relative paths too.

    Code:
    scanf("&#37;s", contacts[entry].first);
    scanf() is not the best way to read a string from the user. It suffers from buffer overruns (unless you use a special format specifier) and only reads one word from the input.

    Code:
    char phone[12];
    For 123-123-1234 you need 13 bytes, not 12. Remember your NULLs.

    Code:
    if( strcmp(firstNameSearch,newc[i].first) && strcmp(lastNameSearch,newc[i].last) )
    Because strcmp() returns 0 when it finds a match, you probably want
    Code:
    if( strcmp(firstNameSearch,newc[i].first) == 0 && strcmp(lastNameSearch,newc[i].last) == 0 )
    Don't cast the return value of malloc(), realloc(), and calloc(). See the FAQ for why.

    Code:
    int main() {
    
        int entry;
        c * newc;
        int choice;
        int counter = 0;
    
    
        
        while (choice != 4) {
    What's to say that the uninitialized value isn't 4 to being with? Use a do-while loop or initialize choice.

    Code:
                                if(counter == 0)  {
                                  newc = (c*) calloc(1,sizeof(c));   }
                                  else
                                      newc = (c*) realloc(newc, (counter+1) * sizeof(c));
    realloc() works just fine in either case.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm glad you got it working.

    You may want to consider these comments:
    - indent your code a little bit more sensible.

    - initialize "choice" to something that is guaranteed to not be 4. [If by random chance, choice happens to be 4, which it could be, you'd immediately exit the program]

    - Use a more descriptive name for your type "c", as it doesn't REALLY say much. Contact may be a good name.

    - Use the same name of the function in your header file as in the code:
    Code:
    void write_file( c*, int );
    and
    Code:
    void write(c* contacts, int num)
    - Not have extra f on the end of fprintf()
    Code:
    fprintff (out_file, .... );
    - Would it not be a good idea to have a "load" file as well, so that your saved file can be loaded if you want to update your phone directory?

    - Allow for longer e-mail addresses, as none of the three I've got at the moment will fit in 20 chars. Ok, so I may have a 9 char long last-name, but my first name is only 4, so not excessive.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    Code:
                                if(counter == 0)  {
                                  newc = (c*) calloc(1,sizeof(c));   }
                                  else
                                      newc = (c*) realloc(newc, (counter+1) * sizeof(c));
    realloc() works just fine in either case.
    Nitpick: only if newc is initialized to NULL (or 0) - which would be a good idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    thank you guys, again you help me out alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. free() is killing my program?
    By alwut in forum C Programming
    Replies: 12
    Last Post: 12-24-2007, 03:50 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21