Search code examples
c++overloadingoperator-keywordpalindrome

C++ Palindrome algorithm - No match for 'operator<'


My below C++ Palindrome agorithm implementation results in the following error:

No match for 'operator<' (operand types are 'std::basic_ostream< char >' and '< unresolved overloaded function type >')

The error happens on the ending else line.

#include <iostream>
using namespace std;

int main()
{
    cout << "Write a number to check if its palindrome" << endl;
    int a;
    cin >> a;
    int z = a;
    int b;
    int c;
    while (a > 0) {
        b = a % 10;
        c = (c * 10) + b;
        a = a / 10;
    }
    if (z == c) { cout << "This is a palindrome" << endl; }
    else { cout << "This is not a palindrome" < endl; }
    return 0;
}

Solution

  • I got the above error

    The last

    <endl;
    

    Must be

    <<endl;
    

    Be careful reading error messages. They give line number and character position in the line where error occurred.

    I could have sworn I looked at the code 10 times.

    If you would format your code perfectly, you would find the error quite quickly. For example

    #include <iostream>
    using namespace std;
    
    int main() {
      cout << "Write a number to check if its palindrome" << endl;
      int a;
      cin >> a;
      int z = a;
      int b;
      int c;
      while (a > 0) {
        b = a % 10;
        c = (c * 10) + b;
        a = a / 10;
      }
      if (z == c) {
        cout << "This is a palindrome" << endl;
      } else {
        cout << "This is not a palindrome" < endl;
      }
      return 0;
    }
    

    why it shows me that the number is not a palindrome no matter what I put in it would be useful

    You do not initialize c with zero before the loop.