It depends. It sounds to me like this is some type of part number with
different positions specifying different things. I once did this in a
retail system and realized when they ran out of space in the 2 byte int that
I should of use strings so they could use characters.
Anyway, it depends mostly becuase you said "if user enters..." which means
you are dealing with user input. I find the best way to deal with user
input is by characters, by strings. Once you get it in a string you can see
exactly what they entered.
That is, what if they etenered
071120921
when that gets put into an int it becomes
71120921
And now you have more of a headache, especially if you have different lenght
numbers they can enter. So, I would accept their input into a string.
Validate that all characters are numeric. Break up the characters into the
parts I want, load them in.
Untested pseudo type code:
int StrToInt( const std::string& Number )
{
std::stringstring Stream;
Stream << Number;
int Result;
Stream >> Result;
return Result;
}
std::string Number;
while ( std::cin >> Number && Number != "" )
{
if ( Number.length() != 9 )
{
std::cout << "Input must be 9 digits. Try again.\n";
break;
}
if ( ! isdigits( Number ) )
{
std::cout << "Only numbers 0-9 are allowed. Try again.\n";
break;
}
// at this point we know we have 9 numberic characters in Number
int j = StrToInt( Number.substr( 0, 2 ) );
int k = StrToInt( Number.substr( 2, 3 ) );
int l = StrToInt( Number.substr( 5, 4 ) );
// Do whatever with j, k and l here as integers
}
If you aren't dealing directly with user input but are receiving an int as
input, then I've just been blowing hot air
The general rule is: when dealing with user input treat it as
characters/strings as long as possible to validate it. That's the rule I
follow anyway.