Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C++ help


  • Please log in to reply

#1
Worf

Worf

    New Member

  • Member
  • Pip
  • 7 posts
I'm sure anyone who has done a bit of program will know what to do here, it's a pretty basic problem, but I am something of a newbie. The problem is to read a file in and recognise what in it is a palindrome.

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.";
}
  • 0

Advertisements


#2
StephenL

StephenL

    Member

  • Member
  • PipPip
  • 12 posts
Hi, can you include your text file so I can run it through with the data you are using please?

Cheers,
Steve
  • 0

#3
StephenL

StephenL

    Member

  • Member
  • PipPip
  • 12 posts

string readLineFromFile(ifstream& fin)
{
string x;
fin >> x;

if(!fin)
exit(1);

return x;

}


There's the offending article :

if(!fin)
exit(1);

Commenting out those 2 lines causes it to run correctly - although it seems to think 'hello' & 'goodbye' are palindromes (suspect that's your logic though :whistling: )

Cheers,
Steve

Edited by StephenL, 28 March 2006 - 01:05 PM.

  • 0

#4
Worf

Worf

    New Member

  • Topic Starter
  • Member
  • Pip
  • 7 posts
Hi Steve, managed to get it to run before your reply. Didn't change what you said to change, the error causing the exception was in the cleanString function.

I changed it to

string cleanString(const string& line)
{
string clean;

for (int i=0; i<line.length(); i++)
{
if (isalnum(line[i]))
clean += tolower (line[i]);
}
return clean;
}

and it ran without an error, but didn't recognize palindromes properly.

The error that was causing it to not recognize the palindromes was in the isPalindrome function. I had to realize that the count for the .length started count at zero, so I changed it to say "-1" at the end, and it worked perfectly, I'll paste it below.

bool isPalindrome(const string& line)
{
int i=0;
int j=0;

j=line.length()-1;


char front=line[i];
char back=line[j];

if (front==back)
{
i++;
j--;
}
else
{
return false;

}

return true;
}

I did that and it worked like a charm. I'll paste the text file below in case you want to play with it.

Thanks for your help.

Attached Files


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP