I'm new on this forum and I would not come here to ask for help if I'm not desperate. I just learn loop last week and my professor give me a project to create a menu for a math tutor program. The user will then select what do they want to do. The program will seed 2 random numbers from 200 to 999 if the user select addition or subtraction. If the user select division or multiplication, the second number will be seeded again from 0 to 10. Here's my code.
Your while is selecting on the basis of the numbers 1 and 5 and not the characters '1' and '5'. You also need to check for the other character input possibilities. Quite a long list of or's
Your while is selecting on the basis of the numbers 1 and 5 and not the characters '1' and '5'. You also need to check for the other character input possibilities. Quite a long list of or's
well what I want is if the user select A or a or 1 or +, the addition computation will happen. if it's anything else, the message error will show up. How do I do this ? Is the do-while loop a bad choice ? Should I use for loop instead ?
I understood what you are trying to do and there is nothing wrong in principle with giving many options for the same type of calculation. All it does is make a very long test for validating the input. Keep it simple and you don't have such a long list. But there's nothing wrong and there's probably no better way of doing it. You could save a little by using toupper() to get everything into uppercase before the test but that's marginal.
FWIW I would only have 1 option in each case - 1, 2, 3 or 4 but there you go.
There's also nothing wrong with using a while loop. It's better than lots of if's, else's etc.
:-)
use "default" to force the loop back to the menu. If the user has entered a valid choice it will never get to default, so it becomes the fallback option for any incorrect entries.
I follow PCrumley instruction and got rid of that problem. Now i can compile the code but it keep pop up
run-time check failure #3 the variable answer is being used without being initialized.