Here is the entire code again showing withe the delete section:
Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string name = "";
string realfirst = "";
string lastname = "";
string datejoined = "";
string email = "";
string search = "";
string type = "";
string finish = "";
string file = "";
string deletemem = "";
do
{
cout<<"Welcome To The Tyranno Database"<<endl;
cout<<endl;
cout<<"What would you like to do? (press i for instructions): ";
getline(cin, type);
cout<<endl;
system ("CLS");
if (type == "i"){
cout<<endl;
cout<<"This is Tyranno Database Software v0.1"<<endl;
cout<<endl;
cout<<"This software can save information about Tyranno members"<<endl;
cout<<endl;
cout<<"To do this, type create into the opening screen"<<endl;
cout<<endl;
cout<<"To search for a member, type search into the opening screen"<<endl;
cout<<endl;
cout<<"To edit member information just use the create function"<<endl;
cout<<endl;
cout<<"To delete a member, type delete into the opening screen"<<endl;
cout<<endl;
cout<<"Press Enter";
cin.get();
system ("CLS");
finish = "no";
}
else if (type == "create"){
cout<<endl;
cout<<"Please enter the username of the member: ";
getline(cin, name);
cout<<endl;
name += ".txt";
ofstream a_file ( name.c_str() );
a_file<<name<<std::endl;
cout<<"Please enter the member's first name: ";
getline(cin, realfirst);
cout<<endl;
a_file<<realfirst<<std::endl;
cout<<"Please enter the member's last name: ";
getline(cin, lastname);
cout<<endl;
a_file<<lastname<<std::endl;
cout<<"Please enter the date the member joined: ";
getline(cin, datejoined);
cout<<endl;
a_file<<datejoined<<std::endl;
cout<<"Please enter the member's e-mail address: ";
getline(cin, email);
cout<<endl;
a_file<<email<<std::endl;
a_file.close();
system ("CLS");
cout<<"Would you like to finish? (yes / no): ";
getline(cin, finish);
system ("CLS");
}
else if (type == "search") {
cout<<"Please enter the username of the member: ";
getline(cin, search);
cout<<endl;
file = search + ".txt";
ifstream b_file ( file.c_str() );
if (!b_file.is_open()) {
cout<<"That file could not be opened"<<endl;
cout<<endl;
cout<<"Would you like to finish? (yes / no): ";
getline(cin, finish);
system ("CLS");
}
else {
cout<<"Username: "<<search<<endl;
cout<<endl;
b_file>>name;
b_file>>realfirst;
b_file>>lastname;
cout<<"Full name: "<<realfirst + " " + lastname<<endl;
cout<<endl;
b_file>>datejoined;
cout<<"Joined Tyranno on: "<<datejoined<<endl;
cout<<endl;
b_file>>email;
cout<<"E-mail address: "<<email<<endl;
cout<<endl;
cout<<"Press Enter";
cin.get();
b_file.close();
cout<<endl;
system ("CLS");
cout<<"Would you like to finish? (yes / no): ";
getline(cin, finish);
system ("CLS");
}
}
else if (type == "delete") {
cout<<"Please enter the username of the member to delete: ";
getline(cin,name);
cout<<endl;
cout<<"Are you sure that you would like to delete the member "<<name<<"? ";
getline(cin, deletemem);
cout<<endl;
if (deletemem == "yes") {
name += ".txt";
ofstream c_file ( name.c_str() );
if (!c_file.is_open()) {
system ("CLS");
cout<<"That file does not exist to be deleted"<<endl;
cout<<endl;
cout<<"Press Enter";
cin.get();
finish == "no";
}
else {
remove ( name.c_str() );
cout<<"Deleted"<<endl;
cout<<endl;
cout<<"Press Enter"<<endl;
cin.get();
finish == "no";
system ("CLS");
}
}
else {
finish == "no";
system ("CLS");
}
}
else {
cout<<"Sorry, Tyranno Database Software does not want to do that!"<<endl;
cout<<endl;
cout<<"Would you like to finish? (yes / no): ";
getline(cin, finish);
system ("CLS");
}
}
while (finish == "no");
}
When you search for the deleted file, it still manages to open it.