Thread: making a phone book directory

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    24

    making a phone book directory

    i need to write a program that reads in a file containing a person's name, address, and phone number and then be able to:

    following options should be in main menu

    1.add friends
    2.modify friend details
    3.display all my friends
    4.search friends
    5.remove friends from buddy book
    6 exit

    Any help that can be provided would be greatly appreciated. I just need a little help getting started(reading the file into a list).

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    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

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    My $0.02 vis, "design stages":

    DO NOT create the menu with all the options first. I see this pretty often here, perhaps because it is the easiest part and gives you something to look at while you're working. It also seems to tempt people to then implement everything at once, which creates a complete mess and I'm sure ends up taking twice as long and full of unnoticed mistakes.

    So start out doing only one thing, and pretend there are no menu options. Eg, write a program that asks two questions:
    1) name to add
    2) number to add
    and make this program append that to a file, then exit.

    When you've got that working, you start with the menu. At this stage, you only have two options:

    1. add friends
    3. display all friends

    #1 is what you already did. When number 1 and 3 work with just name + phone number, then add the address in. Presumably an address can be left blank, but now you will want to add addresses for the test cases you've done, so once you can "add new" and "display all" for name + number + address, you do

    2. modify friend details

    Which there are three fields that can be modified.

    Number 4 and 5 are harder. Make sure you get all this working smoothly before you think about 4 & 5.
    Last edited by MK27; 12-29-2009 at 10:51 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    24
    this small part i coded

    Code:
    #include <string.h>
    int i;
    int number_of_entries;
    typedef struct
    {
            char first [15];
    		  char last [15];
    		  char phone [15];
    		  }
    		  phonebook;
    
    main ()
    {
    phonebook entry[50];
    int response;
    
    printf("\n\tPhonebook\n\n");
    printf("(1)Add Friend\n");
    
    scanf("%d", &response);
    
    switch (response)
    {
    case 1:
         number_of_entries++;
    	  printf("Enter First Name: ");
         scanf("%s", entry[i].first);
    	  printf("Enter Last Name: ");
    	  scanf("%s", entry[i].last);
    	  printf("Enter Phone Number: ");
    	  scanf("%s", entry[i].phone);
    	  return 0;
    
    }
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmmm. You have a return in your switch/case which should probably not be there -- I think you meant to use break?

    Beware some problems which may arise with scanf();

    http://206.251.36.107/programming/stdin_buffer.mhtml
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    24
    Quote Originally Posted by MK27 View Post
    Hmmm. You have a return in your switch/case which should probably not be there -- I think you meant to use break?

    Beware some problems which may arise with scanf();

    STDIN pitfalls
    i didn't get it friend.
    can you suggest how should i start coding?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Replace the current "return 0;" , with "break;"

    Then change main() to int main (), and add "return 0; as the last line, immediately before the last }.

    In C, main always should return an integer.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I would also consider to wrap the code in the case statement in a function. That will keep things clearer when you add case statements later, and also make it easier to test them out of context.

    You'll also need to increment i after you add info and make sure that i doesn't exceed the size of your phonebook array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Must read book!
    By RealityFusion in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-15-2004, 09:05 PM
  3. phone directory program
    By cdummy in forum C Programming
    Replies: 1
    Last Post: 04-13-2002, 11:23 AM
  4. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM