How do I populate my vector that is located in my struct? See the 2nd to the last for loop



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream> 
#include <vector>
#include <string>

using namespace std;

struct Ingredient
{
    float quantity;
    
    string ingredient;
    string instructions;
    string measurements;
    vector <string> stepBystepInstructions;
    
    
    //default constructor
    Ingredient() : stepBystepInstructions(3) {}
    
   //fully parameterized constructor

    Ingredient(float Quantity,  string Ingredient, int num, string Measurement) : stepBystepInstructions(num)
    {
        quantity = Quantity;
    
        ingredient = Ingredient;
        measurements = Measurement;
        
    }

    float Conversiontotablespoon(float Quantity)
    {
        
            Quantity = Quantity + 3;
            return Quantity;
    }
};

int main()
{   
    Ingredient ingredients;

    vector <Ingredient> ingredientsList;
    int num;
    
    
    cout << "Enter the number of ingredients in your recipe." << endl;
        cin >> num;

    
        string ingred;
        int quanity;
        string  measurement;

       

        for (int i = 0; i < num; i++) {
            cout << "Enter your ingredient." << endl;
            cin >> ingredients.ingredient;
            cout << "Enter the qty: " << endl;
            cin >> ingredients.quantity;
            cout << "Enter the measurement in teaspoons: " << endl;
            cout << "What is your measurement (must be cup, teaspoon, tablespoon, or each)?" << endl;
            cin >> measurement;
            
            ingredientsList.push_back(ingredients);
                  
        }
        string instructions;
           

        cout << "Enter the step by step instructions for your recipe (1 line at a time).";
               
        string r_name;
       
        cout << "Enter the name of your recipe: " << endl;
        cin >>  r_name;

        cout << "Here is a list of all your ingredients for recipe:  " << r_name<< "." << endl;
        
        for (int i = 0; i < num; i++)
        {
            cout << ingredientsList[i].ingredient << endl;
        }
        cout << "How many instructions do you have?" << endl;
        cin >> num;


//Here is my problem code - It doesn't like instructions

        for (int i = 0; i < num; i++)
        {
            cout << "Enter instructions: Step by Step" << endl;
            cin >> instructions;
            ingredientsList[i].stepBystepInstructions[i].push_back(instructions);
            
        }

        cout << "Here are a list of your cooking instructions for this recipe." << endl;
        for (int i = 0; i < num; i++)
        {
            cout << ingredientsList[i].stepBystepInstructions[i];
        }
        
}
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

This isn't your first time posting code, you have been asked nicely before to use code tags.
I changed it. I did not know to do this. I do INDENT my code. Also, I am a BEGINNER!
Line 95, try ingredientsList[i].stepBystepInstructions.push_back(instructions);

You don't need the index to your vector when pushing back strings of instructions.

Using code tags makes it a LOT easier to see things. :)

I haven't actually tested the revised code, I simply saw what was causing the error.
perhaps you may want to review your approach
¿why each ingredient have step-by-step instructions? (¿and what's the point of the `instructions' member variable?)
¿how will you model «mix the onion with the grilled milk»?


> Also, I am a BEGINNER!
¿why post in GENERAL then?
As a first refactor to change the data structures used, consider:

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
include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Ingredient {
	float quantity {};
	string ingredient;
	string measurement;
};

struct Recipe {
	string name;
	vector<Ingredient> ingred;
	vector<string> instructions;
};

int main() {
	Recipe recipe;
	size_t numingred {};

	cout << "Enter the number of ingredients in your recipe: ";
	cin >> numingred;
	cin.ignore();

	for (size_t i = 0; i < numingred; ++i) {
		Ingredient ingredients;

		cout << "Enter your ingredient: ";
		getline(cin, ingredients.ingredient);

		cout << "Enter the qty: ";
		cin >> ingredients.quantity;

		//cout << "Enter the measurement in teaspoons: ";
		cout << "What is your measurement (must be cup, teaspoon, tablespoon, or each)? ";
		cin >> ingredients.measurement;
		cin.ignore();

		recipe.ingred.push_back(ingredients);
	}

	cout << "Enter the step by step instructions for your recipe (1 line at a time).\n";
	cout << "Enter the name of your recipe: ";
	getline(cin, recipe.name);

	cout << "Here is a list of all your ingredients for recipe:  " << recipe.name << ".\n";

	for (size_t i = 0; i < recipe.ingred.size(); ++i)
		cout << recipe.ingred[i].ingredient << '\n';

	size_t numinstr {};

	cout << "How many instructions do you have? ";
	cin >> numinstr;
	cin.ignore();

	for (size_t i = 0; i < numinstr; ++i) {
		string instructions;

		cout << "Enter instructions: Step by Step\n";
		getline(cin, instructions);

		recipe.instructions.push_back(instructions);
	}

	cout << "Here are a list of your cooking instructions for this recipe.\n";
	for (size_t i = 0; i < recipe.instructions.size(); ++i)
		cout << recipe.instructions[i] << '\n';
}

Thank you George for your feedback. I tried what you suggested, and I was able to enter 1 instruction, but.. I got an out of index range. I realized I wasn't creating and initializing the vector object so I made a change to do that in the struct. While there are no syntax errors, I'll post my revised code. In the future, I will post in the beginners forum. I apologize for my ignorance! I really appreciate everyone's help.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream> 
#include <vector>
#include <string>

using namespace std;



struct Ingredient
{
    float quantity;
    
    string ingredient;
    string instructions;
    string measurements;
    vector <string> stepBystepInstructions = vector<string>(5);

    
    
    //default constructor
    Ingredient() : stepBystepInstructions(3) {}
    
   //fully parameterized constructor

    Ingredient(float Quantity,  string Ingredient, int num, string Measurement) : stepBystepInstructions(num)
    {
        quantity = Quantity;
    
        ingredient = Ingredient;
        measurements = Measurement;
        
    }

    float Conversiontotablespoon(float Quantity)
    {
        
            Quantity = Quantity + 3;
            return Quantity;
    }
};

int main()
{   
    Ingredient ingredients;

    vector <Ingredient> ingredientsList;
    int num;
    
    
    cout << "Enter the number of ingredients in your recipe." << endl;
        cin >> num;

    
        string ingred;
        int quanity;
        string  measurement;

       

        for (int i = 0; i < num; i++) {
            cout << "Enter your ingredient." << endl;
            cin >> ingredients.ingredient;
            cout << "Enter the qty: " << endl;
            cin >> ingredients.quantity;
            cout << "Enter the measurement in teaspoons: " << endl;
            cout << "What is your measurement (must be cup, teaspoon, tablespoon, or each)?" << endl;
            cin >> measurement;
            
            ingredientsList.push_back(ingredients);
                  
        }
        string instructions;
           

        //cout << "Enter the step by step instructions for your recipe (1 line at a time).";
               
        string r_name;
       
        cout << "Enter the name of your recipe: " << endl;
        cin >>  r_name;

        cout << "Here is a list of all your ingredients for recipe:  " << r_name<< "." << endl;
    
        for (int i = 0; i < num; i++)
        {
            cout << ingredientsList[i].ingredient << endl;
        }
        cout << "How many instructions do you have?" << endl;
        cin >> num;

        for (int i = 0; i < num; i++)
        {
            cout << "Enter instructions: Step by Step" << endl;
            cin >> instructions;
            ingredientsList[i].stepBystepInstructions.push_back(instructions);
            
        }

        cout << "Here are a list of your cooking instructions for this recipe." << endl;
        for (int i = 0; i < num; i++)
        {
            cout << ingredientsList[i].stepBystepInstructions[i];
        }
        
}
To seeplus:

Your code is absolutely more readable and better written. Thank you for your post.
The reason the struct is coded this way is because it was a homework assignment specifically asking for a vector of stepbystepinstructions created inside the recipe struct.

I am learning so I am probably missing the boat on this.

The actual assignment was to:
1. Create a recipe struct
2. Create a vector called ingredientsList inside your recipe instance.
3. push all the instructions on to the stepbystepinstructions vector inside your recipe instance.

When my professor referred to "recipe instance" I thought he meant the actual definition of the struct. Am I making this too hard? Can someone explain this to me in laments terms. In the future, I will post in the beginners forum. I apologize for this!
Side note, When I was thinking of the stepbystepinstructions vector, my thoughts were that if I created a string member of instructions in the recipe I could only do 1 instruction per recipe. So that was my thought on the vector stepbystepinstructions.
Thank you everyone!
If I read the instructions correctly here is the struct layout desired (in very abbreviated form):
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
#include <iostream>
#include <string>
#include <vector>

struct recipe
{
   std::vector<std::string> ingredientList;
   std::vector<std::string> stepbystepinstructions;
};

int main()
{
   // create a single instance of recipe
   recipe myRecipe;

   // the rest of the code for adding stuff to the recipe
   myRecipe.ingredientList.push_back("Some loverly taters");
   myRecipe.ingredientList.push_back("Two really squishy tomatoes");

   myRecipe.stepbystepinstructions.push_back("Take a hatchet to the taters and punish them!");
   myRecipe.stepbystepinstructions.push_back("Talk nicely to the tomatoes");

   // let's display the stuff
   std::cout << "Ingredient list:\n";
   for (const auto& itr : myRecipe.ingredientList)
   {
      std::cout << itr << '\n';
   }

   std::cout << "\nPrep instructions:\n";
   for (size_t i { }; i < myRecipe.stepbystepinstructions.size(); ++i)
   {
      std::cout << i + 1 << ": ";

      std::cout << myRecipe.stepbystepinstructions[i] << '\n';
   }
}
Ingredient list:
Some loverly taters
Two really squishy tomatoes

Prep instructions:
1: Take a hatchet to the taters and punish them!
2: Talk nicely to the tomatoes
Last edited on
RE: what is an instance....

In C++ a class or struct holds information that describes an object. Your recipe struct holds data that are two vectors, one for a list of ingredients and the other to hold the steps to preparing the recipe.

An instance of the struct is when you actually create an object you can use and manipulate. int x; or (line 14 above) recipe myRecipe;.

Think of your recipe struct as a photograph of a meal. Can you eat that photo?

Well, you CAN, but it really is no substitute for actual food.

You have to create the meal (line 14) before you can do anything like gather the ingredients and follow the prep instructions.
The actual assignment was to:
1. Create a recipe struct
2. Create a vector called ingredientsList inside your recipe instance.
3. push all the instructions on to the stepbystepinstructions vector inside your recipe instance.


Apart from the name used in 2) (I used ingred - I'm a lazy typist!), this is what I did above...

I also put the recipe name within the Recipe struct. You could then have say a vector of Recipe to have multiple recipes.
Topic archived. No new replies allowed.