vectors

I am trying to put up some code that I mostly written with the precious help from forum users, and learn arrays with it.
My qeustion is how would the code look like if I wanted to go through loop like in the example below and store inputs from user every time there is a new input, until it is finished. I don't know how to achieve this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string ingredient;
string answer;
string yes;
yes="yes";
      
cout << "Write an ingredient!" << endl;
    getline (cin, ingredient);
      question: cout << "Do you have any other ingredients to add, answer yes or no? " << endl;
        cin >> answer;
            if (answer==yes)
            {
                cout <<"What ingredient would you like to add? "<< endl;
                getline (cin, ingredient);
                goto question;
            }
            else
            { cout << "No ingredients to add!"<<endl;


Plus I also wonder how after I would store those inputs, I would be able to test them each one separatly if they are in arrays pizza or pasta or both of them with a function like the one below.

1
2
3
4
5
6
7
8
9
10
11
string pizza[3]= {"ham", "tomato", "chees"};
string pasta[3]= {"tomato", "ketchup", "olive"};

bool check (string list[], int n, string ingredient)
{   for (int a=0; a<n; a++)
    {  if (ingredient==list[a])
           return true;  
      }
  return false;
}


And then sort them to get outputs either like:
pizza has this ingredients:
pasta has this ingredients:
or both of them don't have any of the ingredients you listed.

I hope I made my self clear.
Thank you in advance for any help.

Last edited on
So are you asking how to loop through your code using a loop? or asking how to store inputs from the user when they enter something? Arrays cannot be resized during runtime, so you cannot add or remove anything when the program is running. I would suggest using a vector either way because it can be resized dynamically while the program is running. They are much better to use and used more than arrays anyways. You really won't have much need for arrays in programming except in special circumstances but you'll use vectors more often. I have been programming for 5 years and I have used an array maybe 2 or 3 times. But that doesnt mean you shouldn't learn arrays though.

Here is the code for a vector with soem comments hope it helps.


 #include <iostream>
 #include <vector>

using namespace std;

int main()
{
    vector<string> str_vect; //Declaring the vector to be of string type

    //.push_back() adds 1 element to the vector dynamically
    //To remove the last element, use .pop_back() which takes
    //no parameters

    str_vect.push_back("Element 1");
    str_vect.push_back("Element 2");
    str_vect.push_back("Element 3");

    for(int i = 0; i < str_vect.size(); i++)
    {
        cout << str_vect[i] << endl;
    }

}
Last edited on
Thank you for your reply!
So, I guess, something like this would apply for my code:

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
 
vector<string> ingredients;
    string ingredient;
    string answer;
    string yes;
    yes=="yes";

    cout << "Do you have any ingredient, yes or no? " << flush;
    cin >> answer;

    while (answer == "yes")
    {
        cout << "Enter ingredient: " <<flush ;
        cin >> ingredient;

        ingredients.push_back (ingredient);

        cout << "More ingredients, yes or no? " << flush;
        cin >> answer;
    }
for (vector<string>::iterator   i = ingredients.begin();
                                        i != ingredients.end();
                                        ++i)
{
    cout << "You have: "<<endl;
    cout << *i << endl;
}
    return 0;
}



Now I am wondering, how would I get from this code output in one sentence, like for example "You have: tomato, ham, chees." , and not what I get, new output for every element of the vector.

Next, something I still don't know, although I was trying in different ways is, how to go through my vector, and check all the elements to see if any of the elements is the same as any of the elements in pizza or pasta arrays, from the code from the first post.
And then sort them to get outputs either like:
pizza has this ingredients that you have:
pasta has this ingredients that you have:
or both of them don't have any of the ingredients you listed.

Thank you in advance!
Last edited on
Topic archived. No new replies allowed.