Why isn't this working! (palFind)

F

flyandread

I have tried and tried and tried to this Palindrome Finder to work but
I just can't seem to fix it. There are no error messages but it just
keeps saying that everything is a palindrome. Here is the code. If you
would like to help, that would be appreciated.


#include <cstdio>
#include <cstring>

int main ()

{
printf ("---------------------------palFind: v0.4
ALPHA----------------------------------\n");
printf (" Press ctrl+Break to Quit\n\n");

while (true)
{
char name [500];
bool palindrome = true;

fgets (name, 500, stdin);

for (int x = x/2; x <= strlen(name)/2; ++x)
{
if (name [x - 1]= name[strlen(name - x)])
palindrome = true;

else
palindrome == false;
}
if (palindrome == true)
{
printf ("Palindrome! :)\n\n");
fflush (stdin);
}
else if (palindrome == false)
{
printf("Not a Palindrome! :(\n\n");
fflush (stdin);
}
}
fflush (stdin);
scanf("*");

return 0;
}
 
F

flyandread

I have tried and tried and tried to this Palindrome Finder to work but
I just can't seem to fix it. There are no error messages but it just
keeps saying that everything is a palindrome. Here is the code. If you
would like to help, that would be appreciated.

#include <cstdio>
#include <cstring>

int main ()

{
printf ("---------------------------palFind: v0.4
ALPHA----------------------------------\n");
printf (" Press ctrl+Break to Quit\n\n");

while (true)
{
char name [500];
bool palindrome = true;

fgets (name, 500, stdin);

for (int x = x/2; x <= strlen(name)/2; ++x)
{
if (name [x - 1]= name[strlen(name - x)])
palindrome = true;

else
palindrome == false;}

if (palindrome == true)
{
printf ("Palindrome! :)\n\n");
fflush (stdin);}

else if (palindrome == false)
{
printf("Not a Palindrome! :(\n\n");
fflush (stdin);}
}

fflush (stdin);
scanf("*");

return 0;

}

btw: there is a close brace at the end but it got put in quoted text.
 
B

Barry

I have tried and tried and tried to this Palindrome Finder to work but
I just can't seem to fix it. There are no error messages but it just
keeps saying that everything is a palindrome. Here is the code. If you
would like to help, that would be appreciated.


#include <cstdio>
#include <cstring>

int main ()

{
printf ("---------------------------palFind: v0.4
ALPHA----------------------------------\n");
printf (" Press ctrl+Break to Quit\n\n");

while (true)
{
char name [500];
bool palindrome = true;

fgets (name, 500, stdin);

for (int x = x/2; x <= strlen(name)/2; ++x)

how can you write
int x = x/2;
???

int x = 1;
{
if (name [x - 1]= name[strlen(name - x)])

name [x - 1] == name[strlen(name) - x]
palindrome = true;

else
palindrome == false;

=

You should try to rewrite the for loop, it can be much simpler, and
without calling /strlen/ every comparison.
if (palindrome == true)
{
printf ("Palindrome! :)\n\n");
fflush (stdin);
}
else if (palindrome == false)
{
printf("Not a Palindrome! :(\n\n");
fflush (stdin);
}
}
fflush (stdin);
scanf("*");

return 0;
}

Well, do your homework harder
:)
 
R

Rolf Magnus

I have tried and tried and tried to this Palindrome Finder to work but
I just can't seem to fix it. There are no error messages but it just
keeps saying that everything is a palindrome. Here is the code. If you
would like to help, that would be appreciated.


#include <cstdio>
#include <cstring>

int main ()

{
printf ("---------------------------palFind: v0.4
ALPHA----------------------------------\n");
printf (" Press ctrl+Break to Quit\n\n");

while (true)
{
char name [500];
bool palindrome = true;

fgets (name, 500, stdin);

for (int x = x/2; x <= strlen(name)/2; ++x)

Even though the above is syntactically possible, it will result in an
undefined start value for x. What do you expect its value to be after the
initialization?
{
if (name [x - 1]= name[strlen(name - x)])
palindrome = true;

else
palindrome == false;

This is a no-op. I guess you meant:

palindrome = false;

}
if (palindrome == true)
{
printf ("Palindrome! :)\n\n");
fflush (stdin);

Attempting to flush stdin results in undefined behavior.
 
J

James Kanze

I have tried and tried and tried to this Palindrome Finder to work but
I just can't seem to fix it. There are no error messages but it just
keeps saying that everything is a palindrome. Here is the code. If you
would like to help, that would be appreciated.
#include <cstdio>
#include <cstring>
int main ()
{
printf ("---------------------------palFind: v0.4
ALPHA----------------------------------\n");
printf (" Press ctrl+Break to Quit\n\n");
while (true)
{
char name [500];
bool palindrome = true;
fgets (name, 500, stdin);
for (int x = x/2; x <= strlen(name)/2; ++x)
how can you write
int x = x/2;
???

With a keyboard:)? Writing it's easy. And it should compile
as well (although a good compiler might give a warning). it
will just have undefined behavior when it's executed; the x
comes into scope *before* the initialization expression is
evaluated, so you are initializing a variable with a value based
on its uninitialized value.

Of course, for what he's doing, I'd probably just write
something like:

std::string line ;
while ( std::getline( std::cin, line ) ) {
std::string r( line ) ;
std::reverse( r.begin(), r.end() ) ;
std::cout << (line == r ? "is" : "is not") << " a palindrome
\n" ;
}

Life's a lot simpler in C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top