while loop with array condition

I have this array and i am asking the user to enter a string that is in array, but if the user doesn't enter a string that is not in the array i want to ask the user to reenter a string until the string is in the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 int size=2; 
       string array[size]={"dd", "BB"};
       string w; 
        for (int i =0; i<size; i++)
         { cout << array[i]<<endl; 
         }
          cout<"<Enter a string from above list"; <<endl;  
          getline(cin,w); 
          
        //the problem i am having
         while(w != array)
         { cout<"<Enter a string from above list"; <<endl;  
          getline(cin,w); }

       



The problem is how do i check if the user input of the string is in the array. The while loop i didn't doesn't work so how can i do this.

THANKS
Last edited on
shouldnt while(word != array) be while(w != array)?

@angeljrulz: yes ur right.

@johnhuge: on line four you have a << operator before the i++ which should be a ;. and you do it with a for and while loop:
1
2
3
4
5
6
7
8
9
10
11
12
bool valid = false;
string line;

while (!valid)
{
getline(cin, line);
for(counter = 0; counter < size; counter++)
{
     if(line == array[counter])
          valid = true;
}
}
Last edited on
THANKS I got it to work
Topic archived. No new replies allowed.