I am making a command line program that finds factors of a number that the user enters. At the moment the user could enter something like this: '768r7fg38gf8320rf' which returns an error. I would like to restrict the user so they can only enter numbers (0 - 9). How would I do this?
_Michael_
Recommended Answers
Jump to PostWithout knowing precise details of your environment, it's very hard to say how you would do that.
But it's far simpler just to let the user enter what they like, then validate it.
Jump to PostJust do something like this :
string num = ""; cin >> num; while( !isNumber( num ) ){ cout << "Only numbers allowed, Try again : " ; cin >> num; }
isNumber is your own function that uses the isdigits() function from cctype.
All 5 Replies
Salem
5,138
Posting Sage
_Michael_
mrnutty
761
Senior Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
WaltP
2,905
Posting Sage w/ dash of thyme
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.