Hello, I need to print a menu, displaying the available choices as characters, no problem. I need to store that character into a variable, then check to make sure that character isn't Q or q, no problem. However, if the user enters anything other than a, b, c, d, or q (lower or uppercase is fine), then I need to prompt them to enter another character.

Whats the ideal way to go about this? I don't want to clutter up main, so I thought it would be best to make a char returning function. I tried a few if statements, but then in the end I still need to return something outside of the if statements.

>Whats the ideal way to go about this?
Well, you're right -- you should make a separate function. But instead of focusing on just getting the input, perhaps you could do a bit more: call the functions which are actually going to execute what the user wants. The function would no longer need to return what the user entered, because the function itself would be handling everything after the menus, and thus main() would have no need for it.

>I tried a few if statements
Try a 'switch' statement. Much easier.

>Whats the ideal way to go about this?
Well, you're right -- you should make a separate function. But instead of focusing on just getting the input, perhaps you could do a bit more: call the functions which are actually going to execute what the user wants. The function would no longer need to return what the user entered, because the function itself would be handling everything after the menus, and thus main() would have no need for it.

Except in an ideal situation (my opinion of ideal at least):
1) main() calls menu function which returns the menu entry
2) Switch statement (as suggested) will call functions to process each menu selection -- one function per menu selection

Thanks for the replies, I suppose I should have worded my question better. I'm not having any issues towards calling the appropriate functions (switch is being used). Rather, my problem is verifying that the input the user enters is a legal choice. For example: the menu is printed, and there are 4 options for the user to choose: a, b, c, and q. If the user enters anything other than these 4 (upper and lowercase allowed), then I prompt them to enter a valid character.

I imagine a while loop is needed, if they enter an invalid character I have to keep prompting them to enter a correct character. I was trying something along the following lines:

if( selection == 'a' || selection == 'A' )
{
      return(selection);
}

the function returns the type char. I always get a warning saying something along the lines of end of control not reached. This is so simple when using numbers, like making a choice between 1 and 500, or making sure it's a positive value, but the character is really throwing me off.

You'd have to show us the rest of the code that was causing the errors.

Just to simplify things a bit, don't you think you should be doing a toupper on the selection before processing it?

You'd have to show us the rest of the code that was causing the errors.

Just to simplify things a bit, don't you think you should be doing a toupper on the selection before processing it?

... or use a switch statement instead of nested if s? It could be cleaner.

... or use a switch statement instead of nested if s? It could be cleaner.

... which is what I said in my first post.

Yeah, and I referenced your suggestion in my first post.

Hello, I need to print a menu, displaying the available choices as characters, no problem. I need to store that character into a variable, then check to make sure that character isn't Q or q, no problem. However, if the user enters anything other than a, b, c, d, or q (lower or uppercase is fine), then I need to prompt them to enter another character.

Whats the ideal way to go about this? I don't want to clutter up main, so I thought it would be best to make a char returning function. I tried a few if statements, but then in the end I still need to return something outside of the if statements.

in my opinion you don't need to make a separate function for it you can easily do it by a do-while loop. just enclose the switch statement in the do while loop.and prompt invalid choice in the default case.the loop will keep on running unless the user enters a valid choice.(use this condition in the test statement of while).

I just coded a similar menu...I encoded the quit option to set a boolean variable and enclosed the whole switch in a do-while loop...when quit was selected it set the boolean variable and dropped out of the loop...When an invalid option was enter the default switch displayed a message saying invalid selection and that was it...the loop ran again and the user was again given the same menu...

I just coded a similar menu...I encoded the quit option to set a boolean variable and enclosed the whole switch in a do-while loop...when quit was selected it set the boolean variable and dropped out of the loop...When an invalid option was enter the default switch displayed a message saying invalid selection and that was it...the loop ran again and the user was again given the same menu...

then what else is required.throw the menu out of the loop if you don't want to display it again and again.

I should have said that the similar code I constructed was a case-switch menu...I looks to me like that would be what you would want to go with for this...If you don't want to display the entire menu every single time, then just print it to the screen before you start your loop...in most cases, you will need a loop, if you want to continue choosing options from the menu...you could use a confusing if-else method, but for bigger menus, this is usually not the best option...For menus I definitely recommend the case-switch method...Have you studied case-switch operations?
EDIT: If you insist on the if-else method, enclose it in a do-while statement that is controlled by a bool "quit" value that is set when the user selects the quit option...

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.