Thread: palindromes and bool

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    Exclamation palindromes and bool

    I have been working on this for 8 hours and i'm and still having problems with the bool palindrome definition at the end of the program. when i execute it always comes up as palindrome true even if it's not. please help








    // A Program that lets a user enter a line of text and states
    // weather or not the line is a palindrome

    #include <iostream> // provides cout, etc.
    #include <fstream> // provides file functions
    #include <cstdlib> // provides exit()
    #include <cstring> // provides strlen()
    using namespace std;

    const int MAXSTRINGSIZE= 80; // no more than 79 characters used
    const char SENTINEL[]="stop!"; // used to end data entry

    void explain_prog(ostream& os);
    // Explains what the program does

    void read_string(istream& is,char s[],int maxlength);
    // postcondition: a string s of at most maxlength-1 characters has
    // been read from input stream is

    void write_string(ostream& os,const char s[]);
    // precondition: string s has a value
    // postcondition: s is written to output stream os

    void blank_remover(const char s1[],char s2[]);
    // precondition: s1 has a value
    // postcondition: s2 is s1 with all the blanks removed

    bool palindrome(const char s[]);
    // precondition: s is a string with all blanks removed
    // postcondition: value true returned if s is palindrome
    // otherwise false is returned

    int main()
    {
    char s1[MAXSTRINGSIZE], // holds one line of text
    s2[MAXSTRINGSIZE]; // s1 with all blanks removed

    bool pal; //true if s1 is a palindrome, otherwise false
    ofstream fout; // internal name for output file

    explain_prog(cout); // explain program to User at terminal

    // open output file - check for errors opening
    fout.open("pal.out");
    if(fout.fail())
    {
    cout << "\nOutput file opening failed!\n";
    exit(1);
    }

    // document output file
    fout << "\nJustin Cochrane CMSV1180\nAssignment #9 Question #1\n\n";
    explain_prog(fout);

    // get first line or SENTINEL
    cout<< "Please enter line of text or" <<SENTINEL<<endl;

    read_string(cin,s1,MAXSTRINGSIZE);

    while(strcmp(s1,SENTINEL)!=0)
    {
    fout<< "\nLine entered.";
    write_string(fout, s1);

    // remove blanks from s1 and store in s2
    blank_remover(s1,s2);

    pal=palindrome(s2);

    if(pal)
    {
    cout<< "line of text is a palindrome";
    fout<< "line of text is a palindrome";
    }
    else
    {
    cout<< "line of text is not a palindrome";
    fout<< "line of text is not a palindrome";
    }

    //get next line of text or SENTINEL
    cout<< "Please enter a line of text or"<<SENTINEL<<endl;
    read_string(cin,s1,MAXSTRINGSIZE);
    }

    // close files
    fout.close();


    return 0;
    }

    void explain_prog(ostream& os)
    {
    os << "\nThis program lets a user enter a line of text "
    << "and states weather it is a palindrome or not";

    }

    void read_string(istream& is, char s[], int maxlength)
    {
    char ch; //used to read eoln character
    is.get(s,maxlength);
    is.get(ch); //read eoln character

    }

    void write_string(ostream& os, const char s[])
    {
    os <<s<<endl;
    }

    void blank_remover(const char s1[],char s2[])
    {
    int n=strlen(s1); // characters in s1
    int j=0; // index for s2
    for(int i=0; i<n; ++i)
    {
    if(s1[i]!=' ')
    {
    s2[j]=s1[i];
    ++j;
    }
    }
    s2[j]='\0'; // put null character in s2
    }

    bool palindrome(const char s[])
    {
    bool pal=true; // assume s is a palindrome until mismatch is found
    int start=0;
    int end=strlen(s);


    while(start<end)
    {
    end--;
    start++;
    bool pal=false;
    }

    return start;
    }[CODE]

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Notice that you never actually compare any part of the string. You have your index variables, but you never use them to get the character at that index point in the string. Next, you redefine pal in a narrower scope, so, even if you were comparing letters, you would be setting a variable which immediately goes out of scope. Next, you don't return pal, you return start, which will always be true (given the loop conditions and increment operator) unless end == 0.

    Additionally, once you make the comparison, I would not bother setting any variable, but rather immediately return false. So, if the loop made it all the way through without returning, it is a palindrome, so it can return true.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Just for your information you can read this (http://cboard.cprogramming.com/showt...threadid=13473) if you haven't already and try to fix your code tag problem. Also, use the preview post button to see if it the code tags worked, and if they didn't, fix them before you finally submit your post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromes
    By jewelz in forum C++ Programming
    Replies: 57
    Last Post: 02-23-2009, 06:11 PM