I cant imagine I'm the first person to ask this, but I couldn't find an answer anywhere here.
I'm writing a menu, where I want the user to be able to choose one of three options, and then after that, choose one of another three options, and then maybe one of four options after this, or something of the sorts. Is there a way to set it up where the user can enter a word instead of a number associated with that word?
Easy real world ex- sandwich building: I want the user to choose one of three types of breads, one of three types of cheeses, one of four or five meats, and one of four or five dressings. How would I go about doing this?
I know I can make a switch
statement where, like I mentioned earlier, the options associate with a number, but what if I wanted the user instead to say "wheat" or "Italian" instead?
The reason I would do this would be so that at the end, I could have the code ask for each part of the sandwich individually, and then at the end say "you ordered a sandwich with wheat bread, American cheese, turkey, and mayo" (implying that Italian
maybe associates with the switch variable bread
or something), instead of having to make a switch block within a switch block within a switch block etc for each possible scenario.
I don't know if this makes sense, but I'm hoping it does enough for someone to help me. I'm pretty new to C++, and so I don't have much experience with replacing variables with names (I tried it once a few weeks ago, with no luck), and as a result I avoid nested switch
blocks.
I have a problem for a class I'm taking, that has a comparable issue with switch
blocks, but an entirely different scenario, but the sandwich thing seemed like an easy example to use that wouldn't just allow you guys to solve it for me.
EDIT: Another way to go about it would maybe be to do the switch
statements with numbers, like I know how to do, but then somehow associate the number with its option somehow as well? So, instead of having the user enter "Italian"
, they enter 1
, but then during the output line, it takes cout << "you ordered a sandwich with " << bread << " bread..."
and instead of saying "you ordered a sandwich with 1 bread", it would say "you ordered a sandwich with Italian bread".