Thread: PHONEBOOK in c

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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