Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking in array

Status
Not open for further replies.

DaRedMonkey

IS-IT--Management
Aug 23, 2002
17
US

Is there a way to check to see if certain value is in the array.

For example, instead of doing something like the following statement:

if ( (x=='A') || (x=='W') || (x=='p') { ... }

I would like to do something to this effect:

options = ('A','W','P');

if ( x in options ) { ... }



Is that possible? If it is, how would you write it.

Thank you very much in advance.

--DRM
 
I dont know if this is what you expected:

Code:
#include <iostream.h>

bool isin( char* options , char x )
{
     for( int i = 0; options[i] != 0; i++ )
     {
          if( options[i] == x )
              return true;
     }
   
     return false;
}

void main()
{
      char *options = &quot;AWP&quot;;
      char x = 'P'; // an example
      
      if( isin( options, x ))
      {
           cout<<x<<&quot; is in &quot;<<options<<endl;
      }
      else
      {
           cout<<x<<&quot; is not in &quot;<<options<<endl;
      }
}
 

Thank you Leibnitz!

That's exactly what I am looking for. But, I thought it would be a lot simpler than that. Oh well, if it works, I am not going to complain. Thanks a bunch.

DRM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top