string arrays loop

Hi,

In my code I would like to search through both arrays, to find if they have a matching string, and then get outputs, only for if both arrays have that string that it will say Both arrays have this string.
In case that one array has that string that I will get an output, only this array has this string.
Or if none of the arrays have that string that I will get none of the arrays have this string.

Here is the example of the code I made for this idea. But it doesn't work properly, I get all of the outputs more than once, and not just one output only once, like I stated above in the question.

I would very much appreciate any help,

Thank you in advance!

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

using namespace std;

int main()
{
    string ingredient;
    string pizza[3]= {"ham", "tomato", "chees"};
    string pasta[3]= {"tomato", "ketchup", "olive"};

    cout << "Write an ingredient!" << endl;
    cin >> ingredient;

    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;
            }

            else if (ingredient==pizza[a])
            {
                cout << "Pizza has "<<ingredient<< "as an ingredient."<< endl;
            }
            else if (ingredient==pasta[b])
            {
                cout << "Pasta has "<<ingredient<< "as an ingredient."<< endl;
            }

            else
            {
            cout << "Ingredient unknown";
            }
   } }

    return 0;
closed account (SECMoG1T)
I just skimmed through your code so I dint get it all but I understood your question.
You might consider making a function that uses algorithms like find() or just plain iterations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

 bool check (string arr[], size_t size, const string& tofind)
 {
     size_t i=0;
     while (i <size)
        {
            if(arr [i]==tofind)
                return true;
            i++;
        }
         return false;
  }

  void printresults (string arr [], string arr2 [], size_t arrsize, size_t arr2size, const string& tofind)
   {
        bool arrbool=false,  arr2bool=false;
        arrbool=check (arr, arrsize, tofind);
        arr2bool=check (arr2, arr2size, tofind);
    
         if (arrbool&&arr2bool) // print something
       else if(arrbool&&! arr2bool)// print something
         /// check the rest  2 conditions of the bools and print their results. 
  }
Last edited on
Consider what happens if you enter "ham". When a==0, line 26 will be true. In particular, line 26 will be true each time through the loop at line line 17. That's why it's printing multiple times.

Andy1992 is right. Create a function that will look for a string in an array. Then in main, use that function to decide if the ingredient is in pizza, pasta, or both.
I tried to implement this in my code, but I can't get it to work it, I am a begginer and don't know yet how to do this, tried to find some resources on internet before I was gonna ask you again, but nothing worked.

Could you please show me on my code how would that actually look?
Like I said I have tried, but I was obviously doing something wrong, because every time I tried I got errors.

Would very much appreciate help!
Thank you and sorry to bother you!
Using Andy1992's check() function and replacing your lines 15-37:

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  if (check (pizza, 3, ingredient) && check (pasta, 3, ingredient))
  { cout<<"Pizza and pasta have "<<ingredient<< " as an ingredient."<<endl;
  }
  else
  { // Not in both lists, check individual lists
    if (check (pizza, 3, ingredient))
    {  cout << "Pizza has "<<ingredient<< "as an ingredient."<< endl;
    }
     else
       if (check(pasta, 3, ingredient)) 
        { cout << "Pasta has "<<ingredient<< "as an ingredient."<< endl;
        }
       else
         { cout << "Ingredient unknown";
          }
    }

Last edited on
Thank you so much!
Topic archived. No new replies allowed.