I need help with a programing assignment for my CS 2 class, the task is to check rather a user inputted string is a palindrome meaning it is the same forwards as it is backwards. I have most of the program working, I input a string then copy it and reverse it and I got to the part were I need to compare the reversed string with the original and am getting an error message I don't understand. Here is my code so far:
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
//class implimentations.
//******************************************************************************
class pstring
{
private:
string testString;
int sizeOfstring;
//size variable is needed to know how many characters are in
//the string so that it can be transversed via loops.
string *ptr;
//creats a pointer to be used with the new operator to
//create a new string array to hold a reversed copy of the
//original string.
public:
//***CONSTRUCTOR***
pstring(string input, int size)
{
testString=input;
sizeOfstring=size;
}
//*****OBJECT'S MEMBER FUNCTIONS PROTOTYPES*****
bool isPalindrome();
//check function that compars elements in string and sees if
//they are equal.
void pstring::copyString();
//Using dynamic memory creates a second string to serve as the
//destination of the reversed string.
};
//******************************************************************************
//Function prototypes
void gatherInput(string &input, int &size);
//Instructs the user about what they are supposed to input and allows them
//to input the string of their choice and the size.
void output(bool const &palindrome);
//******************************************************************************
//******************************************************************************
int main()
{
string input;
int size;
bool palindrome;
gatherInput(input, size);
pstring one(input, size);
//creates a pstring object for use in testing of the class.
one.copyString();
palindrome=one.isPalindrome();
//output(palindrome);
return 0;
}
//******************************************************************************
//******************************************************************************
//Function implimentations
bool pstring::isPalindrome()
{
unsigned int truth[sizeOfstring];
//an array to hold the true false values calculated for each element
//in the string.
char var1, var2;
//the char variabls are used to hold an element in each of the arrays while
//it is being compared.
for(int n=0; n< sizeOfstring; n++)
{
var1=ptr[n];
var2=testString[n];
if(var1==var2)
{
truth[n]=1;
}
else if(var1!=var2)
{
truth[n]=0;
}
else
{
cout << endl << "An error has occured." << endl;
//this line is just a catch in case some how the value for the
//string is not catched in either of the first part of
//the conditional statment.
}
}
}
//******************************************************************************
void pstring::copyString()
{
ptr= new string[sizeOfstring];
//creates the second aray the size of user string.
for(int n=0; n< (sizeOfstring); n++)
{
ptr[n]=testString[sizeOfstring-(n+1)];
}
for(int n=0; n< sizeOfstring; n++) //test print function.
{
cout << ptr[n] << " " << testString[n] << endl;
}
}
//******************************************************************************
//Non-class function implimentations
void gatherInput(string &input, int &size)
{
cout<< "Please enter the string you would like to test " << endl;
cout<< "to see if it is a Palindrome." << endl;
cout << "The string entered should have no spaces and should be" << endl;
cout << " a single word." << endl;
cin >> input;
cout << endl;
//Instcuts the user in the guidlines of the string input and allows them
//to enter the string of their choice.
cout<< "Pleae count the number of letters in the string you just entered ";
cout << endl << "and enter the number now: ";
cin >> size;
cout << endl << endl << endl;
}
//******************************************************************************
void output(bool const &palindrome)
{
cout << endl << "The string entered ";
if(palindrome==true)
{
cout << "is a palindrome." << endl;
}
else if(palindrome==false)
{
cout << "is not a palindrome." << endl;
}
else
{
cout << endl << endl << "An error has occured." << endl;
cout << "It was not possible to determin rather or not ";
cout << "the value entered was a palindrome or not.";
}
}
//******************************************************************************
The error is in line 83.
cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `char' in assignment
I don't know how I'm supposed to compare the two variables, before I tried the character approach I tried doing it with brackets with the individual elements in each string and that gave error messages too.
note: I read ahead in the book were using and search the internet and I know that there are specialized built in functions for working with strings but I don't think were supposed to use those yet.
Thanks for the help.