I have this code, and in switch, case 3, i wanted to delete file, but there\'s e
ID: 3636469 • Letter: I
Question
I have this code, and in switch, case 3, i wanted to delete file, but there's error in deleting file, and i cant figure it out. hope anyone can help me solve this problem. thank you#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct AVLNode
{
string Name;
int Balance;
AVLNode *LeftChild;
AVLNode *RightChild;
};
AVLNode *InputData(fstream &);
AVLNode *InsertData(AVLNode *, AVLNode *);
int FindBalance(AVLNode *);
void Traversal(AVLNode *);
string find(string &);
AVLNode *LeftLeft(AVLNode *);
AVLNode *RightRight(AVLNode *);
AVLNode *LeftRight(AVLNode *);
AVLNode *RightLeft(AVLNode *);
int main()
{
int choice;
string yes_no;
//ifstream CheckFile; //ifstream checks for file existence
fstream CheckFile;
char inputFileName[]="phone.txt";
CheckFile.open(inputFileName); //attempts to open read file, and tests for existence
/*if(!CheckFile)
cout << "Input file does not exist." << endl << endl;
else
cout <<"file open"<<endl;*/
AVLNode *head = InputData(CheckFile);
cout << "travesal: ";
Traversal(head);
cout<<" Do you want to continue?[Yes or No]: ";
cin>>yes_no;
while(yes_no=="Yes"||yes_no=="yes")
{
cout<<"==========Menu==========";
cout<<" 1. Insert new name 2. Search existing name 3. Remove existing name 4. Print Tree ";
cout<<" Please enter your choice [1,2,3,4]: ";
cin>>choice;
switch(choice)
{
case 1:
{
string name, phone;
cout<<" enter Name : ";
cin>>name;
cout<<"enter Phone : ";
cin>>phone;
ofstream myfile;
myfile.open ("phone.txt", ios::app);
if(!myfile)
cout << "Input file does not exist." << endl << endl;
else
cout <<""<<endl;
myfile <<" "<<name<<" "<<phone ;
myfile.close();
break;
}
case 2:
{
string searchLine;
string dataToSearch, name, phone;
int offset;
ifstream myfileSearch1 ("phone.txt");
if (myfileSearch1.is_open())
{
while (myfileSearch1.good()) //while(!myfile.eof())
{
getline (myfileSearch1,searchLine);
//cout << searchLine << endl;
}
myfileSearch1.close();
}
cout<<"Enter name or phone number that you want to search: ";
//cin>>dataToSearch;
cin>>name;
cout<<"Enter name or phone number that you want to search: ";
cin>>phone;
dataToSearch = name " " phone;
ifstream myfileSearch2 ("phone.txt");
if(myfileSearch2.is_open())
{
while(!myfileSearch2.eof())
{
getline(myfileSearch2,searchLine);
if ((offset = searchLine.find(dataToSearch, 0)) != string::npos) {
cout << "found '" << dataToSearch << endl;
}
}
myfileSearch2.close();
}
else
cout<<"Unable to open this file."<<endl;
break;
}
case 3:
{
string line;
// open input file
ifstream in("phone.txt");
if( !in.is_open())
{
cout << "Input file failed to open ";
return 1;
}
// now open temp output file
ofstream out("newphone.txt");
// loop to read/write the file. Note that you need to add code here to check
// if you want to write the line
string dataToDelete,name,phone;
cout<<"text to delete : ";
cin>>name;
cin>>phone;
dataToDelete = name " " phone;
//getline(cin,dataToDelete);
cout<<" dataToDelete : "<<dataToDelete;
int a = 0;
bool del=false;;
while( getline(in,line) ) //loop copy
{
//if(line != "I want to delete this line")
//if(line != "bbb")
if (a==0){
//out <<line;
if(line != dataToDelete){
//out << line << " ";
out << line;
del = false;
}
else{
del = true;
}
}
else{
if(del==true && ((a-1)==0) ){
if(line != dataToDelete)
out << line;
}
else{ //del==false
if(line != dataToDelete){
out << " " << line;
}
}
}
a ;
}
in.close();
out.close();
// delete the original file
remove("phone.txt");
if( remove( "phone.txt" ) != 0 )
cout<<"Error deleting file";
else
cout<<"File successfully deleted";
// rename old to new
rename("newphone.txt","phone.txt");
//fstream NCheckFile;
//NCheckFile.open("newphone.txt");
//AVLNode *head = InputData(NCheckFile);
//cout<<"travesal: ";
//cout<<"Printing.. ";
//Traversal(head);
// all done!
break;
}
case 4:
{
//string wish;
CheckFile.open(inputFileName); //attempts to open read file, and tests for existence
//cout<<"Open Latest Phone List? [Yes or No]: ";
//cin>>wish;
//if((wish == "Yes")||(wish == "yes"))
//{
fstream NewCheckFile;
NewCheckFile.open ("phone.txt");
/*if(!NewCheckFile)
cout << "Input file does not exist." << endl << endl;
else
cout <<"file open"<<endl;*/
AVLNode *head = InputData(NewCheckFile);
cout<<"travesal: ";
cout<<"Printing.. ";
Traversal(head);
CheckFile.close();
/*}
else
{
fstream OldCheckFile;
OldCheckFile.open ("phone.txt");
AVLNode *head = InputData(OldCheckFile);
cout<<"travesal: ";
cout<<"Printing.. ";
Traversal(head);
}*/
break;
}
}
cout<<" Do you want to continue?[Yes or No]: ";
cin>>yes_no;
}
system("pause");
}
AVLNode *InputData(fstream &InFile)
{
AVLNode *Root = NULL;
AVLNode *Leaf;
while(!InFile.eof())
{
Leaf = new AVLNode;
getline(InFile, Leaf->Name);
Leaf->Balance = 0;
Leaf->RightChild = Leaf->LeftChild = NULL;
if(Root == NULL)
Root = Leaf;
Root = InsertData(Leaf, Root);
}
return Root;
}
AVLNode *InsertData(AVLNode *Leaf, AVLNode *Root)
{
if(Root == NULL)
return Leaf;
else if(Leaf->Name < Root->Name)
{
Root->LeftChild = InsertData(Leaf, Root->LeftChild);
if(FindBalance(Root->LeftChild) - FindBalance(Root->RightChild) == 2)
{
if(Leaf->Name < Root->LeftChild->Name)
Root = LeftLeft(Root);
else
Root = LeftRight(Root);
}
}
else if(Leaf->Name > Root->Name)
{
Root->RightChild = InsertData(Leaf, Root->RightChild);
if(FindBalance(Root->RightChild) - FindBalance(Root->LeftChild) == 2)
{
if(Leaf->Name > Root->RightChild->Name)
Root = RightRight(Root);
else
Root = RightLeft(Root);
}
}
Root->Balance = max(FindBalance(Root->LeftChild), FindBalance(Root->RightChild)) 1;
return Root;
}
int FindBalance(AVLNode *Root)
{
if(Root == NULL)
return -1;
else
return Root->Balance;
}
AVLNode *LeftLeft(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->LeftChild;
Rotate->LeftChild = Pivot->RightChild;
Pivot->RightChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) 1;
Pivot->Balance = max(FindBalance(Pivot->LeftChild), FindBalance(Rotate->RightChild)) 1;
return Pivot;
}
AVLNode *RightRight(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->RightChild;
Rotate->RightChild = Pivot->LeftChild;
Pivot->LeftChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) 1;
Pivot->Balance = max(FindBalance(Pivot->RightChild), FindBalance(Rotate->LeftChild)) 1;
return Pivot;
}
AVLNode *LeftRight(AVLNode *RotateTop)
{
RotateTop->LeftChild = RightRight(RotateTop->LeftChild);
return LeftLeft(RotateTop);
}
AVLNode *RightLeft(AVLNode *RotateTop)
{
RotateTop->RightChild = LeftLeft(RotateTop->RightChild);
return RightRight(RotateTop);
}
void Traversal(AVLNode *Root)
{
AVLNode *temp;
if(Root != NULL)
{
Traversal(Root->LeftChild); // print left subtree
cout << "(" << Root->Name << ", "; // print this node
if(Root->LeftChild == NULL)
cout << "NULL, ";
else
{
temp = Root->LeftChild;
cout << temp->Name << ", ";
}
if(Root->RightChild == NULL)
cout << "NULL, ";
else
{
temp = Root->RightChild;
cout << temp->Name << ", ";
}
int temp1 = (FindBalance(Root->RightChild) - FindBalance(Root->LeftChild));
cout << temp1 << ")" << endl;
Traversal(Root->RightChild); // print right subtree
}
return;
}