I am getting myself deeper into trouble on a program I am working on. I strated out with only four errors and am now up to twelve (I am currently learning and working with six languages-c++,html,javascript,PHP,SQL,and c#). I think my languages are running together!
This program is from a textbook, but is NOT homeework, I am trying to write this as practice for what we are learning on classes and constructors. This asks you to write a program using a class to determine if input is a palindrome. Similar posts have been in here, but not using a class. I will be going to a tutorial next week on this, but I would like to continue to work through it in the meantime. Can someonre help me back onto track.

#include <string>
#include <iostream>
using namespace std;
//create class
class PString : public string
{
public:
	PString(const string &aString);
	bool isPalindrome() const;
};
//constructor
PString::PString( const string &aString ) : string( aString )
{

}
//funct to check pal
bool PString::isPalindrome() const
{string upperCase (string str)
{
	for (int i = 0; i < str.size(); i++)
		if(islower(str[i]))
			str[i] = toupper(str[i]);
	return str;
}
bool checkPalindrome(string str)
{
	int n = str.size();
	for (int i=0; i<str.size()/2;i++)
		if (str[i] !=str[n-1-i])
			return false;

	return true;
}

int main()
{
 string str;
 cout << "This program is a palindrome-testing program. Enter a string to test:\n";
 cin >> str;

 // Create a PString object that will check strings
 PString s(str);

 // Check string and print output
 if (s.isPalindrome())
 {
	cout << s << " is a palindrome";
 }
 else
 {
 cout << s << " is not a palindrome";
 }
    cout << endl;
    system("pause");
    return 0;
 }

Recommended Answers

All 5 Replies

Why are you inheriting from string on line 5? There's no need to do that.

You should have a private member variable that's of type std::string to receive your aString variable on line 12. I don't believe what you are trying to do will work.

c++,html,javascript,PHP,SQL,and c#

Get a copy of Partition Magic that works on your brain and section some of those off... ;)

I'll drink to the copy of partition magic, it might help!
Here was the first program itteration I did, the reason for the inhertance was trying to incorporate that aspect of the language into the program since it's what we have been working on.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

// This class represents a string that
//  can be tested to see if it is a palindrome
class PString : public string
{
	public:	//1 fct prototype and 1 full constructor 
    
	bool isPalandrome();
PString(string)

};


// Function to test palindromes
//bool PString::isPalindrome()
{
    
	int lower =0;	//start
	int upper =length()-1; //end\
	while (lower < upper)
	{
	//found mis match
	return false;
	}
	lower++
	upper--;
	}
	//no mismath found
	return true;
}
int main ()
{
    // Request input from user
    string str;
    cout << "This program is a palindrome-testing program." << endl;
	cout << "Enter a string to test: " << endl;
    cin >> str;

    // Create a PString object that will check strings
    PString s(str);

    // Check string and print output
    if (s.isPalindrome())
        cout << s << " is a palindrome";
    else
        cout << s << " is not a palindrome";
    cout << endl;

   system("pause");
   return 0;
}

Read and reread the first response to this question.
http://stackoverflow.com/questions/4205050/inheriting-and-overriding-functions-of-a-stdstring

std::string is a typedef for a template class, so you would have to take that into account with your inheritance, etc.

Adjust your class declaration

class PString 
{
    public:	//1 fct prototype and 1 full constructor 
      bool isPalandrome();
      PString(string str); //shorthand, add your initialization list code onto this, add your const correctness too.
    private:
      std::string mystring; //let the constructor set this
};

OK, I walked away from this for a cuple of days, re-read the notes I have, and have re-written the program. It works but....only if the input is all lowercase or uppercase-not a combination. for example- hannah works and is seen as a palindrome but Hannah does not. That it works gets me where I want, but I want to get it to work with any palindrome put in.

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;

// This class represents a string that
//  can be tested to see if it is a palindrome
class PString : public string
{
    public:	
      bool isPalindrome();
      PString(string s) : string(s.
   {
   
   }
};

// Function to test palindromes
bool PString::isPalindrome()
{
   	int lower =0;	//start
	int upper =length()-1; //end\
	while (lower < upper)
	{
		if((*this) [lower] != (*this) [upper])
		{
	//found mismatch
		return false;
		}
	lower++;
	upper--;
	}
	//no mismatch found
	return true;
}
int main ()
{
    // Request input from user
    string str;
    cout << "This is a palindrome-testing program." << endl;
	cout << "Enter a string to test: " << endl;
    cin >> str;

     PString s(str);

    // Check string and print output
    if (s.isPalindrome())
        cout << s << " is a palindrome";
    else
        cout << s << " is not a palindrome";
    cout << endl;

   system("pause");
   return 0;
}

Ok, but what are you inheriting from string? Nothing. Use composition (that is, make a std::string a member of your class rather than inheriting anything).

The simplest solution to the problem your code is having now is to transform the entire string to lowercase and test whether that is a palindrome. Make a private "to lower" function for your class (a couple of lines, really), and call it from your constructor. Otherwise, you're creating a problem for yourself because 'h' is very different from 'H'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.