Palindrome program compile error

Here is the code which I wrote in Turbo C++...its giving errors..

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
voidmain()
{
clrscr();
int i,j,len,flag,char name[25];
cout<<"Enter a string\n";
gets (name);
for(i=0; name[i]!='\0';i++);
len=1;
for(i=0,j=len-1;i<len/2;i++,j--)
{
if (name[i]==name[j])
flag=1,
else
{
flag=0;
break;
}
}e
if(flag==1)
cout<<"It is palindrome\n";
else
cout<<"It is not a palindrome";
getch();

}
voidmain()

this should be 'int main()' (note the space between int and main)

this:
#include<iostream.h>
should be
#include<iostream>

and you should be returning 0 at the end of the main function.

that's the first thing you should fix.


Also use code tags in your post so we can read it.

edit:

in fact there's a lot of errors, and the compiler should tell you where they are.
Last edited on
mutexe wrote:
and you should be returning 0 at the end of the main function.
False:
http://stackoverflow.com/a/204483/1959975
It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. This is also true with a C99 program. Whether return 0 should be omitted or not is open to debate.
The main function is a special function (in fact it is not even really a function) and so it gets special treatment.
Last edited on
No change still..
It say "Declaration terminated incorrectly" at char name[25]
undefined symbol 'name'
open to debate, so i wouldn't say it's false as such.

i do it for completeness I guess.
mutexe wrote:
open to debate, so i wouldn't say it's false as such.
Whether you should do it is open to debate. Whether it is safe to is not.
so it's not safe to return 0?

this isn't really helping the OP.
Last edited on
Let me clarify:

Whether or not you should always have the return statement in main is open to debate - some say you should, some say you shouldn't care. It's entirely based on opinion and since it is so trivial and specialized nobody can tell for sure which is better practice.

Whether or not it is safe to omit the return statement is known - it is safe to leave it out, and it is safe to have it in. There is no debate here.

I did not intend to create such confusion by simply pointing out that for main only, the return statement is optional. Sorry.
Last edited on
I edited and tried this way..Now working perfect..!!
Thanks guys for the help :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream.h>
#include<conio.h>
#include <string.h>

int main()
{
char str[100];
int flag;
cout<<"Enter a string\n";
cin>>str;
int x=strlen(str)-1;
for(int i=0;i<=x;i++)
{
if(str[i]==str[x-i])
{
flag=1;
continue;
}
else
{
flag=2;
break;
}
}
if(flag==1)
cout<<"It is a Palindrome\n"<<endl;
else
cout<<"It is not a palindrome"<<endl;
getch();
}
Topic archived. No new replies allowed.