I'm working on a menu based program. The actions performed are based on an integer that the user inputs via cin. In order to validate the integer I check cin.fail() and I also check to make sure that it is within range. How do I deal with the user simply pressing <Enter> when prompted to make a selection? I want to be able to display the appropriate error message and prompt them to try again.
I would recommend using getline(), then taking the string and using a stringstream to convert it to an integer. I wouldn't consider that any lower level than the extraction operators, though.
//1 The type
typedefvoid(YourInputActionClass::*FuncPointer)(void);
/** Declare a type which is a function pointer
* to the class which handles the input actions.
* The "void" return and parameter can be changed to
* anything you like. However all functions in the map should
* have the same return value and parameters.
**/
//2 The map looks like this
std::map<std::string, Func> map_of_function_pointers;
/** the string in this chase is the identifier for your function,
* you can change this to and int or any other primitive type.
* It depends on the user input
**/
//3 Initialize
map_of_fuction_pointers["input_indentifier"] = &YourInputActionClass::name_of_function;
//4 How to use:
*(map_of_function_pointers[choice])( void )
/** Second paratense includes the function parameters which you delclared in (1)
* This is would be equivalent to running name_of_function(void)
**/
Might be difficult to understand at first but is very useful to know =)
im not sure what diference it makes if its standard or not, so i would appreciate i u could explain it to me.
and using getch(), the input would not be displayed on the screen, so i think thats a plus
the response would be the screen changing into the menu he selected by the input, or an error message if he did wrong input, specificly in jokermans example.
and thx for explaining why use standard over non-standard
Believe it or not, I have a significant amount of experience in UI design, and simple console menu systems really aren't over my head. The program's response != the user's response.
When people see a prompt like
Please enter a number: _
they really do kind of expect to see what they are typing appear on the screen. This is consistent with the behavior of the rest of the program, and with all other programs in its class. (That colon and blinking cursor are huge cues here.)
A program behaves properly when it behaves the way the user expects it to behave.
Yes, you are getting closer. When you say "Enter" you are telling the user that he is expected to type things, which he will see, and then press ENTER.
Better yet is something like:
┌─────────────────────┐
│ RANDOM MENU │
├─────────────────────┤
│ 1. Get Some Value │
│ 2. Input Some Value │
│ 3. Show Statistics │
│ 9. Exit │
└─────────────────────┘
Notice the lack of a cursor or a textual prompt. This strongly indicates that the user only need type the number indicated and that the computer will instantly respond to it.