-
c++
i have 1 error and i cannot fix it ,here is my code
Code:
#include<iostream>
#include<string>
using namespace std;
int main()
{
char strn[10];
cout<<"Enter the string:"<<endl;
cin.getline(strn,10);
int len=strlen(strn);
bool answer=true;
for(int i=0;i=len/2;i++)
{
if(answer=strn)
{
answer=true;
cout<<"Palindrome"<<endl;
}
}
else
{
answer=false;
cout<<"Not Palindrome"<<endl;
}
cin.get();
return 0;
}
-
With proper indentation the first error is easy to identify:
Code:
#include<iostream>
#include<string>
using namespace std;
int main() {
char strn[10];
cout<<"Enter the string:"<<endl;
cin.getline( strn, 10 );
int len = strlen( strn );
bool answer=true;
for( int i=0; i = len / 2; i++ ) {
if( answer = strn ) {
answer = true;
cout<<"Palindrome"<<endl;
}
}
else {
answer = false;
cout<<"Not Palindrome"<<endl;
}
cin.get();
return 0;
}
You should be getting an error along the lines of "illegal else without matching if", depending on your compiler. Also, using a few spaces here and there isn't illegal, can make code easier to read.
Additionally you have assignment within a conditional expression, and the logic of your test is flawed. Think about what the definition of a palindrome is