#include <iostream> //standard input and output
#include <fstream> //file input and output
#include <string> //for strings
#include <exception> //catches exceptions
#include <sstream> //stringstream comes from here
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
bool writeFile(const string &data); //writes strings to file
bool readFile(); //read file
bool palin(const string &data);
bool operator==(const stack<char>&, const queue<char>&);
bool operator!=(const stack<char>&, const queue<char>&);
void menu(); //display menu of choices
int main(int argc, char *argv[]) {
stack<char> *s;
queue<char> *q;
string input;
string::iterator i;
menu(); //launch menu
return 0;
}
bool palindrome(const char word[], int size)
{
return equal(word, word+size/2, std::string(word, word+size).rbegin());
}
//writes strings to file
bool palin(const string &data){
try
{
ofstream fout("data.txt",ios::app); //open file for writing and append to it
fout<<data<<endl;
/* if(palindrome(word.data(), word.size()))
{ fout << word << " is a palindrome!\n";}
else
{ fout << word << " is NOT a palindrome!\n";}*/
fout.close(); //close the file
return true; //return true for success
}
catch(exception &e)
{
cerr<<e.what(); //write any exceptions that may occur when trying to write to file
return false; //return false for error
}
}
bool writeFile(const string &data)
{
try
{
ofstream fout("data.txt",ios::app); //open file for writing and append to it
fout<<data<<endl; //write data to file
fout.close(); //close the file
return true; //return true for success
}
catch(exception &e)
{
cerr<<e.what(); //write any exceptions that may occur when trying to write to file
return false; //return false for error
}
}
//read strings from file
bool readFile()
{
try
{
ifstream fin("data.txt"); //open a file for reading
string data;
string word;
//keep reading file until there is no more data to be read
while(getline(fin,data))
{
cout<<data<<endl; //output file data
cout<<word<<endl;
}
fin.close(); //close file
return true; //return true for success
}
catch(exception &e)
{
cerr<<e.what()<<endl; //display any error that may occur
return false;
}
}
bool operator==(const stack<char> &s1, const queue<char> &q1) {
bool eq = true;
stack<char> s = s1;
queue<char> q = q1;
if (s.size() == q.size()) {
while ((s.empty() == false) && (eq == true)) {
eq = (s.top() == q.front());
s.pop();
q.pop();
}
} else {
eq = false;
}
return eq;
}
bool operator!=(const stack<char> &s, const queue<char> &q) {
return !(s == q);
}
//display menu
void menu()
{
string choice; //hold user choice value for menu option
int value; //hold choice value in integer form
string strData = ""; //writes strings to file
//loop until the user choices 3 to exit
stack<char> *s;
queue<char> *q;
string input;
string::iterator i;
while(true)
{
cout<<"1. write to file"<<endl;
cout<<"2. read file"<<endl;
cout<<"3. exit"<<endl;
cout<<"4. check palin"<<endl;
cout<<"Option: ";
getline(cin,choice); //get input from user
stringstream(choice)>>value; //convert user input to integer
switch(value)
{
case 1: //writes data to file
cout<<"Write: ";
getline(cin,strData);
writeFile(strData);
break;
case 2: //reads data from file
readFile();
break;
case 3: //exit program
exit(0);
break;
case 4:
while (true) {
s = new stack<char>;
q = new queue<char>;
cout << "> ";
getline(cin,input);
for (i = input.begin(); i != input.end(); i++) {
s->push(*i);
q->push(*i);
}
cout << input << " is ";
if (*s != *q) {
cout << "not ";
}
cout << "a palindrome." << endl;
delete s;
delete q;
palin(input);
break;
}
default: //No valid option giving
break;
}
}
}
this are my coding ..what missing is delete function and loading function from menu ..i try to do it , but could not get a solution , delete function is, example inside a txt file it have data , milk , sugar ..so i want to delete the one of those , and the loading part is where u loading data from different txt file ...please help me ..i'm newbies to this kind of thing , haven't sleep for 2 days to do this thing :-(