0

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".

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Santino
  • 23
  • 5
  • https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings – Retired Ninja Nov 12 '20 at 23:47
  • 1
    I recommend creating a structure that contains the prompting text, the expected response, and a pointer to a function that handles the selection. Create an array of these. Use a loop to display all the prompts. Read the selection. search the array for the struct with a matching response. If the response matches the selection, then dereference the associated function pointer. – Thomas Matthews Nov 12 '20 at 23:48
  • @ThomasMatthews I appreciate the response, but I don't 100% understand what you're trying to get me to do here, I'm sorry haha. Like I said, I'm new to C++ for this example, How would i create an array/loop in the context of my sandwich problem? – Santino Nov 12 '20 at 23:55
  • ***Is there a way for me to make the user input a word instead of a number for a switch statement?*** No. switch() case does not work with strings. – drescherjm Nov 12 '20 at 23:59
  • Closest you get is something like `std::mapmenumap = {{"Wheat", 0}, {"Italian", 1}};` and then `switch(menumap[userchoice])` And this has a number of downsides you need to sort out. Somewhat better is `std::map>menumap = {{"Wheat", do_wheat}, {"Italian", do_Italian}};` and then `menumap[userchoice]();` to get rid of the `switch` entirely. And that has some nasty problems as well. By the time you've sorted them all out, you're back at structure and list as pitched by @ThomasMatthews . If you don't mind that it's slow, the map is easy to write – user4581301 Nov 13 '20 at 00:06
  • @user4581301 so how would I go about writing what Thomas said? I'm having a hard time understanding what he meant unfortunately :( – Santino Nov 13 '20 at 00:17
  • 1
    [Here is a link to something similar](https://stackoverflow.com/a/37580792/4581301). It's a bout 3/4 of the way to what Thomas is talking about, but still uses a `map` instead of a loop. `map` is slow unless you have a reasonably large number of items in the menu, and if you have enough items in the men to make it worth it, your menu is probably too crowded. – user4581301 Nov 13 '20 at 00:24
  • Looking over that code, I wrote it a while ago and would probably use [lambda expressions](https://en.cppreference.com/w/cpp/language/lambda) instead of the more trivial functions and the call to `std::bind`. I Hadn't really gotten deep into modern C++ at the time. – user4581301 Nov 13 '20 at 00:26
  • [Here's a link to a smarter, but less well-explained, version](https://stackoverflow.com/a/57882832/4581301) from last year using a more modern ideology. – user4581301 Nov 13 '20 at 00:31
  • @user4581301 I'm looking at my problem right now, and I'm realizing I can actually solve the code chronologically, as long as I can assign a variable to each item. I don't know if this changes anything. In the context of my actual code, choosing one of the options will just impact the final output number (user inputs n, then the first switch statement lets the user pick n-2 or n+2. the next switch block lets the user pick an option that associates with n/2 or n*2, so chronologically, the code would just find the first n, then take it and solve the next n. (as far as I know at least) – Santino Nov 13 '20 at 00:42
  • Does this change the way I would assign variables to different options? I didnt want to, but heres a link to my actual problem: [link](https://docs.google.com/document/d/1liF0cqZan32D5uSZFl07IvvDBkjzlbAUQVgYS7DZDDs/edit?usp=sharing) **actual problem** – Santino Nov 13 '20 at 00:42
  • 1
    my newest idea is to just output (in the context of my actual problem) a switch that assigns the numbers 1-12 to the various months, and then at the end of the problem, have an if/else if block that says like "if month==1, cout<<"in January, " and so on for each independent variable, building a part of the sentence with each block. – Santino Nov 13 '20 at 01:04
  • For that an enum could be used and a switch() – drescherjm Nov 13 '20 at 01:08
  • You can use a `switch` for that, but if you have a series of `if`s and `else if`s converting from string to number for the `switch`, Consider skipping the `switch` and doing the work the `switch` would do in the `if`s. To avoid code duplication, make a function that you can call for December, January, etc.... – user4581301 Nov 13 '20 at 01:58
  • @user4581301 I think i have it all worked out, thank you so much for your help!! – Santino Nov 13 '20 at 02:48

0 Answers0