473,326 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why isn't this working! (palFind)

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;
}

Oct 21 '07 #1
4 1511
On Oct 21, 5:08 pm, flyandr...@gmail.com wrote:
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.

Oct 21 '07 #2
fl********@gmail.com wrote:
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
:-)
Oct 21 '07 #3
fl********@gmail.com wrote:
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.
}
else if (palindrome == false)
{
printf("Not a Palindrome! :(\n\n");
fflush (stdin);
}
}
fflush (stdin);
scanf("*");

return 0;
}
Oct 21 '07 #4
On Oct 21, 10:38 am, Barry <dhb2...@gmail.comwrote:
flyandr...@gmail.com wrote:
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++.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sebastian Aguilera | last post by:
Hi everyone. I have some troubles using the unserialize() function. I have serialized an array and that works perfect: echo '<input type="hidden" name="vardekedja" value="'.serialize($_POST...
1
by: Sunshine | last post by:
Pretty new to javascript so please help. Why isn't this working in my validateform()? if (document.This.strOld.value.compareTo(Session("strPassword")) != 0) { alert("Current password...
29
by: interpim | last post by:
Just a quick exercise from my C programming book, that I can't figure out why it isn't working properly. It kinda works but im getting wierd output. It is supposed to remove the vowels from text. ...
1
by: Mark Heimonen | last post by:
Hi, I've been using a standard global error handling page for almost two years. I posted a new revision of code on the site on Friday, and my global error handling routine quit working. The...
63
by: IAmIronMan | last post by:
This individual is stealing other peoples code and claiming it as his own. The proof is when I asked him to explain his code he would not. For proof view these threads in dotnet.languages.vb ...
10
by: sp | last post by:
The application is written in Visual Basic / .NET and working without problems under Windows XP, Windows 2000, Windows 2003 but it isn't working under Windows ME and Windows 98 - the computer...
2
by: PrinceMhul | last post by:
var objListBox = document.getElementById('catalist'); var val = objListBox.value; var txt = dyevalue00.value; document.getElementById(val).value = txt; To explain real quick, this is for a...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
7
by: nassausky | last post by:
I acquired a very basic redirect script from: http://www.minisitegallery.com/blog/php-javascript-countdown-script-with-timezone-setting.html which is supposed to count down to a specified date and...
12
by: Hendor | last post by:
Hi all. I've recently set up Apache 2.2 with PHP 5.2 and MySQL 5.1. I played around with SQL a bit, and now I'm trying to access it with PHP. I currently have the code: <?php #...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.