Palindrome Program. Help Please!

My job is to read in a line of data from an input file, remove all white space and punctuation, then check to see whether or not it is a palindrome. The input file is an arbitrary length long including no input. I need to read in a line of data at a time then process it until there is no more data to process.
The output should look something like this.

Input Value: Sit on a potato Pan otis!!!
Is it a palindrome? Yes

Input Value: race car
Is it a palindrome? Yes

Input Value: wooHaaaaa
Is it a palindrome? No

I am very new to Programming so anything that you smarter people can do to help me would be greatly appreciated!
This is what I have right now:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

ifstream infile;
ofstream outfile;

int main()
{
ifstream inData;
ofstream outData;
while(!infile.eof())
{
char sentence[90];

inData.open("InPalindrome.txt");
outData.open("OutPalindrome.txt");

if (!inData)
{
cout << "Can't open input file successfully";
getchar ();
return 1;
}
if (!outData)
{
cout << "Can't open output file successfully";
getchar ();
return 2;
}

outData << "Input Value :"<<endl;

inData >> sentence;

int x = strlen(sentence)-1;



for(int i = 0; i <= x; i++)
{
if (sentence[i] == sentence[x-i])
{
continue;
}
else
{
outData<<"Sentence is a palidrome"<<endl;
getchar ();
return 0;
}
}


outData<< "Sentence is a Palidrome"<<endl;


}
inData.close();
outData.close();
getchar ();
return 0;
}


My debug is successful but it just doesn't work how I would like it too
Thanks again!
Last edited on
Hello,

first of all, please use code tags. makes your code a lot easier to read for us! thnks.

first thing i noticed you open the file inside a while loop.
What you need to do is open the file en then get all the data out.
Not open it every time:

1
2
3
4
5
6
7
8
9
10
ifstream inData;
inData.open("InPalindrome.txt");
string sentence;
while(!infile.eof())
{
  /* get sentence */
  getline (inData,sentence);

  /* check palidrome ( I did not check this part )*/
}


Hello thanks for replying!

I am kind of confused on how to integrate what you told me to do, into my program.
I started over because something happened with my visual studio and took a slightly different approach. I posted the new code in a new topic discussion

Topic archived. No new replies allowed.