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.