Thread: Classic phone book( and yes this is MY source code not ripped off)

  1. #46
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Pelles C is a good IDE for learning on. I don't use it myself, so I don't know much about it. I know it has syntax highlighting from the screen shots, but can't find any info on enabling auto indent or warnings, though I'm quite sure those features exist. Check the Pelles C help files or check the forum on their website.

    Line 89 in your newest post is still wrong, it should only have one = sign.

    Line 130 in your newest post doesn't make sense either. You don't put a conditional statement with an else, only with if or else if. else is for everything that hasn't been previously covered, so it doesn't need a condition. What you're really getting is:
    Code:
    if (a == 'a')
    {
    }
    else if (a == 'd')
    {
    }
    else if (a == 'p')
    {
    }
    else
        (a != 'a' || a != 'd' || a != 'q' || a != 'p' || a != 'z' || a != 's');
    That line in red is not the "else condition", but the statement that gets executed if none of the ifs or else ifs get executed, it's the body of the else clause. It is another statement that does nothing, it's just a bunch of comparisons, no calculations/assignments. Try getting in the habit of always using curly braces around every if, else if, else and loop body, to help you keep track of what statements belong where.

    Your delete function has a bug. You always copy blanks to book[i].surname and book[i].number, even if i is invalid. The else only applies to the next one command, unless you surround surround several commands with curly braces:
    Code:
    if (i < 0 || i > 20)
        print "invalid"
    else {
        strcpy name
        strcpy surname
        strcpy number
    }
    This is a perfect example of how auto indent would help you find this error. The first strcpy would be indented further than the second two, and the second two would line up with the else, showing you they're not part of the if/else.

    Also, your print loop has issues. It uses i as the loop counter, but you also use i to track the number of entries. Try adding a couple names, then printing, then adding again, you'll see that after printing, you always insert entry number 20. Call i num_entries, since it's a sensible name that reflects what the variable is for. Also, move your print code into a function on it's own. Pass in num_entries so you know when to stop printing, then you wont have to worry about printing all those blank entries.
    Last edited by anduril462; 03-14-2012 at 02:42 PM.

  2. #47
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    Back at it again, Most bugs kinda worked out. Update on the code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //strcmp
    
    
    
    
    
    void delete(void);
    
    struct entry
    
         {
        char name[12];
    
        char surname[12];
            
        char number[12];
    
        };
    
    
    struct entry book[20];
    
    
    
    int Createnew(int i)
            {
    
            printf("Entry Number %i \n",i);
    
                printf("Enter Name\n");
    
            scanf("%s", book[i].name);
    
            printf("Enter Surname\n");
    
            scanf("%s", book[i].surname);
    
            printf("Enter Phone Number\n");
    
            scanf("%s", book[i].number);
    
            i++;
            
            printf("%i\n",i);
    
            return i;
            
            }
    
    void delete()
                {
    
                int i;
    
    
                printf("Enter index of entry to delete\n");
    
                scanf("%i",&i);
    
        
                if( i<0 || i>20)
            
                printf("invalid Entry\n");
    
                else{
    
                strcpy(book[i].name, " ");
                strcpy(book[i].surname," ");
                strcpy(book[i].number," ");}
            
                }
    
    void printfunct(num_entries)
                {
    
                int i;
    
                i=num_entries;
    
                for(i=0;i<20;i++)
    
                printf("%s\n%s\n%s\n\n\n",book[i].name,book[i].surname,book[i].number);
                        
                }
    
    
    
    int main(void)
    {
    
    int num_entries;
    num_entries=0;
    
    int i;
    i=0;
    
    char a;     /* command line input */
    
    a='z';
    
    for(i=0; i<=num_entries; i++)
        {
            strcpy(book[i].name, " ");
    
             strcpy(book[i].surname," ");
    
             strcpy(book[i].number," ");
    
        }
            i=0;
            while(a!='q')
            {
    
                  printf(" Enter a command\n 'A' : Add an Entry \n 'D' : Delete an Entry\n 'S' : Sort Entries \n 'P' : Print the phone book\n 'Q' : Quit\n\n");
    
                  scanf("%c",&a);
            printf("\n\n");
    
    if(a=='a')
    
                     {
        
                i=Createnew(i);
                num_entries=i;
                     }
        
                    else if(a=='d')
                    {
                    delete();
                    printf("Entry Deleted\n");
                    }
        
                        else if(a=='p')
    
                        
    
                        {
                        printfunct(num_entries);
                        }
        
                            else 
                
                            printf("unknown command\n");
    
    
                /* if(a=='s')*/
        
            }
    
            printf("\nGood Bye!!\n");    
             return 0;
    
    }

  3. #48
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Hmm, four pages of advice in and I still see:
    • Awful indentation
    • Huge waste of stack/global memory space with a global array of structs (learn to pass variables already)
    • Scanf() for getting character input.
    • Ignoring newlines in user input.
    • Usage of a while() loop for input when a do-while loop is the obvious standard for this.
    • No bounds checking when reading into 12-byte arrays.
    • Using the overhead of strcpy()'ing a space to "delete". Why not:
      Code:
      book[i].name[0] = '\0';
      because that's essentially what strcpy is doing.


    Edit: fixed, was looking at another quote
    Last edited by memcpy; 03-14-2012 at 07:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with phone book console app in C
    By cb0ardpr0gr^mm3r in forum C Programming
    Replies: 6
    Last Post: 11-11-2010, 09:09 PM
  2. Phone book
    By Nightmaresiege in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2008, 05:12 PM
  3. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  4. Phone Book
    By FerrariF12000 in forum C Programming
    Replies: 7
    Last Post: 12-10-2003, 12:07 AM
  5. phone book directory 2
    By arangawawa in forum C Programming
    Replies: 4
    Last Post: 08-01-2003, 05:32 PM