How to counter invalid input for char. Let say i have a menu which allow user to key in and cin only accepts char. How can i make sure that user to more key in a string etc.
Jason123 0 Light Poster
Recommended Answers
Jump to PostIf the user keys in more than just a single character you can flush the input keyboard buffer of all remaining keystrokes. See Narue's article here about how to do that.
Jump to Postchar ch; while ( cin.get ( ch ) && ch != '\n' ) { if( isalpha(ch) ) { //do something } }
Jump to PostYou're getting char with
cin.get(ch)
inside while function. You don't needcin>>ch;
before (it steals one char from you)
All 11 Replies
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Jason123 0 Light Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Jason123 0 Light Poster
Sci@phy 97 Posting Whiz in Training
Jason123 0 Light Poster
Sci@phy 97 Posting Whiz in Training
sri phani 0 Newbie Poster
Jason123 0 Light Poster
Sci@phy 97 Posting Whiz in Training
Jason123 0 Light Poster
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.