It's a phone book program.
Takes number, name, and address and saves them to addressbook.txt. It has add, search and delete functions. I got all but delete.

Here's the search function(works):

void search(){
	char num[50];
	char search[50];
	ifstream input;
	input.open("addressbook.txt");
	cout << "Enter phone number: ";
	cin >> num;
	while(!input.eof()){
		input.getline(search, 50);
		if(strcmp(num, search)==0){
			for(int i=0; i<3; i++){
				cout << search << endl;
				input.getline(search, 50);
			}
		}
	}
}

And I need to write a function that will delete an entry (3 lines).

void dlete(){
	char num[50];
	char search[50];
	fstream input;
	input.open("addressbook.txt");
	cout << "Enter phone number: ";
	cin >> num;
	while(!input.eof()){
		input.getline(search, 50);
		if(strcmp(num, search)== 0){
			input.getline(search, 50);
                                                //???????
		}
	}
}

For some reason I can't figure out how to get the line, clear it, and repeat.

Recommended Answers

All 10 Replies

You don't change a file in-place, you either read the file into memory and make your changes before truncating the file and rewriting it, or you use a temporary file so that only one record is in memory at a time.

Member Avatar for iamthwee

-Don't use eof either to control file i/o
-And when you post on forums use spaces instead of tabs to indent the code!
-You would also be better of using std::strings.

Why not use eof? The search function does exactly what I want it to do, but if you have a better way to do it then I'm always open to learning. And thanks Narue, that makes a lot more sense than what I was trying to do. I suppose I'll crack at it later when I get a chance. I'm just not very good with i/ofstream. Sorry about the indenting, first thread.

-Don't use eof either to control file i/o
-And when you post on forums use spaces instead of tabs to indent the code!
-You would also be better of using std::strings.

What can I use other than the eof line?
I'm supposed to use char arrays instead of strings.

See this info. feof() is the same as .eof()

I got this for the delete function:

void dlete(){
  char num[50];
  char search[50];
  //new_entry entry;
  ifstream old;
  old.open("addressbook.txt");
  ofstream current;
  current.open("temp.txt");
  cout << "Enter phone number: ";
  cin >> num;
  while(old.getline(search, 50)){
    if(!strcmp(num, search)==0){
      current << search << '\n';
    }
  }
}

That works, but it only gets rid of one line. I want to get rid of that line, and 2 more following it. Any suggestions?

I would guess that the 2 following lines do not equal search, since that is the only delete requirement. You need to either
1) know what the next two lines contain so you can compare them
2) just read off the next two lines when you get to a line to delete

void dlete(){
  char num[50];
  char search[50];
  new_entry entry;
  ifstream old;
  old.open("addressbook.txt");
  ofstream current;
  current.open("temp.txt");
  cout << "Enter phone number: ";
  cin >> num;
  while(old.getline(search, 50)){
    if(strcmp(num, search)==0){
      for(int i=0; i<2; i++){
        memset(search, '\n', sizeof(search));
        old.getline(search, 50);
        }
      }
      else if(!strcmp(num, search)==0){
      current << search << '\n';
    }
  }
}

Got it too work. Thanks a lot to everyone that helped.

In this code of yours (formatted for readability):

if (strcmp(num, search) == 0)
    {
        ...
    }
    [B]// is the following IF necessary?  Doesn't the ELSE automatically
    // assume this condition because of the above IF?[/B]
     else if (!strcmp(num, search) == 0)
    {
        ...
    }

You can leave off the second IF because it's true by default.

Oh silly me. Thanks. I got around using the eof's too. Thanks again; actually useful mods on a forum for a change.

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.