I'm having problem with this code.
Here's what I'm trying to do.
I want the user to enter the firstname, last name of the person whom he wants to delete from the phonebook.
then I try to compare the data entered by user with the names already stored on phonebook.
if the name doesnot match, it stores that name on the phonebook again.
If it matches then it won't store it on phonebook again.
So this way the contact which user wanted to delete will not be stored again on the file and ultimately which means it got deleted.

Here is my code but I'm not able to do what i wanted to.
Please if you can help me figure out where the problem is?

void delete(string contacts[], string fName, string lName, ifstream& aInFile, ofstream& aOutFile) {
cout << "DELETE A CONTACT\n";
cout << "Type the contacts first name\n";
cin >> fName;
cout << "Type the contacts last name\n";
cin >> lName;
int j = 0;
while(!aInFile.eof()){
        aInFile.open(OUTPUT_FILE);
        checkFileOpen(aInFile);
        aInFile >> contacts[j];
        string x = contacts[j];
        aInFile >> contacts[j];
        string y = contacts[j];
        aInFile >> contacts[j];
        string z = contacts[j];
        aInFile.close();
        if (lName != x || fName != y){
                cout << "couldn't find contact with Name" << fName << lName << endl;

                aOutFile.open(OUTPUT_FILE, ios::app);
                checkFileOpen(aOutFile);
                aOutFile << x <<"\t" << y <<"\t" <<  z <<endl;
                aOutFile.close();
        }else if ( fName == y && lName == x){
                cout << "deleted"<< endl;
        }
        j++;

}
}

Thanks.

Your program after line 7 is a big mess. Just delete all that junk.

I don't know what all those parameters to the delete function are supposted to be, but you don't need any of them.

All you have to do is open the input file, read the first and last names, compare them with what the user entered,

enter first name
enter last name
open input file
set a bool flag to false which is set to true if the name is found
while not end of file
     read last name
     read first name
     if last name and first name match user input
          set flag to true
          exit the loop and do nothing
end of loop
if the flag is still false
   add the last and first names to the file

let me try this way:)

here's how it look like now. But it's not doing anything except just displaying the cout statements

void delete(string contacts[], string fName, string lName, ifstream& aInFile, ofstream& aOutFile) {
cout << "DELETE A CONTACT\n";
cout << "Type the contacts first name\n";
cin >> fName;
cout << "Type the contacts last name\n";
cin >> lName;
int j = 0;
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
bool flag = false;
while(!aInFile.eof()){
        aInFile >> contacts[j];
        string x = contacts[j];
        aInFile >> contacts[j];
        string y = contacts[j];
        aInFile >> contacts[j];
        string z = contacts[j];
        aInFile.close();
        if (lName == x && fName == y){
                cout << "name deleted\n";
                flag = true;
                break;
        }
if (flag = false){
        //cout << "deleted"<< endl;
        aOutFile.open(OUTPUT_FILE, ios::app);
        checkFileOpen(aOutFile);
        aOutFile << x <<"\t" << y <<"\t" <<  z <<endl;
        aOutFile.close();
        }
j++;
}       //      cout << "deleted"<< endl;
}

Make sure you don't have any spaces in the code tags.

[code=cplusplus] // code

[/code]
Not this

[code = cplusplus] // code

[/code]

void delete(string contacts[], string fName, string lName, ifstream& aInFile, ofstream& aOutFile) {
	cout << "DELETE A CONTACT\n";
	cout << "Type the contacts first name\n";
	cin >> fName;
	cout << "Type the contacts last name\n";
	cin >> lName;
	int j = 0;
	aInFile.open(OUTPUT_FILE);
	checkFileOpen(aInFile);
	bool flag = false;
	while(!aInFile.eof()){
		aInFile >> contacts[j];
		string x = contacts[j];
		aInFile >> contacts[j];
		string y = contacts[j];
		aInFile >> contacts[j];
		string z = contacts[j];
		aInFile.close();
		if (lName == x && fName == y){
			cout << "name deleted\n";
			flag = true;
			break;
		}
		if (flag = false){
			//cout << "deleted"<< endl;
			aOutFile.open(OUTPUT_FILE, ios::app);
			checkFileOpen(aOutFile);
			aOutFile << x <<"\t" << y <<"\t" << z <<endl;
			aOutFile.close();
		}
		j++;
	} // cout << "deleted"<< endl;
}

Line 24 - Do you want == instead of = ?
Lines 12 to 17 - Any reason you aren't reading directly into x, y, and z?

yes i did change = to ==.
and wanted to make 12 to 17 look like this so haven't changed it.
this is not creating any problem or is it?

void delete(string contacts[], string fName, string lName, ifstream& aInFile, ofstream& aOutFile) {
cout << "DELETE A CONTACT\n";
cout << "Type the contacts first name\n";
cin >> fName;
cout << "Type the contacts last name\n";
cin >> lName;
int j = 0;
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
bool flag = false;
while(!aInFile.eof()){
        //aInFile.open(OUTPUT_FILE);
        //checkFileOpen(aInFile);
        aInFile >> contacts[j];
        string x = contacts[j];
        aInFile >> contacts[j];
        string y = contacts[j];
        aInFile >> contacts[j];
        string z = contacts[j];
        aInFile.close();
        if (lName == x && fName == y){
                cout << "name deleted\n";
                flag = true;
                break;
        }
if (flag == false){
        //cout << "deleted"<< endl;
        aOutFile.open(OUTPUT_FILE, ios::app);
        checkFileOpen(aOutFile);
        aOutFile << x <<"\t" << y <<"\t" <<  z <<endl;
        aOutFile.close();
        }
j++;
}       
}

yes i did change = to ==.
and wanted to make 12 to 17 look like this so haven't changed it.
this is not creating any problem or is it?

void delete(string contacts[], string fName, string lName, ifstream& aInFile, ofstream& aOutFile) {
cout << "DELETE A CONTACT\n";
cout << "Type the contacts first name\n";
cin >> fName;
cout << "Type the contacts last name\n";
cin >> lName;
int j = 0;
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
bool flag = false;
while(!aInFile.eof()){
        //aInFile.open(OUTPUT_FILE);
        //checkFileOpen(aInFile);
        aInFile >> contacts[j];
        string x = contacts[j];
        aInFile >> contacts[j];
        string y = contacts[j];
        aInFile >> contacts[j];
        string z = contacts[j];
        aInFile.close();
        if (lName == x && fName == y){
                cout << "name deleted\n";
                flag = true;
                break;
        }
if (flag == false){
        //cout << "deleted"<< endl;
        aOutFile.open(OUTPUT_FILE, ios::app);
        checkFileOpen(aOutFile);
        aOutFile << x <<"\t" << y <<"\t" <<  z <<endl;
        aOutFile.close();
        }
j++;
}       
}

but it still displays all the contacts even after i run this delete function?

I remember having this conversation with you on the last thread, but refresh my memory and for the sake of those who have not seen your last thread, please answer the following question. Each string in the contacts array is supposed to have a first name, a last name, and a phone number, correct? Also, arrays are always passed by reference. If the contacts array needs to remain the same, you are changing it in this function and are likely losing some or a lot of information. Do you really need to pass the array to this function? If you are getting all the info from the file and simply deleting from the file, don't bother passing the array.

Just ask the user to input a first and last name, open up the file, read through it line by line, looking for the first and last name, flag accordingly, then either write or don't write every line to the file by the way that flag is set.

I don't know whether you need to pass the contacts array or not, but passing fName and lName, then immediately writing over it may not be harmful, but is certainly going to confuse everyone and make it harder to help. Like I said, unless you have a very clear idea of what you want in the contacts array AFTER this function is over, don't pass it. It looks to me like this array is going to contain nothing but phone numbers when you are done. If you DON'T want that to happen, regardless of whether you delete correctly in the file, either don't pass contacts to the function or use it differently in the function.

yes i think u r right.
I shouldn't pass the arrays in my function.
is it that i should not pass even contacts[] in my function alongwith lName, fName?
because i tried just removing fName & lName and it still didn't work

Here is my view of one way you can handle this. All you need is the filename of the file that contains the data. If you know how many records there are in the file, that could be good too. If the filename is global, you don't even need that. Let's say it is not global. Have the function take the filename and nothing else:

void delete (string filename)

Let's say you have a file called addressbook.txt, the contents of which are the following:

Fred   Flintstone 555-555-5555
Barney Rubble     666-666-6666
Hank   Hill       777-777-7777

I like vectors and I think they'd work here. Not sure if you have used them before. Here's some pseudocode:

void delete (string filename)
{
     string fname;
     string lName;
     // ask user for fName and lName

     ifstream ins;
     ins.open (filename.c_str ());
     vector <string> phonerecords;

     while (more records are in file)
     {
          string aRecord;
          // read first name, last name, phone num
          // from file, place in aRecord
          phonerecords.push_back (aRecord);
     }
     ins.close ();
     ofstream outs;
     outs.open (filename.c_str ());
     for (int i = 0; i < phonerecords.size (); i++)
     {
          bool deleteRecord;
          // flag deleteRecord as true if this record
          // should be deleted, false otherwise

          if (!deleteRecord)
               outs << phonerecords.at (i) << endl;
      }
      outs.close ();
}

I think that is what you are looking for. There are many ways to do it. I think the above is a good approach, but certainly not the only one that could work.

Thanks a lot. May be the algorithm u have used might help .Because I don't have to use vectors or cstrings to do this program.
I'm only using arrays and string here.
Let me try if i can make this work with strings and arrays.
One ques. with arrays & strings. i think i have to pass both input file and output file even though i'm using only one file for both the purposes.

I'm trying to write with arrays and string, in the way you have written using vecotrs.
But I'm having difficulties with loops now.

void deleteFromBlackBook( ifstream& aInFile, ofstream& aOutFile) {
string fName;
string lName;
string name;
cout << "DELETE A CONTACT\n";
cout << "Type the contacts first name\n";
cin >> fName;
cout << "Type the contacts last name\n";
cin >> lName;
//int j = 0;
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
bool flag = false;
while(!aInFile.eof()){
        aInFile >> contacts1;
        string x = contacts1;
        aInFile >> contacts2;
        string y = contacts2;
        aInFile >> contacts3;
        string z = contacts3;
        name = x + "/t" + y + "/t" + z;
}
aInFile.close();
aOutFile.open(OUTPUT_FILE, ios::app);
checkFileOpen(aOutFile);
for(int j = 0; j < NUMBER; j++){
        if (fName == y && lName == x){
                bool flag = true;
                continue;
        }
        if (flag == false){
                aOutFile <<name <<endl;
       }
}
aOutFile.close();
}

Thanks a lot. May be the algorithm u have used might help .Because I don't have to use vectors or cstrings to do this program.
I'm only using arrays and string here.
Let me try if i can make this work with strings and arrays.
One ques. with arrays & strings. i think i have to pass both input file and output file even though i'm using only one file for both the purposes.

Arrays can work too. In my opinion, arrays are harder with my algorithm because you don't know ahead of time how many records you have in the file. If you DO know ahead of time, pass that value by reference and it will help you allocate memory for the array. I suggested this algorithm because it seemed to me the best fit for what looks like your current programming level. I don't know if you know how to reallocate dynamic array sizes as the array grows. A vector does this for you.

The issue of whether to use an array or a vector or a linked list or whatever is irrelevant to the issue of whether to pass ifstreams or ofstreams and filenames. I think you'll make life easier if you don't try to pass the ifstream and ofstream but rather just declare it in the function. Similarly, I wouldn't try to use the fstream flags like ios::app yet. They certainly have their place and are good to learn, but I think you are courting disaster if you try to read a line from the file, then write that line to the file, then read from the file, then write to the file, etc. It's a lot to keep track of and one more thing that can go wrong. I really think your best bet is to read the entire file into either a vector or an array, close that file, reopen it as an output file, and output what you want into it. And again, I think, unless you have a firm reason to do so, I think it's a mistake to pass the ifstream and ofstream to the function.

Again, whether you use a vector or an array should have no impact on your decision on whether to pass the ifstream or ofstream.

I'm trying to write with arrays and string, in the way you have written using vecotrs.
But I'm having difficulties with loops now.

void deleteFromBlackBook( ifstream& aInFile, ofstream& aOutFile) {
string fName;
string lName;
string name;
cout << "DELETE A CONTACT\n";
cout << "Type the contacts first name\n";
cin >> fName;
cout << "Type the contacts last name\n";
cin >> lName;
//int j = 0;
aInFile.open(OUTPUT_FILE);
checkFileOpen(aInFile);
bool flag = false;
while(!aInFile.eof()){
        aInFile >> contacts1;
        string x = contacts1;
        aInFile >> contacts2;
        string y = contacts2;
        aInFile >> contacts3;
        string z = contacts3;
        name = x + "/t" + y + "/t" + z;
}
aInFile.close();
aOutFile.open(OUTPUT_FILE, ios::app);
checkFileOpen(aOutFile);
for(int j = 0; j < NUMBER; j++){
        if (fName == y && lName == x){
                bool flag = true;
                continue;
        }
        if (flag == false){
                aOutFile <<name <<endl;
       }
}
aOutFile.close();
}

Lines 14 - 22: You aren't storing name in any type of array OR vector. You are just continually writing over it and losing the information.

Lines 15, 17, 19 - You never declare contacts1, contacts2, contacts3. If they are string variables, you need to declare them as such before you use them. Also, use more descriptive variable names such as contactFName, contactLName, contactPhone, so your code can be comprehended. Similarly "record" is a better variable name than "name" because that string contains more than just the name. To avoid confusion about what variables contain what information, it is important to use accurate variable names.

Line 29 - You can delete this line. It is not necessary.

Line 27 - Variables x, y, and z no longer exist at this line of code. They went "out of scope" at line 22. Any variables that are defined in the while loop from lines 14 through 22 do not exist after line 22. If you want them to exist after line 22, you must declare them BEFORE the while loop, not INSIDE the while loop. When in doubt, declare everything at the top of the function. That way they'll exist for the whole function.

Is there any way in which i just use arrays & strings to make this function work.
main idea is to first compare the first and last name enetered by the user with that of the firstname and last name stored on the file. if it comes out to be true then do nothing, if the comparison comes out to be false then write that name on the same file which will be used as the output file now.

Please help me get this work. it's becoming impossible for me:(

You're already using strings. This is a revision of what I recommended before that uses an array instead of a vector. It's not the best approach in the world probably, but it's the easiest I can think of right now. Basically it involves reading the data in twice. The first time is simply to count the number of records you have. The second time is to actually check to see if the record is in the phone book, etc., and delete it. It's not the most efficient algorithm in the world, but it should work for you:

void delete (string filename)
{
     string fname;
     string lName;
     string phonenum;
     // ask user for fName and lName

     ifstream ins;
     ins.open (filename.c_str ());

     int numRec = 0;  // numRec = # of records in phone book before deletion
     while (ins >> fname)
     {
          ins >> lName;
          ins >> phonenum;
          numRec++;
     }

     ins.close ();
     ins.clear ();
     ins.open (filename.c_str ());
     string records[] = new string[numRec]; // one time dynamic
              // allocation of array.
              // must use dynamic allocation of array because we don't
              // know number of records at compile time.

     for (int i = 0; i < numRec; i++)
     {
          // read first name, last name, phone num
          // from file, place in records[i].  Make sure
          // to add spaces between first name, last
          // name, phone.
     }

     ins.close ();
     ofstream outs;
     outs.open (filename.c_str ());
     for (int i = 0; i < numRec; i++)
     {
          bool deleteRecord;
          // flag deleteRecord as true if this record
          // should be deleted, false otherwise

          if (!deleteRecord)
               outs << records[i] << endl;
      }
      outs.close ();

     delete [] records;  // for memory management
                         // not the end of the world if
                         // you don't add this line, but
                         // a good habit to get into.
}

THANKS:)

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.