Fstream Palindrome

Hey guys, I am really frustrated right now, I have tried to fix as many errors as I can and I've reduced the errors from a lot to a little, and I was finally able to get my code to compile, but it is very, very wrong and not what I intended. I wanted multiple functions, one to get the text file of the palindromes, one to see whether or not the list from the text file are palindromes, and a last function as the output displaying the entire list, if they were or weren't a palindrome.

so it would look like
“Able was I ere I saw elba” is a palindrome.
“Yes” is not a palindrome.
“Go hang a salami Im a lasagna hog” is a palindrome.
“Dennis and Edna sinned” is a palindrome.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 #include <fstream>
#include <iostream>
#include <string>
using namespace std;

string processFile();
string  processPalindrome(string,ifstream&,char*);
void printOutput(string);

char str[80];
bool isPalindrome = true;

int main()
{
        string input;
        string checkPalindrome;
        char str[80];
        ifstream inputFile;

        input = processFile();
        checkPalindrome = processPalindrome(input,inputFile,str);
        printOutput(checkPalindrome);
        return 0;
}

string processFile()
{
        string input;

        ifstream inputFile;
        inputFile.open ("palindromes.txt");

        if (!inputFile)
        {
        cout <<"Error: File not found, now exiting \n";
        return 0;
        }
        else
        return input ;
}

string processPalindrome(string input, ifstream& inputFile, char* str)
{
        int len= 0;
        string checkPalindrome;


        while (!inputFile.eof())
        {
                inputFile.getline(str,80);
                for(int i = 0; i <= len; i++)
                        {
                        if (str[i] != str[len- i])
                        {
                        isPalindrome=false;
                        break;
                        }
                        if(isPalindrome)
                        return checkPalindrome;
                        }
        }
}

void printOutput(string checkPalindrome)
{
        if(isPalindrome)
        cout<<str<< "-- A palindrome.\n";
        else
        cout<<str<< "-- Not a palindome.\n";
}
Last edited on
can someone please help me? I really need this done :(
Well, first, 'len' is always 0 inside your loop.

Any particular reason for the choice to mix character arrays and strings?

And the indexes in the loop are a little off. Is your algorithm case sensitive? What about spaces? We don't count spaces when considering palindromes, but your algorithm will.
You've got the right idea in your code. It seems that you're just getting tripped up over some syntax and a structure.

I wanted multiple functions, one to get the text file of the palindromes, one to see whether or not the list from the text file are palindromes, and a last function as the output displaying the entire list, if they were or weren't a palindrome.


That's the first problem. Think about it: if you want to process the entire file before printing any of the output, then you'd have to store the whole file within your program, along with whether each line is a palindrome. A better way to handle it is like this:
1
2
3
4
while (file is good) {
    read a line
    process the line
}


And to procss a line:
1
2
3
4
5
6
print the line
    if (line is a palindrome) {
      print "is a palindrome\n";
    else {
      print "is not a palindrome\n"
    }


To that end, here are my specific suggestions:
1. Get rid of processFile(). Instead, open the file and check whether it's open inside main().

2. Put the read/process loop inside main. There's a handy way to check for errors/end of file:
1
2
3
4
string str;
while (getline(fstream, str)) {
    processLine(str);
}

getline() returns the stream (fstream in this case). while() is looking for a bool and there just so happens to be a bool conversion built into the stream class that calls the good() method.

3: Add processLine(). It looks almost exactly like your printOutput function:
1
2
3
4
5
6
7
8
void processLine(string &str)
{
    if (isPalindrome(str)) {
        cout<<str<< "-- A palindrome.\n";
    } else {
        cout<<str<< "-- Not a palindome.\n";
    }
}


4. Add the isPalindrome() function. This takes a string and returns true or false, depending on whether it's a palindrome:
1
2
3
4
5
6
7
8
9
10
11
bool isPalindrome(string &str)
{
    int len = str.size();
    for(int i = 0; i <= len; i++) {
         if (str[i] != str[len- i]) {
             return false;
         }
    }
    // If you get here then it's a palindrome
    return true;
}


See if you can put all that together. Once it's working, look at isPalindrome() and see if you can make it faster. (Hint: the code written actually checks each character twice).

I don't think your processFile function is actually doing anything

you declare a string input, you open a file, but you don't do anything with the opened file and you just return an empty string "input"

you also didnt close the file, idk how serious that is but its just good practice to always close a file

So you need to read in the values of the text file into a variable and return that variable, so far your function doesnt really do anything
Topic archived. No new replies allowed.