Thread: c++

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

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

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    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
    Last edited by JacobN; 10-26-2010 at 03:36 PM.

Popular pages Recent additions subscribe to a feed