Building a Recipe

For the love of humanity, someone please look at this. This is the shell I have so far. I am trying to build a recipe, but I think I have already started off wrong and now I have confused myself. Under the struct for Ingredient, do I specifically list the ingredients and measurements? I am not sure how to push the steps into the vector. Here is what I am so far.

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



#include <iostream>
#include<string>
#include<vector>
using namespace std

struct Ingredient
{
    enum class Measurement {cup=0.5, tablespoons=3, cup=.25, cup=.50, cup=.50, apples=8};
    int number;
    Measurement type;
    string ingredient; 
};

struct Recipe;
{
    vector <ingredient> ingredientsList;
    vector <string> stepBysteInstructions;

};

Recipe applePie;


int main()
{
    applePie.theItems.push_back(ingredient{ 1,
    Measurement::count, "Unsalted Butter" });

    applePie.theItems.push_back(ingredient{ 2,
    Measurement::count, "All-Purpose Flour" });

    applePie.theItems.push_back(ingredient{ 3,
    Measurement::count, "Cup of Water" });

    applePie.theItems.push_back(ingredient{ 4,
    Measurement::count, "Cup of White Sugar" });

    applePie.theItems.push_back(ingredient{ 5,
    Measurement::count, "Cup of Packed Brown Sugar" });

    applePie.theItems.push_back(ingredient{ 6,
    Measurement::count, "Branny Smith Apples - Peeled, Cored and Sliced" });


} 
You have the right idea.

A couple of problems:
Line 7: using namespace std requires a semicolon after it.

Line 11: enum values must be implicitly convertible to an integral type. i.e. You can't use float values. You also use cup as an enumeration 4 times. enumerations must be unique.

You don't need to number the elements you push onto a vector.

Here's a rework of what you have:
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
#include <iostream>
#include<string>
#include<vector>
using namespace std;

enum Measurement{ cup = 1, tablespoons = 2, each = 3 };

// Index this by Measurement to get the name of a measurement
const char* meas_names[] =  
{ ""  // zero not used
  "cup",
  "tablespoon",
  "each"
};

struct Ingredient
{
    float quantity; 
    Measurement type;
    string ingredient;
};

struct Recipe
{
    vector <Ingredient> ingredientsList;
    vector <string> stepBysteInstructions;

    void AddIngredient(const string& ing, float qty, Measurement meas);
};

void Recipe::AddIngredient(const string& ing, float qty, Measurement meas)
{
    Ingredient  temp;

    temp.ingredient = ing;
    temp.quantity = qty;
    temp.type = meas;
    ingredientsList.push_back(temp);
}

int main()
{
    Recipe applePie;
        
    applePie.AddIngredient("Unsalted Butter", 0.5, cup);
    applePie.AddIngredient("All-Purpose Flour", 2, cup); 
    applePie.AddIngredient("Cup of Water", 1, cup);
    applePie.AddIngredient("Cup of White Sugar", 1, cup);
    applePie.AddIngredient("Cup of Packed Brown Sugar", 1, cup);
    applePie.AddIngredient("Branny Smith Apples - Peeled, Cored and Sliced", 6, each); 
    
}

Last edited on
@ Abstraction, thank you! I get so confused. Your explanation at the beginning is so helpful. I think I was confusing the enumeration with a string and it went downhill from there.
Last edited on
enum names are mostly used to make your code prettier, similar to const. You could say
applePie.AddIngredient("Unsalted Butter", 0.5, 1); ///what is 1?
instead of
applePie.AddIngredient("Unsalted Butter", 0.5, cup); //ahah! I see what it is here!

you cannot read the word "cup" from the user and associate that to 1 though, the name is ONLY in the code (just like any other variable; you also can't get 'int x' from the user and associate it to a variable without doing it yourself). If you need to interface between the variable's name and its value, you need strings as shown above. The 'new' enum class failed to resolve this aggravation as well, so it remains an ugly spot in C++ where you have to do it yourself.

Last edited on
Topic archived. No new replies allowed.