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.

If 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.

Any sample.. will this work??

char ch;

 while ( cin.get ( ch ) && ch != '\n' )
{
    //do something
}

if my char accepts 'A', 'B'but not digits? How do i go about ?

char ch;
 while ( cin.get ( ch ) && ch != '\n' )
{
    if( isalpha(ch) )
    {
         //do something
    }
}

I add a menu with option in char like 'a', 'b' etc in the if statement. If i type in abc as input case 'a', b will be called etc. Error will display about 2-3 times

How can i make it such that user can only type in char and no functions will be called?

char ch;
 do{      
       //show menu
       cout <<"a ..........
                   b ......... " ; <<endl;   
       cin >> ch;
     while ( cin.get ( ch ) && ch != '\n' )
    {
     if( isalpha(ch) )
     {  
        switch(ch)
        {
            case 'a'://function a
                 break;
            case 'b': //function b        
                 break;            
            case 'c':
                  break;
            default:                
                cout<<"Error!\n"<<endl;    
         }
     }
   }
}while(ch!='q');

You're getting char with cin.get(ch) inside while function. You don't need cin>>ch; before (it steals one char from you)

Edited.. still occured the error..as above

char ch;
 do{      
       //show menu
       cout <<"a ..........
                   b ......... " ; <<endl;   
     while ( cin.get ( ch ) && ch != '\n' )
    {
     if( isalpha(ch) )
     {  
        switch(ch)
        {
            case 'a'://function a
                 break;
            case 'b': //function b        
                 break;            
            case 'c':
                  break;
            default:                
                cout<<"Error!\n"<<endl;    
         }
     }
   }
}while(ch!='q');

You don't need two loops (do, and while). Try to implement just one. (think, think :) )

hai i seen ur doubt. after accepting the character write the condition that the size of accpeted char is equal to the size of char using sizeof operator.
write
if(sizeof(c)==sizeof(char));

You don't need two loops (do, and while). Try to implement just one. (think, think :) )

I need to prompt the menu if invalid option entered..if with only 1 loop. which loop do i remove?

do loop. And inside while add check ch != 'q'.

And you have to cleverly put menu printout somewhere inside :)

if i put it inside the while loop, upon compiling and run the program.. No menu is show.. Any tips?

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.