Thread: PHONEBOOK in c

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    PHONEBOOK in c

    hi! i'm new here and i'm a newbie at this programming and stuff..Our project is to create a phone book in pure c language and that it can add, view, search, delete, and edit entries. My problem is that I don't know how to search the name then view the details instead in our codes, when you search, you need to input its contact number and that just kills the use of phone book.<br>

    We have the codes but it's not in pure c language, it has c++ codes.We are doing this by pair and most of the codes here are from my classmate I only did a tadbit of editing and debugging. Some also are from the internet. Help please?

    Code:
    #include<iostream>
    #include<string.h>
    #include<iomanip>
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
     
    using namespace std;
     
    void menu();
    void addPatient();
    void viewRecords();
    void viewAllRecords();
    void viewOneRecord();
    void display();
    void deleteMechanism();
    void editRecord();
    void menuDisplay();
    void viewOneMechanism();
    void deleteRecord();
    void question();
     
    struct studinfo
    {
    int idno;
    char name[25];
    char address[25];
    };
     
     
    FILE *Patfile;
    FILE *tempPatfile;
     
    int id;
    bool found;
     
    main()
    {
    int a;
     
    printf("\n\n\n\n\n\n\n\t\t");
    for(a=1; a<=49; a++){
    printf("*");
    }
     
    printf("\n\n\n\t\t\t\t WELCOME\n\n\n\t\t");
    for(a=1; a<=49; a++){
    printf("*");
    }
     
    getch();
    system("cls");
    menu();
    }
     
    void menu()
    {
    int choice=0;
     
    while(choice!='E')
    {
    system("cls");
    menuDisplay();
    printf("\nType the letter of your choice: ");
    scanf("%c", &choice);
    choice = toupper(choice);
    switch(choice)
    {
    case 'A':{addPatient();break;}
    case 'B':{viewRecords();break;}
    case 'C':{deleteRecord();break;}
    case 'D':{editRecord();break;}
    }
    }
    }
     
    void menuDisplay()
    {
    printf("\n\t\t\t P O E B E  and  J A I' S");
    printf("\n\n\n\t\t\t\t Phonebook");
    printf("\n\n\n************************************");
    printf("********************************************");
    printf("\n\nC H O I C E S");
    printf("\n- - - - - - - - - - - - - - -");
    printf("\n[A] ADD Contact");
    printf("\n[B] VIEW Contacts");
    printf("\n[C] DELETE ONE Contact");
    printf("\n[D] EDIT Contacts ");
    printf("\n[E] EXIT");
    printf("\n- - - - - - - - - - - - - - -");
    }
     
    void addPatient()
    {
    int cntct,num;
    found=false;
    char name[25],address[25];
    studinfo students;
    printf("\n\nContact number:");
    scanf("%d", &num);
    Patfile=fopen("Profile.txt","a+");
    rewind(Patfile);
    while(!feof(Patfile)&& found==false)
    {
    fscanf(Patfile,"%d%s%s",&cntct,&name,&address);
    if(cntct==num)
    {
    printf("Number is Existing!..");
    found=true;
    }
    }
    if(found==false)
    {
    printf("\nName:");
    scanf("%*c%s", &name);
    printf("\nAddress:");
    scanf("%s", &address);
    fprintf (Patfile, "\n%-5d %-20s %-20s",num,name,address);
    printf("\nThe new record was successfully saved!");
    }
    getch();
    fclose(Patfile);
    }
     
    void viewRecords()
    {
    system("cls");
    char c;
    printf("\nView One Contacts or View All Contacts?");
    printf("\n[Press [A] to view One Record]");
    printf("\n[Press [B] to view All Records]");
    printf("\nEnter Choice: ");
    scanf("%c", &c);
    c=toupper(c);
    switch(c)
    {
    case 'A':
    viewOneRecord();
    break;
    case 'B':
    viewAllRecords();
    break;
    default:
    system("cls");
    return viewRecords();
    break;
    }
    question();
    }
    void question()
    {
    char e;
    printf("\n\nDo you want another Transaction?[Y/N]:");
    scanf("%*c%c", &e);
    e=toupper(e);
    switch(e)
    {
    case 'Y': 
    system("cls");
    return viewRecords();
    break;
    case 'N': 
    system("cls");
    return menu();
    break;
    default:
    system("cls");
    return question();
    break;
    }
    }
     
    void viewAllRecords()
    {
     
    int idno;
    char name[25],address[25];
    Patfile=fopen("Profile.txt","r");
    rewind(Patfile);
    if(!feof(Patfile))
    {
    system("cls");
    cout<<" \nLIST OF CONTACTS";
    display();
    while(!feof(Patfile))
    {
    if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
    cout<<setw(15)<<idno<<setw(25)<<name<<setw(25)<<address<<"\n";
     
    }
    }
    else{
    printf("No record found in the file!");}
    fclose(Patfile);
    getch();
    }
     
    void viewOneRecord()
    {
    viewOneMechanism();
    getch();
    }
     
    void deleteRecord()
    {
    char cont;
    viewOneMechanism();
    if (found==true)
    {
    cout<<"Are you sure you want to delete this record(y/n)?:";
    cin>>cont;
    if(cont=='y'||cont=='Y')
    {
    deleteMechanism();
    cout<<"\n\nThe record was successfully deleted!";
    }
    else
    {
    cout<<"\nThe record was not deleted!";
    }
    getch();
    }
    }
     
    void editRecord()
    {
    char cont;
    char name[25],address[25];
    viewOneMechanism();
    if(found==true)
    {
    printf("\nAre you sure you want to update this record(y/n)?:");
    scanf("%c", &cont);
    cont=toupper(cont);
    if(cont=='Y')
    {
    deleteMechanism();
    Patfile=fopen("Profile.txt","a+");
    cout<<"\nType Name:";
    cin>>name;
    cout<<"\nType Address:";
    cin>>address;
    fprintf (Patfile, "\n%-5d %-20s %-20s",id,name,address);
    fclose(Patfile);
    cout<<"\nThe record was successfully updated!";
    }
    else
    {
    cout<<"\nThe record was not updated!";
    }
    getch();
    }
    }
     
    void display()
    {
    cout<<"\n"<<setw(15)<<left<<"Contact no."
    <<setw(25)<<left<<"Name"
    <<setw(25)<<left<<"Address"<<"\n";
    }
     
    void deleteMechanism()
    {
    int idno;
    char name[25],address[25];
    Patfile=fopen("Profile.txt","r");
    tempPatfile=fopen("tempProfile.txt","w+");
    rewind(Patfile);
    while(!feof(Patfile))
    {
    if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
    {
    if(idno!=id)
    {
    fprintf (tempPatfile, "\n%-5d %-20s %-20s",idno,name,address);
    }
    }
    }
    fclose(Patfile);
    fclose(tempPatfile);
     
    Patfile=fopen("Profile.txt","w+");
    tempPatfile=fopen("tempProfile.txt","r");
    rewind(tempPatfile);
    while(!feof(tempPatfile))
    {
    if(fscanf(tempPatfile,"%d%s%s",&idno,&name,&address)>0)
    {
    if(idno!=id)
    {
    fprintf (Patfile, "\n%-5d %-20s %-20s",idno,name,address);
    }
    }
    }
    fclose(Patfile);
    fclose(tempPatfile);
    }
     
    void viewOneMechanism()
    {
    int idno;
    char name[25],address[25];
    found=false;
    printf("\n\nEnter CONTACT to be viewed:");
    cin>>id;
    Patfile=fopen("Profile.txt","r");
    rewind(Patfile);
    while(!feof(Patfile)&&found==false)
    {
    if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
    {
    if(idno==id)
    {
    display();
    cout<<setw(15)<<idno<<setw(25)<<name<<setw(25)<<address<<"\n";
    found=true;
    }
    }
    }
    if(found==false)
    {
    printf("\n Record is not existing!");
    }
    fclose(Patfile);
    }
    Last edited by chie22; 10-17-2011 at 05:33 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<iostream> /* C++ */
    #include<string.h> /* C */
    #include<iomanip> /* C++ */
    #include<stdio.h> /* C */
    #include<conio.h> /* C, compiler specific, only for gotoxy type stuff */
    #include<ctype.h> /* C */
    If you want "pure C++" meaning "written in C", then lose everything with the comment /* C++ */. If you want "pure C" meaning standard C, then get rid of everything without the comment /* C */.

    Then fix your indentation.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Our project is to create a phone book in pure c language
    Code:
    void addPatient();
    Maybe you should consider writing your own code, rather than trying to cobble together your project from various disparate Internet sources and dumping it here to try to get us to make it work so you can get a good grade you don't deserve.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(!feof(Patfile)&& found==false)
    {
    fscanf(Patfile,"%d%s%s",&cntct,&name,&address);
    
    ...
    
    if(!feof(Patfile))
    {
    system("cls");
    cout<<" \nLIST OF CONTACTS";
    display();
    while(!feof(Patfile))
    {
    if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
    cout<<setw(15)<<idno<<setw(25)<<name<<setw(25)<<address<<"\n";
    
    ...
    
    while(!feof(Patfile))
    {
    if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
    
    ...
    
    while(!feof(tempPatfile))
    {
    if(fscanf(tempPatfile,"%d%s%s",&idno,&name,&address)>0)
    
    ...
    
    while(!feof(Patfile)&&found==false)
    {
    if(fscanf(Patfile,"%d%s%s",&idno,&name,&address)>0)
    Why it's bad to use feof() to control a loop
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    r2r has pretty much nailed this IMO. No doubt the OP is already looking for another forum to dump this crap on, rather than buckling down to some actual work.
    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
    Sep 2006
    Posts
    8,868
    A few suggestions for the start of the program:

    Code:
    #include<string.h>
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    #include <time.h> 
     
    //function prototypes
    void menu(void);
    int addRecord();
    void viewAllRecords();
    void viewOneRecord();
    void display();
    void deleteRecord();
    void editRecord();
    void sort(); //sort through an index maybe?
    
    
    struct record {
       int idno;
       char name[25];
       char address[25];
       char phone[8];
       int code;    //area code
    }rec1;
    
    
    
    int main(void) { 
       FILE *Patfile;
       FILE *tempPatfile;
     
       int a, id;
       bool found;
        
       system("cls");
       menu();
    
       return 0;
    } //end of main
     
    void menu(void) {
       int choice=0, i, y;
     
       printf("\n\n\n\n\n\n\n\t\t");
       for(i=0; i<50; i++) {
          printf("*");
    
       printf("\n\n\n\t\t\t\t WELCOME\n\n\n\t\t");
       for(a=0; i<50; i++){
          printf("*");
    
       while(choice!='E') {
          //   system("cls");
          menuDisplay();
          printf("\nType the letter of your choice: ");
          scanf("%c", &choice);
          choice = toupper(choice);
       
          switch(choice)    {
             case 'A':{addPatient();break;}
             case 'B':{viewRecords();break;}
             case 'C':{deleteRecord();break;}
             case 'D':{editRecord();break;}
             case 'E': { gotoxy(1,20);
                for(y=20;i<22;y++)  {  //clear an area on screen
                   gotoxy(1,y);
                   for(i=0;i<2;i++)
                      printf("                                        ");
                }
                gotoxy(36,21);
                printf("*Goodbye*");
                Sleep(500);  //1/2 of a second, not 500 seconds ;)
                break;
             }    
             default: { printf("Please re-enter that choice\n"); 
                sleep(500);
             }
          }
       }
    }
    void menuDisplay(void) {
       printf("\n\t\t\t P O E B E  and  J A I' S");
       printf("\n\n\n\t\t\t\t Phonebook");
       printf("\n\n\n************************************");
       printf("********************************************");
       printf("\n\nC H O I C E S");
       printf("\n- - - - - - - - - - - - - - -");
       printf("\n[A] ADD Contact");
       printf("\n[B] VIEW Contacts");
       printf("\n[C] DELETE ONE Contact");
       printf("\n[D] EDIT A Contact ");
       printf("\n[E] EXIT");
       printf("\n- - - - - - - - - - - - - - -");
    }
    You've put together a lot of the code that you could use, but now you need to adapt it to ONE program, and take care of all the details. It's time to review your books or notes, and work with C, much more directly. Cobbling together code, can only go so far - and it won't be far enough -- never is.

    Good luck with your assignment.
    Last edited by Adak; 10-17-2011 at 11:46 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    r2r has pretty much nailed this IMO. No doubt the OP is already looking for another forum to dump this crap on, rather than buckling down to some actual work.
    Maybe this is an example of extreme-agile programming!


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple phonebook?!..X_x
    By djdashwood in forum C Programming
    Replies: 10
    Last Post: 01-24-2011, 04:02 PM
  2. phonebook help
    By noob2c in forum C Programming
    Replies: 5
    Last Post: 04-19-2003, 01:51 PM
  3. phonebook error
    By cguy in forum C Programming
    Replies: 2
    Last Post: 01-30-2003, 01:08 AM
  4. phonebook
    By jk81 in forum C Programming
    Replies: 6
    Last Post: 09-25-2002, 04:41 AM