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

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    43

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

    Completely new to any sort of programming at all, the problem i am having right now, is trying to figure out how to not exit program after i add a new entry. I know probably simple fix. Any suggestions for the sorting part i will need later? Quick sort is worth bonus. Dont know how to use pointers at all.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //strcmp
    //strcpy
    
    int i;
    
    void Createnew(void);
    
    void delete(void);
    
    struct entry
    
         {
            char name[12];
    
            char surname[12];
            
            char number[12];
        
    
        };
    
    struct entry f[20];
    
    
    
    void Createnew(void)
    {
    
           printf("Enter Name\n");
    
        i=0;
    
        scanf("%s", f[i].name);
    
        printf("Enter Surname\n");
    
        scanf("%s", f[i].surname);
    
        printf("Enter Phone Number\n");
    
        scanf("%s", f[i].number);
    
    
    }
    
    void delete()
        {
    
    
    
    
        printf("Enter index of entry to delete\n");
    
        scanf("%i",&i);
    
        
        if( i<0 || i>20)
            
            printf("invalid Entry\n");
    
            else
    
            (f[i].name," ");
            
        }
    
    
    int main(void)
    {
          char a;     /* command line input */
    
        for(i=0; i<20; i++)
        {
                     strcpy(f[i].name, " ");
    
                     strcpy(f[i].surname," ");
    
                      strcpy(f[i].number," ");
    
        }
    
    
    
            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);
    
        if(a=='a')
          {
    
                            Createnew();
          }
         else if(a=='q')
        {
            printf("\nBye\n");    
             return 0;
        }
        else if(a=='d')
        {
        delete();
        }
    
        else 
    
        {
            printf("unknown command\n");
        }
    
        /* if(a=='s')*/
    
        
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    Okay so i just copied and pasted
    Code:
            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);
     
        if(a=='a')
          {
     
                            Createnew();
          }
         else if(a=='q')
        {
            printf("\nBye\n");    
             return 0;
        }
        else if(a=='d')
        {
        delete();
        }
     
        else
     
        {
            printf("unknown command\n");
        }
    a second time, but that does not seem like the right answer, and doesnt really work because it takes my phone number as the next command

  3. #3

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    okay, so while loop the whole program until Char a==q and get rid of the if a==q then return 0....

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //strcmp
    //strcpy
    
    int i;
    
    void Createnew(void);
    
    void delete(void);
    
    struct entry
    
         {
            char name[12];
    
            char surname[12];
            
            char number[12];
        
    
        };
    
    struct entry f[20];
    
    
    
    void Createnew(void)
    {
    
           printf("Enter Name\n");
    
        i=0;
    
        scanf("%s", f[i].name);
    
        printf("Enter Surname\n");
    
        scanf("%s", f[i].surname);
    
        printf("Enter Phone Number\n");
    
        scanf("%s", f[i].number);
    
    
    }
    
    void delete()
        {
    
    
    
    
        printf("Enter index of entry to delete\n");
    
        scanf("%i",&i);
    
        
        if( i<0 || i>20)
            
            printf("invalid Entry\n");
    
            else
    
            (f[i].name," ");
            
        }
    
    
    int main(void)
    {
          char a;     /* command line input */
    
        a=='z';
    
        for(i=0; i<20; i++)
        {
                     strcpy(f[i].name, " ");
    
                     strcpy(f[i].surname," ");
    
                      strcpy(f[i].number," ");
    
        }
    
    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);
    
        if(a=='a')
          {
    
                            Createnew();
          }
        
        else if(a=='d')
        {
        delete();
        }
    
        else 
    
        {
            printf("unknown command\n");
        }
    
    
        /* if(a=='s')*/
    
        
    
                }
    
            printf("\nBye\n");    
             return 0;
    }
    Tried the while loop, am i using the ! operator wrong?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "!==" is likely not correct try "!=".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    Thanks dude, its always the simple mistakes

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    Okay so i am still having this problem that after i enter name, surname, and number, I get unknown command then the options list then options list agian, and its ready to take new command. Cannot seem to fig;ure out why. Any thoughts?

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    Sorry, here is the updated code....
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //strcmp
    //strcpy
    
    int i;
    
    void Createnew(void);
    
    void delete(void);
    
    struct entry
    
         {
            char name[12];
    
            char surname[12];
            
            char number[12];
        
    
        };
    
    struct entry f[20];
    
    
    
    void Createnew(void)
    {
    
           printf("Enter Name\n");
    
        i=0;
    
        scanf("%s", f[i].name);
    
        printf("Enter Surname\n");
    
        scanf("%s", f[i].surname);
    
        printf("Enter Phone Number\n");
    
        scanf("%s", f[i].number);
    
    
    }
    
    void delete()
        {
    
    
    
    
        printf("Enter index of entry to delete\n");
    
        scanf("%i",&i);
    
        
        if( i<0 || i>20)
            
            printf("invalid Entry\n");
    
            else
    
            (f[i].name," ");
            
        }
    
    
    int main(void)
    {
          char a;     /* command line input */
    
        a=='z';
    
        for(i=0; i<20; i++)
        {
                     strcpy(f[i].name, " ");
    
                     strcpy(f[i].surname," ");
    
                      strcpy(f[i].number," ");
    
        }
    
        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);
    
        if(a=='a')
          {
         Createnew();
          }
        
            else if(a=='d')
            {
            delete();
            }
        
                else if(a=='p')
    
                    {
                    printf("%s\n%s\n%s\n",f[0].name,f[0].surname,f[0].number);
                    }
        
                else if(a!='a' || a!='d' || a!='q' || a!='p' ||a!='z' ||a!='s' )
    
                {
                printf("unknown command\n");
                }
    
    
        /* if(a=='s')*/
    
        
    
                }
    
            printf("\nBye\n");    
             return 0;
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    These suck:
    Code:
    int i;
    
    void Createnew(void);
     
    void delete(void);
    
    struct entry f[20];
    Why? Because they are unnecessarily global. This makes it harder for you to reason about your program and harder for you to reuse your code (not that you'll need to here, but just saying for the future). Furthermore, a name like f for such an array is really... f'ed up. Such short and undescriptive names are typically only used for loop counters and in generic mathematical computations.

    Rather, design your functions to use local variables, and have parameters where necessary. For example:
    Code:
    void create_entry(struct entry *entries, int i); /* Create entry at index i */
    Now in the main function:
    Code:
    int main(void)
    {
        /* ... */
        struct entry entries[20]; /* 20 should be a named constant instead */
        int entries_size = 0;
        create_entry(entries, size++);
        /* ... */
    }
    Personally though, I would have create_entry be concerned about creating a single entry:
    Code:
    void create_entry(struct entry *entries);
    Then in main the function call will be:
    Code:
    create_entry(&entries[size++]);
    But that's up to you. Remember to check that the size does not exceed 20.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    As for the create_entry, i do not know how to use pointers at this time, its next chapter. The rest of what you have said makes alot of sense, i my try and look ahead and see if i might figure out enough about pointers to use in this program as it might actually make it alot easier.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    UGH!!! Still struggling with just getting the bugs out of what i have so far. I have not started on the sorting part of the assignment yet. Anyways, it appears im only storing the last entry and cannot figure out why. Other problems include, the "Unkown Command" coming up when its not needed, and "P" printing my one stored entry and 19 more blank cells. Anyone have any suggestions for these?
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //strcmp
    //strcpy
    
    int i;
    
    void create_entry(struct entry *entries, int i); /* Create entry at index i */
    
    void delete(void);
    
    struct entry
    
         {
            char name[12];
    
            char surname[12];
            
            char number[12];
        
    
        };
    
    struct entry book[20];
    
    
    
    void Createnew(void)
    {
    
           printf("Enter Name\n");
    
        i=0;
    
        scanf("%s", book[i].name);
    
        printf("Enter Surname\n");
    
        scanf("%s", book[i].surname);
    
        printf("Enter Phone Number\n");
    
        scanf("%s", book[i].number);
    
    
    }
    
    void delete()
        {
    
    
    
    
        printf("Enter index of entry to delete\n");
    
        scanf("%i",&i);
    
        
        if( i<0 || i>20)
            
            printf("invalid Entry\n");
    
            else
    
            (book[i].name," ");
            
        }
    
    
    int main(void)
    {
          char a;     /* command line input */
    
        a=='z';
    
        for(i=0; i<20; i++)
        {
                     strcpy(book[i].name, " ");
    
                     strcpy(book[i].surname," ");
    
                      strcpy(book[i].number," ");
    
    
        }
    
        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);
    
        if(a=='a')
          {
         Createnew();
          }
        
            else if(a=='d')
            {
            delete();
            printf("Entry Deleted\n");
            }
        
                else if(a=='p')
                    for(book[i].name!=" ";i<20;i++)
                    {
                    printf("%s\n%s\n%s\n",book[i].name,book[i].surname,book[i].number);
                    }
        
                else (a!='a' || a!='d' || a!='q' || a!='p' ||a!='z' ||a!='s' );
    
                
                
                printf("unknown command\n");
    
                
    
    
    
        /* if(a=='s')*/
        
                
    
            }
    
            printf("\nBye\n");    
             return 0;
    
    }

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    So was looking at it, Do i need i to be declared as static variable?

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    43
    grrr.... so i tried with i being static, no differnce so then
    Code:
    void Createnew(void)
    {
    
           printf("Enter Name\n");
    
        i+=i;
    and that was even worse then before

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Tc Diehl View Post
    Do i need i to be declared as static variable?
    That is almost never the correct answer.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

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