Thread: simple phonebook?!..X_x

  1. #1
    Registered User
    Join Date
    Jan 2011
    Location
    ??
    Posts
    3

    Question simple phonebook?!..X_x

    hi i'm a newbie noobie in C...anyways i'm trying to create program that can hold contact infos. of persons in short a simple phonebook...and i want to make my program same as with this:
    v=Z5i07EF_Nh4YouTube - c language phone book program


    well i'm trying soO hard to make it like that but i cant figure out how did the person made that..anyways i need help please someone improve my code cuz i really need it after 5 hours..

    here's my code:

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <string.h>
    
    #define clrscr()
    
    struct teledir{
            long teleno ;
            char name[20];
            char address[20];
    };
    
    
    
    main()
    {
        struct teledir t;
        FILE *fp;
        long tno;
        char sname[20],c;
        int ch;
        while(1)
        {
            clrscr();
            printf("\t\t\t\tMENU\n");
            printf("1. TO ADD THE RECORD IN THE FILE.\n");
            printf("2. TO SEARCH THE RECORD BY NAME.\n");
            printf("3. TO VIEW RECORDS.\n");
            printf("ENTER YOUR CHOICE:-> ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1:
                        fp=fopen("telephon.txt","ab");
                        while (1)
                        {
                          clrscr();
                          printf("ENTER THE TELEPHONE NUMBER:-> ");
                          scanf("%ld",&t.teleno);
                          fflush(stdin);
                          printf("\nENTER THE NAME               :-> ");
                          scanf("%s",t.name);
                          fflush(stdin);
                          printf("\nENTER THE ADDRESS            :-> ");
                          scanf("%s",t.address);
                          fwrite(&t,sizeof(t),1,fp);
                          fflush(stdin);
                          printf("\n\nWISH TO CONTINUE?(Y/N) \n\n");
                          scanf("%c",&c);
                          if(c=='n' || c=='N')
                                break;
                        }
                        fclose(fp);
                        break;
                case 2:
                        fp=fopen("telephon.txt","rb");
                        clrscr();
                        printf("ENTER THE NAME    :-> ");
                        scanf("%s",sname);
                        while(fread(&t,sizeof(t),1,fp))
                        {
                            if(strcmp(sname,t.name)==0)
                            {
    a:                          printf("\n\t TELEPHONE NUMBER: %ld\t NAME: %s\t ADDRESS: %s",t.teleno,t.name,t.address);
                                scanf("%c",&c);
                                if(c=='n' || c=='N')
                                getch();
                                break;
                            }
                        }
                        fclose(fp);
                        getch();
                        break;
    
                default:
                        goto a;
                        break;       
            }
        }
            getch();
    
    }
    Last edited by djdashwood; 01-24-2011 at 08:12 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could edit your post so that you have [code][/code] tags around your code.
    Didn't you read the intro threads at the top of the forum?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Location
    ??
    Posts
    3
    amf..sorry...
    i'll just edit it...amf..X_x

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Some notes:
    1. No such thing as fflush(stdin).
    2. Reading input using %s will not allow you to read names or addresses with spaces. You should either (a) use a different % thing, such as %[, or (b) use a different input method, such as fgets. If you decide for (a) you need to make sure to limit how much you read in so as to not overflow your struct; if you decide for (b) make sure you deal with \n characters appropriately.
    3. When looking for an individual character, generally you want to use " %c" as your format string, so as to prevent extraneous \n characters messing with you.
    4. Your default case shouldn't have a goto in it, and that means you can get rid of the label a as well.
    5. I'm not sure what you intended by #defining clrscr, but it doesn't do anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So where is case 3 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Location
    ??
    Posts
    3
    Quote Originally Posted by tabstop View Post
    Some notes:
    1. No such thing as fflush(stdin).
    2. Reading input using %s will not allow you to read names or addresses with spaces. You should either (a) use a different % thing, such as %[, or (b) use a different input method, such as fgets. If you decide for (a) you need to make sure to limit how much you read in so as to not overflow your struct; if you decide for (b) make sure you deal with \n characters appropriately.
    3. When looking for an individual character, generally you want to use " %c" as your format string, so as to prevent extraneous \n characters messing with you.
    4. Your default case shouldn't have a goto in it, and that means you can get rid of the label a as well.
    5. I'm not sure what you intended by #defining clrscr, but it doesn't do anything.
    i'm using dev c++ from bloodshed
    and it gives me error whenever i compile
    but when i wrote #define clrscr it accepted my code..O.o
    amf..X_x

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by chriXyren View Post
    i'm using dev c++ from bloodshed
    and it gives me error whenever i compile
    but when i wrote #define clrscr it accepted my code..O.o
    amf..X_x
    That's because clrscr doesn't exist. I don't know why you decided to write it, but it would probably be best all round if you just took it out.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Obviously it's there because he copied the code from here.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What a depressingly obsolete web site.
    Nice find though - it looks like a lot of stuff is ctrl-c/ctrl-v from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Or... as I once heard it called ..." scoop and poop."

  11. #11
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    231
    I would personally write this by myself than copy it because I would be spending more time then it took to make it figuring out why its not working.

    Or... as I once heard it called ..." scoop and poop."
    LOL @ that 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM