i have to write a program that checks a palindrome to see if it is in fact a palindrome but i get this warning
warning C4800: 'char *' : forcing value to bool 'true' or 'false' (performance warning)
and its not quite working

any help would be great thanks.

#include <iostream>
using namespace std;

const MAX=20;
void getSent(char array[MAX]);
bool palindrome(const char array[MAX]);

int main()
{	
		char array[MAX];
		cout << "Enter you palindrome: ";
		getSent(array);
		bool palindrome(array);
		if(palindrome == true)
		cout << "yes " << array << " is a palindrome";
		return 0;
}

void getSent(char array[MAX])
{
	char pal;
	int index = 0;
	
		cout << "Enter you palindrome: ";
		while ((pal = cin.get() != '\n') && (index < (MAX - 1)))
		{
			array[index] = pal;
			index ++;
		}
		array[index] = '\0';
		cin.ignore(1000,'\n');

}

bool palindrome(char array[MAX])
{
	int end, start,i = 0;
	while(array[i] != '\0')
	{
	array[i] = tolower( array[i] );
	i++;
	}
	while(array[i] != '\0')
	{
	if (!isalpha(array[i]))
	i--;
	}
	for(end = 0;array[end] != '\0';end ++);
	for (start = 0; start < end;)
	{
		if (array[start] != array [end])
		{
			return false;
		}
		start ++;
		end --;
	}
	return true;
}

Recommended Answers

All 8 Replies

This same problem appeared in an earlier post (within the last day)

while ((pal = cin.get() != '\n') && (index < (MAX - 1)))

is not doing quite what you want it to. Look for the previous explanation.

When quoting error messages, it helps to give the line the message points to. In this case, your error is the line starting bool:

bool palindrome(array);
		if(palindrome == true)
		        cout << "yes " << array << " is a palindrome";

What do you think is happening here? Is 'palindrome' a variable or a function?

hmm i can't seem to find the previous explanation.

palindrome is a function it returning a value im really not sure about this code at all.

i found your explanation thanks.

when i run the program it repeats the cout asking for a palindrome then always says it is a palindrome. the output is in very strange characters too.

also that error occurs where u said it did

You're displaying the prompt to ask for input both in main( ) and in the input function - pick one.

The odd characters are caused by the first problem I pointed out.

As to 'palindrome', you declare a function by that name, but the usage in main( ) looks like a declaration of a variable. If you mean to have a variable that stores the result returned by the palindrom( ), give it another name.

Though you don't really need to store its result, just call the function in the if( ) statement that follows.

ok i've fixed everything now im getting a linking error
error LNK2001: unresolved external symbol "bool __cdecl palindrome(char const * const)" (?palindrome@@YA_NQBD@Z)

#include <iostream>
using namespace std;

const MAX=20;
void getSent(char array[MAX]);
bool palindrome(const char array[MAX]);

int main()
{	
		char array[MAX];
		getSent(array);
		if(palindrome(array))
		{
			cout << "Yes, " << array << " is a palindrome.";
		}
		else
		{
			cout << "No, " << array << " is not a palindrome.";
		}
		return 0;
}

void getSent(char array[MAX])
{
	char pal;
	int index = 0;
	
		cout << "Enter you palindrome: ";
		while (((pal = cin.get()) != '\n') && (index < MAX - 1))
		{
			array[index] = pal; 
			index ++;
		}
	
		array[index-1] = '\0';	
			cout << array;
		cin.ignore(1000,'\n');
}

bool palindrome(char array[MAX])
{
	int end, start,i = 0;

	while(array[i] != '\0')
		{
			array[i] = tolower( array[i] );
			i++;
		}

	while(array[i] != '\0')
		{
			if (!isalpha(array[i]))
			i--;
		}

	for(end = 0;array[end] != '\0';end ++);

	end --;

	for (start = 0; start < end;)
	{

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

		start ++;
		end --;

	}

	return true;
}

im also trying to make it work even if there is a ',' or whitespace but it is not working.

The link error - look at your function prototype and your function definition.

What is the purpose of the second loop in palindrome( )?

I think that if you want to check, not counting whitespace or punctuation, you'd have to copy just the letters to a second string, which is what you then test.

i finished it last night thanks for your help though

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.