Enum with Maps, and Struct

The idea is to have the user choose from a menu of items and store them all in a struct and then print out the order at the end. Everything has worked fine except that with the sauces (which i have to use enums for them) i cant seem to figure out how to push them into the struct. I can print them fine, i just cant put them in the struct, atleast not in the same way i did with the other map. This is just part of the code and everything works fine except for the enum thing. Any help would be great! Maps are needed for this also.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 class MenuItem
{
public:
	struct Order
	{
    
	vector<string> iteamName;
	vector<int> totalQuantity;
	vector<double> totalPrice;

	};
	virtual double getPrice(int foodNum, int quantity) = 0;
	string toString() {
		return "Your Order";
	}
};
class Appetizer :
	public MenuItem	
{
	enum appSauce { RANCH = 1, CHEESE, BBQ, HONEY, SPICY, SWEET };
public:
	Appetizer() {}
	double getPrice(int foodNum, int quantity)
	{

		double price = 0;
		double menuNum[5]{ 0, 0.60, 0.70, 7.99, 6.99 };
		price = menuNum[foodNum] * quantity;
		return price;

	}
	void toString()
	{
		
		map<int, string> AppMenu;
		map<int, string>::iterator toppingIterator;
		AppMenu[1] = "Traditional Wings - $.60";
		AppMenu[2] = "Boneless Wings - $.70";
		AppMenu[3] = "Chessy Bread - $6.99";
		AppMenu[4] = "Garlic Bread - $7.99";
		

		for (toppingIterator = AppMenu.begin(); toppingIterator != AppMenu.end(); ++toppingIterator)
			{
				cout << toppingIterator->first << ": " << (*toppingIterator).second << endl;
			}
		string answer;
		cout << "Would you like Sauce?(y/n): ";
		cin >> answer;
		if (answer == "y")
		{
			int sauceNum;
			set<appSauce, string> wingSauce;
			map<appSauce, string> sauceNames;
			map<appSauce, string>::iterator sauceIterator;
			sauceNames[RANCH] = "Creamy Ranch";
			sauceNames[CHEESE] = "American Cheese";
			sauceNames[BBQ] = "BBQ Sauce";
			sauceNames[HONEY] = "Honey Mustard";
			sauceNames[SPICY] = "Spicy Asian Sauce";
			sauceNames[SWEET] = "Sweet Chili Sauce";

			for (sauceIterator = sauceNames.begin(); sauceIterator != sauceNames.end(); ++sauceIterator)
			{
				cout << sauceIterator->first << ": " << (*sauceIterator).second << endl;
			}
		}
	}
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Appetizer myApp;
	Pizza myPizza;
	Sandwich mySandwich;
	
	MenuItem::Order A, P, S;


	int foodNum = 0;
        int quantity = 0;
        myApp.toString();
			map<int, string> AppMenu;
			AppMenu[1] = "Traditional Wings - $.60";
			AppMenu[2] = "Boneless Wings - $.70";
			AppMenu[3] = "Chessy Bread - $6.99";
			AppMenu[4] = "Garlic Bread - $7.99";
		
				cout << "Enter (1-4) for your Appetizers:  " << endl;
				cin >> foodNum;
				cout << "Quantity:  " << endl;
				cin >> quantity;
				A.iteamName.push_back(AppMenu[foodNum]);
				A.totalQuantity.push_back(quantity);
				A.totalPrice.push_back(myApp.getPrice(foodNum, quantity));
			
			system("Pause");
			system("CLS");

                                cout << "Appetizers: " << endl;
				for (unsigned int i = 0; i < A.iteamName.size(); i++) {
					cout << "x" << A.totalQuantity[i] << " " << A.iteamName[i] << endl;
				}

for (int i = 0; i < A.totalPrice.size(); i++) { sum += A.totalPrice[i]; }
Last edited on
On line 53 (in class Appetizer) you are using set<..> wrongfully, but you don't even use wingSauce. Appart from that I can't see what your problems are?
On line 53 i did exactly how the teacher showed us. I'll get to why that is there.
Your right i did not use wingSauce or any other part of the sauce code in the main program because i dont get how to it. Let me explain:
First i use line 10 myApp.toString() to print out the menu options
Then i have the user select the number that goes with that option and save it into foodNum
Then i push the results into the struct by doing A.iteamName.push_back(AppMenu[foodNum]);

I wanted to do the same thing but with the sauces. I was going to use wingSauce to hold the choices but i cant put that in the struct where it needs to be. I cant return a number to the
map<appSauce, string> sauceNames; like it did for the AppMenu. I need to keep the enum values in the class, so i cant place the map into the main program like i did with AppMenu starting at line 11.
Thats why i didnt implement anything for the sauces because im not sure how to push what the user chooses in map<appSauce, string> sauceNames; to a vector in the struct
Last edited on
OP: can you share the question in its entirety? My gut feel is that std::map might not even be required here but I might be wrong of course
The question is how do i push the value from the map sauceNames to a struct. Yes i do need to use maps and i need to use enums for the sauceNames map and its also required for me to use structs to store the order.
I want to be able to print out the options, which i can already do. When the user selects a number i would save that number to a variable and then A.iteamName.push_back(sauceNames[foodNum]); to push it into a vector in the struct.
My problem is that that code wont work because of the enum. Enums have to stay in the class with the map, so i cant move the map to the main program like i did in lines between 12 and 23.
my AppMenu map doesnt need to use enums. That was from part one of the project. So that is fine.
When i print out the options for sauce, When the user selects the number for which sauce they want how do i push that answer that corresponds to that chosen number to a vector in the struct.
As of now i dont have a separate vector for sauces but that would be the plan
Ok, i think i might have somewhat of a solution that may work out but im getting some errors.
// which none of these was a problem untill i added line 37-45 im main.cpp
Line 59: '<': signed/unsigned mismatch
C2064: term does not evaluate to a function taking 2 arguments
C2056: illegal expression

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <set>
#include <iomanip>
#include <map>
using namespace std;
enum appSauce { RANCH = 1, CHEESE, BBQ, HONEY, SPICY, SWEET };
class MenuItem
{
public:
	struct Order
	{
	vector<string> iteamName;
	vector<int> totalQuantity;
	vector<double> totalPrice;
	set<appSauce, string> wingSauce;
	};
	virtual double getPrice(int foodNum, int quantity) = 0;
	string toString() {
		return "Your Order";
	}
};

class Appetizer :
	public MenuItem	
{
	
public:
	Appetizer() {}
	double getPrice(int foodNum, int quantity)
	{

		double price = 0;
		double menuNum[5]{ 0, 0.60, 0.70, 7.99, 6.99 };
		price = menuNum[foodNum] * quantity;
		return price;

	}
	void toString()
	{
		
		map<int, string> AppMenu;
		map<int, string>::iterator toppingIterator;
		AppMenu[1] = "Traditional Wings - $.60";
		AppMenu[2] = "Boneless Wings - $.70";
		AppMenu[3] = "Chessy Bread - $6.99";
		AppMenu[4] = "Garlic Bread - $7.99";
		

		for (toppingIterator = AppMenu.begin(); toppingIterator != AppMenu.end(); ++toppingIterator)
			{
				cout << toppingIterator->first << ": " << (*toppingIterator).second << endl;
			}
		
	}
};
class Sauce :
	public Appetizer
{
	
public:
	
	Sauce() {}
	double getPrice(int foodNum, int quantity)
	{
		double price = 0;
		double menuNum[2]{ 0, .25 };
		price = menuNum[foodNum] * quantity;
		return price;
	}
	void toString()
	{
		
			map<appSauce, string> sauceNames;
			map<appSauce, string>::iterator sauceIterator;
			sauceNames[RANCH] = "Creamy Ranch";
			sauceNames[CHEESE] = "American Cheese";
			sauceNames[BBQ] = "BBQ Sauce";
			sauceNames[HONEY] = "Honey Mustard";
			sauceNames[SPICY] = "Spicy Asian Sauce";
			sauceNames[SWEET] = "Sweet Chili Sauce";

			for (sauceIterator = sauceNames.begin(); sauceIterator != sauceNames.end(); ++sauceIterator)
			{
				cout << sauceIterator->first << ": " << (*sauceIterator).second << endl;
			}

		
	}

};


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <numeric>
#include "stdafx.h"
#include "menu.h"

using namespace std;



int main()
{
	Appetizer myApp;
	Sauce mySauce;
	
	MenuItem::Order A,  W;
	
	int foodNum = 0;
	int quantity = 0;
        myApp.toString();
			map<int, string> AppMenu;
			AppMenu[1] = "Traditional Wings - $.60";
			AppMenu[2] = "Boneless Wings - $.70";
			AppMenu[3] = "Chessy Bread - $6.99";
			AppMenu[4] = "Garlic Bread - $7.99";
		
				cout << "Enter (1-4) for your Appetizers:  " << endl;
				cin >> foodNum;
				cout << "Quantity:  " << endl;
				cin >> quantity;
				A.iteamName.push_back(AppMenu[foodNum]);
				A.totalQuantity.push_back(quantity);
				A.totalPrice.push_back(myApp.getPrice(foodNum, quantity));
			
				mySauce.toString(); //print out the menu of sauces
					
				cout << "Enter (1-6) for your Sauces:  " << endl;
				cin >> foodNum;
				cout << "Quantity:  " << endl;
				cin >> quantity;
				W.wingSauce.insert(appSauce(foodNum));
				W.totalQuantity.push_back(quantity);
				W.totalPrice.push_back(mySauce.getPrice(foodNum, quantity));
				
			system("Pause");
			system("CLS");

                        double sum = 0;
			cout << "Your Order: \n" << endl;
			if (!(A.iteamName.size() == 0))
			{
				cout << "Appetizers: " << endl;
				for (unsigned int i = 0; i < A.iteamName.size(); i++) {
					cout << "x" << A.totalQuantity[i] << " " << A.iteamName[i] << endl;
				}
			}
                        for (int i = 0; i < A.totalPrice.size(); i++) { sum += A.totalPrice[i]; }
                        cout << "Your Order Total is $" << sum << endl;
			system("Pause");
			system("CLS");
}
Last edited on
set<appSauce, string> wingSauce;

This makes no sense. I know you said that this is exactly what someone else showed you; sure, I believe you. That doesn't change the fact that it makes no sense. What are you trying to do with this?
The way my teacher used it: When the user would select what they wanted from the menu, he would push that value into the set kinda like a vector. In the end he would iterate through the set and print out what was in it. I would use a vector like it did for the others but it wont let me. Im not really sure how to solve this
A set is a container for holding instances of some class.

When you create it, you state what kind of class that will be.

set<string> is for holding string objects
set<appSauce> is for holding appSauce objects

See the format?

set <the_kind_of_object_to_go_in_the_set>

So what kind of object do you want to be stored in the set? One kind of object. Just one. What kind of object do you want to store in the set?

Now that you know this, see how set<appSauce, string> makes no sense? If you wanted to store appSauce objects, you'd create a set<appSauce>. If you wanted to store string objects, you'd create a set<string>.

set<appSauce, string> just makes no sense, because appSauce, string isn't ONE kind of object.


---------


Note that a set will hold only ONE instance of each object. If you try to add something to a set that is the same as something that has already been added, you will NOT get two of them in the set. The set will still only contain ONE of them. So if you're intending to allow your user to pick more than one of some menu item, is a set really the right thing to use to hold their choices?

Here's an example of this:

1
2
3
4
set<int> someSet;  // create a set for holding int values
someSet.insert(3); // Now, the set contains a single number 3
someSet.insert(4); // Now, the set contains a single number 3 AND  a single number 4
someSet.insert(3); // Now, the set contains a single number 3 AND  a single number 4 - it does NOT contain two instances of the number 3 
Last edited on
This is appSauce
enum appSauce { RANCH = 1, CHEESE, BBQ, HONEY, SPICY, SWEET };

and i have this which holds all the strings that will be printed onto the screen. I have an iterator to print it out.
1
2
3
4
5
6
7
8
                        map<appSauce, string> sauceNames;
			map<appSauce, string>::iterator sauceIterator;
			sauceNames[RANCH] = "Creamy Ranch";
			sauceNames[CHEESE] = "American Cheese";
			sauceNames[BBQ] = "BBQ Sauce";
			sauceNames[HONEY] = "Honey Mustard";
			sauceNames[SPICY] = "Spicy Asian Sauce";
			sauceNames[SWEET] = "Sweet Chili Sauce";

what i need to do is have it printed out onto the screen have the user select which one or how ever many they want and then save that string in a struct. At the end print out everything in the struct. Also i see what you mean that the set wont hold two instances of the same thing, i have a separate vector to store the quantity of each item

If i didnt have to use enums i would be doing it the same way i did
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
map<int, string> AppMenu;
			AppMenu[1] = "Traditional Wings - $.60";
			AppMenu[2] = "Boneless Wings - $.70";
			AppMenu[3] = "Chessy Bread - $6.99";
			AppMenu[4] = "Garlic Bread - $7.99";
		
				cout << "Enter (1-4) for your Appetizers:  " << endl;
				cin >> foodNum;
				cout << "Quantity:  " << endl;
				cin >> quantity;
				A.iteamName.push_back(AppMenu[foodNum]);
				A.totalQuantity.push_back(quantity);
				A.totalPrice.push_back(myApp.getPrice(foodNum, quantity));
Last edited on
Also when i removed line 43 W.wingSauce.insert(appSauce(foodNum)); the program ran without the errors so the problem is defiantly here i just dont know how to fix it
You are creating a set named "wingSauce".

What kind of object do you want to store in that set?

I know I already asked you this question, but it seems like you didn't notice.


--------

appSauce(foodNum)
This appears to be a call to a function named appSauce. There is no such function. What are you trying to do with appSauce(foodNum)?
Last edited on
I want a string to be stored in the set. Sorry for not mentioning that
Last edited on
appSauce is the enum. It was the only thing that wouldnt give me a red line underneath the text. It was also the way my teacher taught it to us.

It kinda like the code below. Instead of it referencing the map, its going based off the enum. At least thats the way i understood it. It worked for him but my code doesnt like it so much lol
 
A.iteamName.push_back(AppMenu[foodNum]);
Last edited on
Here is how to make a set for storing strings:

set<string> chosen_sauce;

------------

1
2
3
4
5
cout << "What kind of sauce do you want (enter number 1 - 6)?" << '\n';
int choice;
cin >> choice;

chosen_sauce.insert(sauceNames[choice]);


The problem with this is that you are creating the useful map of enums to saucenames inside a function, so you don't have access to it anywhere else you need it. Your design is a mess (and the names of classes and what they are doing suggests you don't have a solid grasp of what your classes are meant to do). You need to stop bashing code based on what gets a red underlining and start doing real programming; real programming happens inside your head or on paper or whatever helps you think.
Last edited on
OP: i'd asked you once already to share the assignment in full, instead you keep posting snippets of code hoping somehow this will get you the answer. Well if that's the way you want to play its your prerogative of course but it just seems inefficient
@gunnerfunner yes you said "can you share the question in its entirety?" i did not realize that meant you wanted the whole entire assignment sorry for that. I thought you wanted me to be more clear on the question that i was trying to ask for this snippet of code. Because my problem wasn't the assignment as a whole but more about trying to find some direction on this one little issue. so i posted an example of the code so you would know what i was talking about. Then i posted again because i edited it and i got an error that i didnt know what it meant.
I sorry if i caused any trouble and the reason i started another thread about the error was because i needed to find a solution fast and i didnt know what that error meant. I didnt hear from anyone for a little bit, so i thought it wouldnt hurt
Sorry again if i cause any trouble and or confusion.
no worries. I see green ticks all around, glad you got there in the end. good luck!
Topic archived. No new replies allowed.