hey....
im supposed to make a school project using files and graphics..my topic is telephone directory.if i have to delete a particular entry,should i keep making a new file or is there any alternative

with regards

Recommended Answers

All 17 Replies

If you have to delete an entry, the simplest method conceptually is to rewrite the file in its entirety without the entry in question. For huge files that can be a performance issue, but for a school project you don't need to concern yourself with huge files.

the point is the when the examiner evaluates and tries to delete more than one file .,the program must work right?
Also,after deleting an already existing entry and trying to add a new one,how will i call the new file?

The file's name won't change, you're just overwriting it with updated information. At least, I'm assuming that's how your design works: you have a single database file containing records.

so.ill use the file in ios::out mode??

No, ios::out mode will truncate the file to 0 bytes. You want to use ios::in | ios::out for update mode. An alternative is this:

  1. Open the original file in read mode
  2. Open a temporary file in write mode
  3. Copy the original file to the temporary file

    • Don't copy deleted records
    • Use updated version of records
    • Insert new records as they come
  4. Delete the original file

  5. Rename the temporary file to match the original file

I think that is easier to get right than updating a single file.

i guess it is easier...i want to know one more thing.....how do i write text in a box(basically to make it look like a button) using graphics..also i'd like to know where we can use the keyword "delete"

It depends on the graphics library you're using.

can you elaborate

Not until you elaborate. How are you doing "graphics"?

basically.we were only informed to do a project using files and graphics...ive never been taught graphics.so my knowledge in this field is zero..all suggestions and advices are welcome..

when the code is compiled , no error is being shown but there is an error in search , modify and delete funcs.......in search func, the program gets stuk up after entering the name to be searched, in modify func it keeps on showing "invalid entry" even if the contact is searched and then only called for modification and delete func gets stuck up after being called

Can u pls tell us the errors the errors to b clarified

when the code is compiled

What code?

Can u pls tell us the errors the errors to b clarified

WHAT ERRORS?! Do you expect us to read your mind?

i tried posting the code but i am not able to do that .it says my code if formatted incorrectly.......

i tried posting the code but i am not able to do that .it says my code if formatted incorrectly.......

That error means your code wasn't in a code block. Paste everything in, select it all and hit the tab key. Alternatively, just use the Code button on our editor. It will pop up a code editor box that you can paste your code directly into.

/*
   TELEPHONE DIRECTORY
*/
#include<iomanip.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
#include<graphics.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>

class entry
{
  public:
  char add[200];
  long phone;
  char city[50];
  char state[50];
 char name[80];

  void accept();
  void show();
};

void entry::accept()
{
 clrscr();

 cout<<"\n\nEnter the new entry's details:";
 cout<<"\n\n Name: ";
 gets(name);
 cout<<"\n Address: ";
 gets(add);
 cout<<"\n Phone: ";
 cin>>phone;
 cout<<"\n City: ";
 gets(city);
 cout<<"\n State: ";
 gets(state);

}

void entry::show()
{
 clrscr();
cout<<"\n Name: "<<name;
 cout<<"\n Address: "<<add;
 cout<<"\n Phone: "<<phone;
 cout<<"\n City: "<<city;
 cout<<"\n State: "<<state;
 cout<<"\n Press enter to continue:";
getch();


}

void newentry()
{
 clrscr();
fstream f1;
  f1.open("entry.dat",ios::out|ios::binary);
  entry e1;
  char a;
do
{

 e1.accept();
 f1.write((char*)&e1,sizeof(e1));
 cout<<"\nWould you like to add a new entry??(Y/N)";
 cin>>a;
}while(a=='Y'||a=='y');

f1.close();



}

void modify()
{
  clrscr();

fstream f1;
  char nadd[200];
  long nphone;
  char ncity[50];
  char nstate[50];
  f1.open("entry.dat",ios::in|ios::out|ios::binary);
  entry e1,e2;
  char NAME[80],detail[80];
  int value=0;

   cout<<"\n Enter the entry to be modified:";

  gets(NAME);
  cout<<"\n Enter the new details (will be updated only if the entry is available): ";
 cout<<"\n New address:";
 gets(nadd);
 cout<<"\n New phone no.";
 cin>>nphone;
 cout<<"\n New city:";
 gets(ncity);
 cout<<"\n New state:";
 gets(nstate);
  while(!f1.eof())
{
 int pos = f1.tellg();
 f1.read((char*)&e1,sizeof(e1));
 if (strcmpi(e1.name,NAME)==0)
{
 strcpy(e1.add,nadd);
 e1.phone=nphone;
 strcpy(e1.city,ncity);
 strcpy(e1.state,nstate);
 f1.seekp(pos);
 f1.write((char*)&e1,sizeof(e1));
 value=2;
}
}
  f1.close();
if (value==2)
cout<<"Entry modified!";
else cout<<"Entry was not modified!";

}


void edelete()
{
 clrscr();
fstream f,f1;
 entry e3;
 int value=0;
 char dname[80];
 cout<<"Enter the name of the entry to be deleted:";
 gets(dname);
 f.open("entry.dat",ios::in|ios::binary);
 f1.open("entry1.dat",ios::out|ios::binary);
 while(!f.eof())
 {
  f.read((char*)&e3,sizeof(e3));
  if((e3.name,dname)!=0)
  {f1.write((char*)&e3,sizeof(e3));
  value=1;
  }
}
if(value==1)
cout<<"Entry deleted!";
else cout<<"Entry was not deleted!";
f.close();
f1.close();
remove("entry.dat");
rename("entry1.dat","entry.dat");
}

void search()
{
 clrscr();

fstream f;
entry e3;
char dname[80];
cout<<"Enter the name of the person you wish to search for: ";
gets(dname);
f.open("entry.dat",ios::in|ios::binary);

while(!f.eof())
{
f.read((char*)&e3,sizeof(e3));
if(strcmpi(dname,e3.name)==0)
e3.show();
}
f.close();

}

void exit()
{
 clrscr();

 cout<<"\n\n\n\n\t\t\t\tTHANK YOU FOR USING OUR SERVICE";
 cout<<"\n\n\t\t\t\t PLEASE VISIT AGAIN";
 cout<<endl<<endl<<"\t\t\tPress enter to continue.";

getch();


 exit(0);
}



void start()
{
 clrscr();



cout<<"\n\n\n\t\t\t WELCOME TO TELEPHONE DIRECTORY";

cout<< "\n\n\n\n\n\n\n\t\t\t Press enter to continue.";
getch();
clrscr();



}

void main()
{
 start();
 clrscr();
 entry e;
 int choice,i;

 cout<<"\n\t\t\t TELEPHONE DIRECTORY";
 start:

 cout<<endl<<endl<<"\n\n\n\t\t\t*** MENU *** ";
 cout<<"\n1. NEW ";
 cout<<"\n2. SEARCH";
 cout<<"\n3. MODIFY";
 cout<<"\n4. DELETE";
 cout<<"\n0. EXIT";
 cout<<"\n\n\n Enter your choice :";
 cin>>choice;

 if (choice==1)
 newentry();
 else if (choice==2)
 search();
 else if (choice==3)
 modify();
 else if (choice==4)
 edelete ();
 else if (choice==0)
 exit();
 else
 {
  cout<<"\n\nInvalid entry";
  for(int i=0; i<20; i++);
  goto start;
 }
 goto start;
}





even after deleting a particular entry, if i search for it,it shows me that entry's details....

You have a lot of problems, but the particular problem in edelete() that you're asking about is this:

if((e3.name,dname)!=0)

The code is legal, but you clearly meant to write this:

if(strcmpi(e3.name,dname)!=0)

Anyway, out of the goodness of my heart, I'll fix your code and make it portable so that you can learn what good code (well, semi-good) looks like and hopefully emulate it in the future. I'll keep the code simple and use your basic design except for one modification: instead of a binary file I've serialized to a pipe delimited format in a text file. The more experience I get, the less inclined I am to use binary files, they're often more trouble than they're worth.

Here's the code:

#include <cctype>
#include <fstream>
#include <ios>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>

struct Entry {
public:
    std::string name;
    std::string address;
    std::string city;
    std::string state;
    std::string phone;

    std::string serialize() const;
    void deserialize(const std::string& s);

    friend std::istream& operator>>(std::istream& in, Entry& obj);
    friend std::ostream& operator<<(std::ostream& out, const Entry& obj);
};

void AddEntry();
void FindEntry();
void ModifyEntry(bool delete_entry = false);

int main()
{
    bool done = false;

    std::cout << "\n\t\t\tTELEPHONE DIRECTORY";

    while (!done) {
        int choice = 0;

        std::cout << "\n\n\n\t\t\t*** MENU ***\n";
        std::cout << "1. NEW\n";
        std::cout << "2. SEARCH\n";
        std::cout << "3. MODIFY\n";
        std::cout << "4. DELETE\n";
        std::cout << "0. EXIT\n\n\n";
        std::cout << "Enter your choice: " << std::flush;

        if (std::cin.peek() == '\n') {
            // Just ignore that can of worms, but allow it without a diagnostic
            std::cin.ignore();
            continue;
        }

        bool is_valid = std::cin >> choice;

        // Always clear out extraneous data on the line
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        if (!is_valid)
            std::cout << "Invalid input\n";
        else {
            switch (choice) {
            case 0: done = true; break; 
            case 1: AddEntry(); break;
            case 2: FindEntry(); break;
            case 3: ModifyEntry(); break;
            case 4: ModifyEntry(true); break;
            default:
                std::cout << "Unrecognized selection\n";
                break;
            }
        }
    }
}

std::string Entry::serialize() const
{
    std::ostringstream oss;

    oss << name << '|' << address << '|' << city << '|' << state << '|' << phone;

    return oss.str();
}

void Entry::deserialize(const std::string& s)
{
    std::istringstream iss(s);

    // Error handling on the format will make the code more robust
    std::getline(iss, name, '|');
    std::getline(iss, address, '|');
    std::getline(iss, city, '|');
    std::getline(iss, state, '|');
    std::getline(iss, phone, '|');
}

std::istream& operator>>(std::istream& in, Entry& obj)
{
    // Error handling on the getline calls will make the code more robust
    std::cout << "Name: ";
    std::getline(in, obj.name);
    std::cout << "Address: ";
    std::getline(in, obj.address);
    std::cout << "City: ";
    std::getline(in, obj.city);
    std::cout << "State: ";
    std::getline(in, obj.state);
    std::cout << "Phone: ";
    std::getline(in, obj.phone);

    return in;
}

std::ostream& operator<<(std::ostream& out, const Entry& obj)
{
    out << "Name: " << obj.name << '\n';
    out << "Address: " << obj.address << '\n';
    out << "City: " << obj.city << '\n';
    out << "State: " << obj.state << '\n';
    out << "Phone: " << obj.phone << '\n';

    return out;
}

void AddEntry()
{
    std::ofstream out("entry.dat", std::ios::app);
    char again = 'N';
    Entry record;

    do {
        if (std::cin >> record)
            out << record.serialize() << '\n';

        std::cout << "Would you like to add a new entry? (Y/N): ";

        if (std::cin.get(again) && again != '\n')
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    } while (toupper(again) == 'Y');
}

void FindEntry()
{
    std::ifstream in("entry.dat");
    std::string name, line;
    bool match = false;
    Entry record;

    std::cout << "Enter the name of the person you wish to search for: ";

    if (getline(std::cin, name)) {
        while (std::getline(in, line)) {
            record.deserialize(line);

            if (record.name == name) {
                match = true;

                std::cout << "Match found!\n" << record << '\n';
                std::cout << "Press enter to continue..." << std::flush;

                char ch;

                if (std::cin.get(ch) && ch != '\n')
                    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
        }

        if (!match)
            std::cout << "No matching records for name '" << name << "'\n";
    }
}

void ModifyEntry(bool delete_entry)
{
    std::ifstream in("entry.dat");
    std::ofstream out("entry_temp.dat");
    std::string name, line;
    bool match = false;
    Entry record;

    std::cout << "Enter the name of the person you wish to " << (delete_entry ? "delete" : "modify") << ": ";

    if (getline(std::cin, name)) {
        while (std::getline(in, line)) {
            record.deserialize(line);

            if (record.name != name)
                out << line << '\n';
            else {
                match = true;

                std::cout << "Match found!";

                if (delete_entry)
                    std::cout << "Deleting record...\n";
                else {
                    std::cout << "Enter the modifications\n";
                    std::cin >> record;
                    out << record.serialize() << '\n';
                }
            }
        }

        // Needed to commit changes and unlock the files for removal/renaming
        in.close();
        out.close();

        // Replace entry.dat with entry_temp.dat
        remove("entry.dat");
        rename("entry_temp.dat", "entry.dat");

        if (!match)
            std::cout << "No matching records for name '" << name << "'\n";
    }
}

Feel free to ask questions.

i meant to use strcmpi but i guess i left that out...nyways....for the program, thanx.....
ill go through it and come back if i dont understand

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.