[try Beta version]
Not logged in

 
string arrays loop

Oct 23, 2014 at 7:06pm
Hi,

I need some help in understanding how to do loops with string arrays.
If I have few string arrays with some elements, for example:

string pizza[3]= {"ham", "tomato", "chees"};
string pasta[3]= {"tomato", "chicken", "olive"};

I wonder how, if I was to ask user for example:
string ingredient;
cout << " Please type an ingredient: ";
cin >> ingredient;

If user was to type in tomato, how can I loop it, to get an output like:

Pizza and pasta have tomato as an ingredient.

Is there any other way, to get the same result for this kind of problem.
Help would be much appreciated.

Thank you in advance!
Oct 23, 2014 at 8:46pm
1
2
3
4
5
6
7
8
for(int a=0;a<3;a++)
    {
        for(int b=0;b<3;b++)
        {
            if((ingredient==pizza[a])&&(ingredient==pasta[b]))
            cout<<"Pizza and pasta have "<<ingredient<< " as an ingredient."<<endl;
        }
    }
Oct 24, 2014 at 3:07pm
Great, thank you very much!
Topic archived. No new replies allowed.