I know I am close, but when I compile and try to run it, I get an error that says:
"Unhandled Exception: c0000005
At Address: 004018de"
Here is my code, does anyone have any idea what could be causing this error? Windows suggests sending an error report, but somehow I don't think they're going to help me.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
string readLineFromFile(ifstream& fin);
string cleanString(const string& line);
void displayPalindrome(const string& palindrome);
void displayErrorAndStop(void);
bool isPalindrome(const string& line);
bool isEmpty(const string& line);
int main( void )
{
string x; //declare string variable to store line
string y; //string variable to store clean string
ifstream fin("palindromes.txt");
if (!fin)
{
displayErrorAndStop();
}
else
{
while(! fin.eof() )
{
x=readLineFromFile(fin);
if (!isEmpty(x))
{
y=cleanString(x);
if (isPalindrome(y))
{
displayPalindrome(x);
}
}
}
}
fin.close();
return 0;
}
void displayErrorAndStop(void)
{
cout << "Error opening input data file!";
exit(1);
}
string readLineFromFile(ifstream& fin)
{
string x;
fin >> x;
if(!fin)
exit(1);
return x;
}
bool isEmpty(const string& line)
{
if (line=="")
return true;
else
return false;
}
string cleanString(const string& line)
{
string clean;
string lowerCase;
for(int i=0;i<line.length();i++)
{
lowerCase[i]=tolower(line[i]);
}
for(int j=0;j<lowerCase.length();j++)
{
char symbol=lowerCase[j];
if (isalnum(symbol))
{
clean = clean + symbol;
}
}
return clean;
}
bool isPalindrome(const string& line)
{
int i=0;
int j=0;
bool b=0;
j=line.length();
while(i<=j)
{
char front=line[i];
char back=line[j];
if (front==back)
{
b=1;
i++;
j--;
}
else
{
b=0;
}
}
return b;
}
void displayPalindrome(const string& palindrome)
{
cout << palindrome << "is a pilindrome.";
}