1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[help] hoe i can remove name from telephone book program

Discussion in 'C++' started by dor8, Jan 2, 2011.

  1. dor8

    dor8 New Member

    Joined:
    Jan 2, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I make telephone book program in 'c' language ..
    the program need:
    to add person with first an last name and phone num and write him to file(txt),
    to serch name from file,
    and Delete all details of someone from the list by writing the firt name and last name..
    i dont know how to do the last function (delete), i hope thet someone can help me..
    my program not so good but the function add and find wores..
    this is the code:
    ...
    Code:
    [LEFT]struct person
    {
    char firstname[10];
    char lastName [15];
    char phonenumber[15];
    char email[20];
    };[/LEFT]
     
     
    [LEFT]person fillStruct (person &pr);
    void copyToText (person prso[],int&in);
    void find();
    void erase (person prso[],int&in);[/LEFT]
     
    [LEFT]int in=0;
    void main ()
    {
    person prso[30];
    char menu;
    cout<<"\t\t Phone Book\n";
    cout<<" Menu: \n\n";[/LEFT]
     
     
    [LEFT]for (;1;)
    {
    Add Phone\n";
    Delete phone book\n";
    find Phone by name\n";
    Quit\n";
    cin >> menu;[/LEFT]
     
     
    [LEFT]switch (menu)
    {
    case 'A': copyToText(prso,in); 
    break;
    case 'D' : erase (); 
    break;
    case 'F': find();
    break;
    case 'Q' :
    exit(2);
    break;
    default : 
    cout <<"wrong input.. enter new value\n";
    continue;
    } 
    }
    }
    person fillStruct (person &pr)
    {
    cout<<"write firts name: ";
    cin>>pr.firstname;
    cout<<"\nwrite last name: ";
    cin>>pr.lastName;
    cout<<"\nphone number: ";
    cin>>pr.phonenumber;
    return pr;
    }[/LEFT]
      
    [LEFT]void copyToText(person *prso,int&in)
    {
    person pr;
    fillStruct(pr);
    strcpy(prso[in].firstname,pr.firstname);
    strcpy(prso[in].lastName ,pr.lastName);
    strcpy(prso[in].phonenumber,pr.phonenumber);[/LEFT]
     
     
    [LEFT]FILE* ofp;
    ofp = fopen("c:\\in.txt", "a");
    if (ofp == NULL)
    {
    printf("The file could not be opened\n");
    }
    fprintf (ofp, "%s %s %s\n", pr.firstname, pr.lastName, pr.phonenumber, in++;
    fclose(ofp);
    }
    void find()
    {
    char ch[50],name[30],aaa[30];
    int i,j,fss=0;
    FILE* fp;
    fp = fopen("c:\\in.txt", "r");
    cout<<"Enter Name to find\n";
    cin>>name;
    i=0;
    while(!feof(fp))
    {[/LEFT]
     
    [LEFT]fgets(ch,50,fp)
    i=0;while (ch[i] != '\0')
    {
    j=0;
    strcpy(aaa,"");
    while (ch[i++] != ' ')
    {
    aaa[j] = ch[i-1];
    j++;
    aaa[j]='\0';
    }
    if (strcmp(Deme,name) == 0)
    {
    fss=1;
    break;
    }
    fscanf(fp,"%[^\n]\n",chi=0;
    }[/LEFT]
     
    if(fss==1)
    {
    cout<<ch<<"\n";
    fscanf(fp,"%[^\n]\n",ch);
    continue;
    }
    else (fss==0);
    {
    [LEFT]fclose(fp);
    }[/LEFT]
     
    Last edited by a moderator: Jan 2, 2011

Share This Page