Thread: bool functions

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

    Exclamation bool functions

    [CODE]working on it 3 days and I'm still having trouble heres the code i have but it won't return anything at all . please write the code that i should use out please.


    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=0; start!=end;++start;--end
    {
    bool pal=false;

    if(start!==s[])
    {
    return false;
    }

    }

    return true;
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    This problem has been covered many times. A search can be helpful.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Code:
    bool palindrome(const char s[])
    {
        bool pal=true; // assume s is a palindrome until mismatch is found
        int start=0;
    
        int end=strlen(s);
    
        for(start=0; start<=end;start++)//you don't want to add one to start until the next time round
        {
            pal=false; //You lready defined the tupe at the top, no need to do it again
    
            if(s[start]!=s[end])//test if the first and last elements of your character array are the same
            {
                return false;
            }
            
            --end; //take one off end at the end of the loop, not in the condition
        }
    
    return true;
    }
    Last edited by UnclePunker; 11-27-2003 at 07:06 AM.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

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

    Exclamation bool functions

    thanks for the code but I'm still having a problem it still only returns palindrome true. could look at my program.[CODE]// 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);

    for(start=0; start<=end;start++)//you don't want to add one to start until the next time round
    {
    bool pal=false; //You lready defined the tupe at the top, no need to do it again

    if(s[start]!=s[end])//test if the first and last elements of your character array are the same
    {
    return false;
    }

    --end; //take one off end at the end of the loop, not in the condition
    }

    return true;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I suggest you keep pressing the "preview reply" before ever pressing submit again, because you're simply not getting this posting code with code tags right!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think your palindrome() function has a slight problem with the array index.
    I suspect end doesnt contain the correct index.
    Your pal variable isnt used either, and isnt needed.

    Code:
    bool palindrome(const char s[]) {
    	int len = strlen(s);
    	int max = len / 2; //loop until halfway point
    	len--; //now len contains last index
    	int i;
    	for (i = 0; i < max; i++)
    	{
    		if (s[i] != s[len - i])
    		{
    			return false; //not a palindrome
    		}
    	}
    	return true;
    }
    There might be some other problem though, since I would expect you to get palindrome false rather than true.

    EDIT:
    oops, didnt pay attention to the variable names.
    Last edited by laserlight; 11-27-2003 at 09:51 AM.

  7. #7
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    This should work, I see no reason for it not, minimum use of variables.

    Code:
    bool palindrome(const char s)
    {
                    int i = 0;
                    int errors = 0;
    
    	for (i=0; i<=(strlen(s)/2); i++)
    	{
    		if (s[i]!=s[strlen(s)-i])
    		{
    			errors++;
    		}
    	}
                    
                    if (errors<=0)
                    {
                                     return true;
                    }
    
    return false;
    }
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... UnclePunker, you have the same idea as me, but I think your implementation is somewhat inefficient, since strlen() is used too many times, when using it once will suffice.
    Also, shouldnt we just declare the string as not a palindrome once the first mismatch is found?
    No need to record the errors then, methinks.

  9. #9
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Well, the strlen thing I reckon we just differ in opinions there I just prefer to declare less variables, but I understand your point, the reason i put the errors count in there is that he said it still wasn't working I was just thinking of a different way of doing as he had tried declaring it as a palindrome instantly already, but I know what you mean, just giving a different angle to come from.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  10. #10
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Originally posted by UnclePunker
    Well, the strlen thing I reckon we just differ in opinions there I just prefer to declare less variables, but I understand your point, the reason i put the errors count in there is that he said it still wasn't working I was just thinking of a different way of doing as he had tried declaring it as not a palindrome instantly already, but I know what you mean, just giving a different angle to come from.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  11. #11
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    please don't double post, once is enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Classes help
    By DarkAlex in forum C++ Programming
    Replies: 13
    Last Post: 01-26-2008, 03:00 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM