C++ identifying a palindrome (output error)

  • Context: Comp Sci 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    C++ Error
Click For Summary

Discussion Overview

The discussion revolves around a homework assignment involving a C++ function designed to determine if a five-digit integer is a palindrome. Participants are troubleshooting issues related to code execution and syntax errors.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant shares their code and describes the problem of receiving no output from the if/else statement.
  • Another participant identifies a syntax error in the second cout statement, suggesting that fixing it may resolve the issue.
  • A different code snippet is provided by another participant, which implements a function to check for palindromes using a different approach.
  • One participant mentions switching compilers to resolve the issue without receiving error messages, indicating a possible environment-related problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solve the problem, and multiple perspectives on troubleshooting and implementation are presented.

Contextual Notes

There are unresolved syntax issues in the original code, and the discussion includes different methods for checking palindromes, which may depend on varying interpretations of the problem.

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 count 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 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K