-1

I'm relatively new to C++/coding and I'm working on my final project for CS2. I'm trying to design a "recipe calculator" that will take in 3 ingredients(into a vector) then search through a recipe database for potential recipes.

Currently, I'm struggling with some basics, when I call the function that initializes the vector, it won't output the ingredients again in the main. When I try to output the vector inside the actual function, it works. But I want to make sure the same vector is saved in "ingreds" in the main.

int main()
{
    int y;
    cout << "Hello! Welcome to Abby's Recipe Calculator." << endl << endl;
    cout << "Please select an option: 1 to search by ingredient or 2 to browse recipes..." << endl;
    cin >> y;

    vector <string> ingreds;
    ingreds.reserve(4); 

    if (y == 1)
    {
        ingredientvector(ingreds);
        for (int i = 0; i < ingreds.size(); i++)
        {
            std::cout << ingreds[i];
        }
    }


    //else if (y == 2)
    //{
    //call recipe function... 
    //}

    system("pause");
    return 0;
}

vector<string> ingredientvector(vector<string> x)
{
    cout << "SEARCH BY INGREDIENT" << endl;
    cout << "Please enter up to three ingredients... " << endl;

    for (int i = 0; i < 4; i++)
    {
        x.push_back("  ");
        getline(cin, x[i]);
        if (x[i] == "1")
        {
            break;
        }
    }

    return x;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

Replace

vector<string> ingredientvector(vector<string> x)

By

void ingredientvector(vector<string>& x)

And don't return x at the end of ingredientvector. Passed by reference (&), the object can directly be modifier by the function).


Note: Your code could have worked, if you did:

ingreds = ingredientvector(ingreds);

Else, local x variable inside ingredientvector is filled, but has no effect on ingreds because it was passed by copy (x is a local copy of ingreds within ingredientvector). ingreds is only affected if x is returned by ingredientvector and then affected to ingreds.

But passing variable by references is definitely the right way to do here.

jpo38
  • 20,821
  • 10
  • 70
  • 151
0

When returning values, by default return them by value:

std::vector<string> ingredientvector()
{
  std::cout << "SEARCH BY INGREDIENT" << std::endl;
  std::cout << "Please enter up to three ingredients... " << std::endl;
  std::vector<string> x;
  x.reserve(4); 
  for (int i = 0; i < 4; i++)
  {
    x.push_back("  ");
    getline(cin, x[i]);
    if (x[i] == "1")
    {
        break;
    }
  }  
  return x;
}

Used like this:

if (y == 1)
{
    const auto ingreds=ingredientvector();
    for (const auto& this_ingred : ingreds)
    {
        std::cout << this_ingred << " ";
    }
}
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • Your code returns `"1"` as an ingredient. It'd be better to getline into a string variable, and only call `push_back` if we got past that test. – M.M Nov 27 '15 at 21:54
  • I'm just showing OP how to return the value. The logic is meant to be the same as in OPs post. – Johan Lundberg Nov 27 '15 at 22:26