January 18th, 2013, 01:58 PM
Using one of the solutions I suggested, this will do what I think you are trying to do far more succinctly:
if (NUM < 300 )
std::cout << "Num is less than 300" << std::endl;
// Pad digits to multiple of three
int digits = intDigits( NUM ) ;
int width = digits + (3 - digits % 3) ;
cout << setfill ('0') << setw(width) << NUM ;
On another note, it is not very clear how you want the user to enter a value, and your validation is minimal. Date formats are cultural and locale specific; the date 18 January 2013 for example might be expressed:
180113 // usual UK order ddmmyy
011813 // usual US order mmddyy
18012013 // UK ddmmyyyy
01182013 // US ddmmyyyy
20130118 // ISO 8601 order yyyymmdd
Given that a user might respond to your prompt in any of the above ways, and possibly more, it is not at all clear what you expect.
Last edited by clifford : January 18th, 2013 at 02:31 PM.