Specify a fixed length of int

Hi
Im writing a program that check if a number is a palindrome or not. The problem I have is that I need to set a fixed length to the integer, i.e. 5. So when the user put in a number it most contain five numbers or the program will say "not five numbers". So for example if the user writes in 12345 the program will start, if the user writes 1234 the program will say "not five numbers".

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
  #include <iostream> 
using namespace std;

int main()
{
    int n, num, digit, rev=0;
    
    cout << "Write a number with 5 digits : ";
    cin >> num ;
    
    n=num;
    
    while(num!=0)
    {
        digit=num%10;
        rev=rev*10+digit;
        num=num/10;    
    }
    
    if (n==rev)
    {
        cout << Palindrome << endl;
    }
    else
    {
        cout << "Not a palindrome" << endl;
    }
}
Last edited on
1
2
3
4
5
6
7
8
9
if( (num / 10000) == 0 )
{
    // not enough digits...
}

if( (num / 100000) > 0 )
{
    // too many digits...
}


Or, you could read the number in as a std::string instead of an int and work with it that way.
Topic archived. No new replies allowed.