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.

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 ?

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');

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?

This article has been dead for over six months. Start a new discussion instead.