#include <iostream>
#include <vector>
#include <string>
usingnamespace 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];
}
}
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?
include <iostream>
#include <vector>
#include <string>
usingnamespace 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.
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.
#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 (constauto& 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
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.