Comp Sci C++ identifying a palindrome (output error)

  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    C++ Error
AI Thread Summary
The discussion revolves around a C++ program intended to check if a five-digit integer is a palindrome. The user encountered an output issue due to a missing operator in the second `cout` statement, specifically a missing `<<` before `endl`. After correcting this syntax error, the program should function properly. Another user suggested an alternative approach using a different function to check for palindromes. The conversation highlights the importance of syntax accuracy in C++ programming for successful code execution.
mr.me
Messages
49
Reaction score
0
I had a homework assignment that wanted me to write a function that would determine whether a five digit integer was a palindrome or not

When I run the program I enter the palindrome there is no output for my if/else statement. What did I do wrong? Code Below

Code:
#include <iostream>
using std::endl;
using std::cout;
using std::cin;

main ()

{

int number; 
int a,b,c,d,e;

cout<< "I will take a digit five digit interger and indentify whether or not it is palindrome...";
cout<< "Please enter the number you want to check!\n"endl;
cin>> number;    // a = (number % 10);      
b = (number % 100) / 10;
c = (number % 1000) / 100;
d = (number % 10000) / 1000;
e = (number % 100000) / 10000; //Compare a to e and b to d. You don't need c

if (a==e && b==d)
    cout<<"The number is a palindrome!\n";
else 
    cout<< "The number is not a palindrome!\n";

return 0;

}
 
Last edited:
Physics news on Phys.org
If I try to compile that I get an obvious error in the second cout statement. If you fix that, I think it will work fine.
 
With syntax highlighting (testing 1..2..3):
Code:
#include <iostream>
[/color][b]using[/b][/color] std::[/color]endl;
[b]using[/b][/color] std::[/color]cout;
[b]using[/b][/color] std::[/color]cin;

main ()

{

int[/color] number; 
int[/color] a,b,c,d,e;

cout<<[/color] "I will take a digit five digit interger and indentify whether or not it is palindrome..."[/color];
cout<<[/color] "Please enter the number you want to check![/color][b]\n[/b][/color]"[/color]endl; // << before endl here!
cin>>[/color] number;    [i]// 
[/i][/color]

a =[/color] (number %[/color] 10[/color]);      
b =[/color] (number %[/color] 100[/color]) /[/color] 10[/color];
c =[/color] (number %[/color] 1000[/color]) /[/color] 100[/color];
d =[/color] (number %[/color] 10000[/color]) /[/color] 1000[/color];
e =[/color] (number %[/color] 100000[/color]) /[/color] 10000[/color]; [i]//Compare a to e and b to d. You don't need c
[/i][/color]
[b]if[/b][/color] (a==[/color]e &&[/color] b==[/color]d)
    cout<<[/color]"The number is a palindrome![/color][b]\n[/b][/color]"[/color];
[b]else[/b][/color] 
    cout<<[/color] "The number is not a palindrome![/color][b]\n[/b][/color]"[/color];

[b]return[/b][/color] 0[/color];

}
 
Last edited:
Code:
#include <iostream>
using namespace std;
 
int isp(int j){
        int p;
        p=j%10; j /= 10;
        p=p*10 + j%10;
        return p == j/100; }
                
int main(void) {
        for(int i=10000; i<=99999; i++ ) 
                if( isp(i) ) 
                        printf("%5d\n", i); 
        return 0;}
 
Thanks

I never got an error message and couldn't get it to run proper so it tried using a different compiler
and fixed the error .
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
3K
Replies
3
Views
2K
Replies
2
Views
2K
Replies
8
Views
1K
Replies
7
Views
2K
Back
Top