I want to know how I can make a menu so people can choose what they want to buy. I know how to make a menu with one choice, but how do you make a menu where multiple choices can be picked at one time ?
How do you select multiple cases at once ? For example, if someone wanted to select meat, vegetables and dairy, how would you set up that condition so they could continue entering information ?
It would be easier to handle one input at a time, and ask if the user wants to buy anything else when he's done entering other data.
But if you are determined to ask for everything at once I can think of two possibilities
1) Using a bitmask. This is a bit long to explain and not exactly at beginner level (and counter-intuitive for the user), but you can research it if you want.
2) Using a string. For example, let's say that the user wants to buy meat and fruit. He writes meat vegetables dairy and you compare each word to every choice. Something like
1 2 3
std::cin >> word; // word is a std::string
if(word == "meat")
// do your stuff
Or you can use single letters, like m, v, d, f (the input becomes mvf) and compare one char at a time
@maeriden
Switch checks don't only work for integers... It'll work for char (which technically are integers but read differently), bool, and string.
@RadCod3Win
Switches are tests statements like the if statement... They're not containers to hold information like strings which hold a series of character pointers.
Below is the simplest way to use switches
For your code you could have a string array or vector which stores all the entered selections as maeriden said and a switch statement to make your code efficient and clear for checking what it is... I'd suggest creating separate functions to list each category, a little like this...
Do you have to use all of this code enable to write the menu ?
1 2 3 4 5 6
string str;
vector<string> choices;
choices.push_back("");//At least one statement will be received.
getline(cin, str);
for(int i=0, int j=0; i<string.length(); i++){//To separate the entered categories into sub-strings
if(str[i] != ' ')
If so, I have never been taught this. What does that all mean ??
You didn't show us how far you've gotten with you own code, So I'm not sure exactly how much explaining you need, so I've made comments in the code you were asking about above.
If you don't know what an array or a class object is yet then vectors are going to come later.
1 2 3 4 5 6 7 8 9 10 11 12
string str; //create an empty string, strings are available when you #include <string>
vector<string> choices; //Create a vector-object of string-type arrayed variables named "choices" (must #include <vector>)
choices.push_back("");//At least one statement will be received. - vectors are defined in a class,
// so "choices" is your object and "push_back()" is a function that is available to that object.
// The . in between lets your program know that you're using a function from the object's class
//push_back() adds memory to the vector-object to allow for a new variable in the "choices" array.
getline(cin, str);//common use of getline to retrieve input
//The next line is a for-loop, they create a loop of code that runs over and over until a condition is met.
for(int i=0, int j=0; i<string.length(); i++){//To separate the entered categories into sub-strings
if(str[i] != ' ')
If you don't understand any of what this code does then you should first learn about while loops and for loops, play around with those for a bit, then learn arrays then how to make classes, then move into vectors.
In reality you can use vectors without knowing about classes, but knowing about them will help you understand why you must do what you do to make vectors work.
Switch works on Boolean logic, similar to standard if statements but strictly == or !=, it can't compare greater or less than, but it should be able to return true or false for any class provided that it as a defined == operator (which everything in the standard library should have) http://www.cplusplus.com/doc/tutorial/control/
(Switch is right at bottom)
I'll check when I get on my laptop (bout 11:00 today) but while we on strings have you ever had a problem saying something about string not naming a type and I can't fix it, it has been on this forum many times but caused by different things, I can't find why my error is here or how to fix it.
EDIT: hm, turns out this doesn't compile, and I do receive an error stating that the tested statement in the switch condition is not an integer value.
Sorry about previous posts, I don't know what I will have confused this with but it'll be something XD
Using a switch with chars as the options (make sure you include a quit option) is a good idea for this problem. You can put the whole thing in a while loop like this: