Palindrom

So I'm writing this code that check if a five-digit number is a palindrom or not. But I get the error message 'Code will never be executed' on line 28. Please tell me what I'm doing wrong
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>   
using namespace std;

int main()
{
    char ch;                                                                
    
    do
    {
        int n, num, digit, rev=0;                                           
        
        while(true)
        {
            cout << "Enter a five digit number : ";                             
            cin >> num ;                                                        
        
            if( (num / 10000) == 0 )                                            
            {
                cout << "Not enough numbers" << endl;              
            }
        
            if( (num / 100000) > 0 )                                            
            {
                cout << "To many numbers" << endl;                                             
            }
        }
    
        n=num;                                                              
        
        while(num!=0)                                                   
        {                                                    
            digit=num%10;                                                   
            rev=rev*10+digit;                                             
            num=num/10;                                                   
        }
        
        if (n==rev)                                                         
        {
            cout << rev << endl;                                            
        }
        else                                                                
        {
            cout << "Not a palindrom" << endl;                              
        }
        
        cout << "Run to program again(y/n)? ";                                      
        do
        {
            cin >> ch;                                                      
            ch = toupper(ch);                                               
        }while( !(ch == 'Y' || ch == 'N'));                                
        
        
    }while(ch == 'Y');                                                    
    
    return 0; 
} 
Last edited on
In what situation does the loop (before line 28) stop?
If a write a 5-digit number the program starts over again. But I want it to continue. If a write a 3-digit number for example the program says, Not enough numbers and then starts over. if a write a 6-digit number the program says, To many numbers and then starts over.
closed account (E0p9LyTq)
Sloppy and quick, but works for a five digit number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

int main()
{
   unsigned number = 0;

   while (true)
   {
      std::cout << "Enter a five digit number: ";
      std::cin >> number;

      if (number > 9999 && number < 100000)
      {
         break;
      }
      std::cout << "Not the correct number of digits!\n\n";
   }
   
   std::cout << "\n";

   // create a string from the number
   std::string num_str = std::to_string(number);
   
   if ((num_str[0] == num_str[4]) && (num_str[1] == num_str[3]))
   {
      std::cout << "The five-digit number is a palindrome!\n";
   }
   else
   {
      std::cout << "The five-digit number is NOT a palindrome!\n";
   }
}

Enter a five digit number: 1234
Not the correct number of digits!

Enter a five digit number: 1223456
Not the correct number of digits!

Enter a five digit number: 12345

The five-digit number is NOT a palindrome!


Enter a five digit number: 22522

The five-digit number is a palindrome!
Topic archived. No new replies allowed.