C++ identifying a palindrome (output error)

  • Comp Sci
  • Thread starter mr.me
  • Start date
  • Tags
    C++ Error
In summary, the conversation discusses a homework assignment to create a function that determines whether a five digit integer is a palindrome or not. The code provided has an error in the second cout statement, but once fixed, the program should work properly. The conversation also includes a different code that checks for palindromes using a different method.
  • #1
mr.me
49
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
  • #2
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.
 
  • #3
With syntax highlighting (testing 1..2..3):
Code:
[color=#BC7A00]#include <iostream>
[/color][color=#008000][b]using[/b][/color] std[color=#666666]::[/color]endl;
[color=#008000][b]using[/b][/color] std[color=#666666]::[/color]cout;
[color=#008000][b]using[/b][/color] std[color=#666666]::[/color]cin;

main ()

{

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

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

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

[color=#008000][b]return[/b][/color] [color=#666666]0[/color];

}
 
Last edited:
  • #4
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;}
 
  • #5
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 .
 

Related to C++ identifying a palindrome (output error)

1. What is a palindrome?

A palindrome is a word, phrase, or sequence that reads the same backwards as it does forwards. For example, "kayak" and "racecar" are both palindromes.

2. How can C++ identify a palindrome?

C++ can identify a palindrome by using a combination of string manipulation and conditional statements. The program would take in a word or phrase as input, reverse it, and then compare it to the original input. If they are the same, then the word or phrase is a palindrome.

3. What is an output error in relation to identifying a palindrome in C++?

An output error in this context refers to a situation where the program does not correctly identify a palindrome. This could be due to a mistake in the code, incorrect input, or a logical error in the program's logic.

4. How can I fix an output error when identifying a palindrome in C++?

If you encounter an output error when trying to identify a palindrome in C++, first check your code for any mistakes or logical errors. Make sure your input is correct and that your program is correctly comparing the reversed input to the original input. Debugging tools such as print statements can also be helpful in identifying and fixing any errors.

5. Are there any libraries or built-in functions in C++ that can help identify palindromes?

Yes, the reverse function in the algorithm library can be used to reverse a string, making it easier to compare to the original input. The string library also has a compare function that can be used to check if two strings are equal. However, these libraries are not necessary and a palindrome can be identified without them using basic string manipulation and conditional statements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
832
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top