My assignment in my class is to create a palindrome program for any given string. The main function is supposed to call an integer function named InputString that reads the characters of length n and it needs to determine if its a non-zero length. If this is so, another function named PalindromeTest is called to test if its a palindrome The returned value here is boolean. Then, from within PalindromeTest, I have to call another function, PrintMessage, upon return from PalindromeTest to display the result of the test.

I am open to any help or suggestions or criticism.

#include <iostream>
#include <cstring>

using namespace std;

#define false 0
#define true 1

char s[100];
char sr[100];

void InputString();
bool PalindromeTest();
void PrintMessage();

int main()
{
    InputString();
    
    system("PAUSE");
    return 0;
}

void InputString(int n=0)
{
     cout<<"Input the string you wish to be tested";
     cin.getline(s, 100);
     
     if(strlen(s)>n)
     PalindromeTest();
     
}

bool PalindromeTest()
{
     bool rvalue = true;
     
     strcpy(sr, s);
     strrev(sr);
     if(strcmp(s, sr)==0)
     {            
                  rvalue=true;
                  PrintMessage();
     }
     else
     {            rvalue=false;
                  PrintMessage();
     }
     
}


void PrintMessage(bool rvalue)
{
     if(true == rvalue)
             cout<<"The entered string IS a palindrome"<<endl;
     else
             cout<<"The entered strins IS NOT a palindrome"<<endl;
}

Recommended Answers

All 5 Replies

Have you tried to compile and run this? I can see an issue with your calls to PrintMessage(). The error I see usually would allow compilation, but cause the linker to fail.

You don't need the #defines. In C++ true and false are already reserved words.

First off, this code :

#define false 0
#define true 1

char s[100];
char sr[100];

Is bad already. You do not need to use define. C++ already has, "true" and "false" defined.

Next :

int main(){
    InputString();
    system("PAUSE");
    return 0;
}

No need to use a system command. Use cin.get(); Do not use global variables when local will do. Make your getInput() function return the input. Why are you using a char array. Use the string header.
Get into good habit early. I would suggest your main to look something like this :

int main(){
 string input = getInput();
 bool isPalin = isPalindrome(input);
 handleMessage(isPalin);
 return 0;
}

Next :

void InputString(int n=0)
{
     cout<<"Input the string you wish to be tested";
     cin.getline(s, 100);
     if(strlen(s)>n)
     PalindromeTest();
}

There is a lot of bad things that could happen. Instead of listing them all
I will tell you the solution. Use C++ strings. Do not use char arrays.
The whole point of this exercise is to get your logical thinking skills up.
So don't make use of the strrev function. Its a good idea to use the stl
for solutions, but thats not the point here. Create your own for loops
and apply some logic.

I hope that helped somewhat, and you take it into consideration.
Because what you got right now is not very good.

commented: Solid advice! +8

Have you tried to compile and run this? I can see an issue with your calls to PrintMessage(). The error I see usually would allow compilation, but cause the linker to fail.

You don't need the #defines. In C++ true and false are already reserved words.

I have tried to compile it and I do get linker errors with PrintMessage() and for InputString(). I am using the Dev-C++ compiler and the error log calls them undefined references.

I have tried to compile it and I do get linker errors with PrintMessage() and for InputString(). I am using the Dev-C++ compiler and the error log calls them undefined references.

check the signature of the function.

I have checked if your code works on compiler; but it says error..."strrev was not declared in this scope"
please check your code before you simply post it to your customers.

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.