Make a veriable be equel to multiple numbers

Started by
16 comments, last by kingpinzs 17 years, 7 months ago
How can I have variable X be equal to multiple numbers? Example X = 1 and 2 and 3 and 4. If( input == x) { //do what ever… } I know a variable can only equal one at a time but I want to be able to input a integer and have it check against a list of numbers and if it equals one of them then continue. is that possible?
Advertisement
Logical operators, in this case logical OR: ||

if (input == 1 || input == 2)

Similarly, there is also logical AND: &&

if (input > 0 && input <= 10)

for instance, checks whether the variable is within a certain range.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
You could always do something like this:

std::vector<int> list;// add each item to the list like this:list.push_back( 1 );list.push_back( 2 );// loop through each one and do the checkfor( int i = 0; i < list.size(); ++i ){    if( input == list )    {        // do what ever...    }}
Mike Popoloski | Journal | SlimDX
Quote:Original post by ussnewjersey4
You could always do something like this:

*** Source Snippet Removed ***


with your code how can I add an else statment when it does not equel it with out having it loop through all the possible combos when it finds the one it needs?

Also whould it be better if I just read through a txt file or something because I also need to do some what the same thing with some strings or can I do the same thing with strings inside a vector?
Quote:Original post by kingpinzs
Quote:Original post by ussnewjersey4
You could always do something like this:

*** Source Snippet Removed ***


with your code how can I add an else statment when it does not equel it with out having it loop through all the possible combos when it finds the one it needs?

Also whould it be better if I just read through a txt file or something because I also need to do some what the same thing with some strings or can I do the same thing with strings inside a vector?

std::vector<int>::iterator it = std::find( list.begin(), list.end(), input );if( it != list.end() ){   // handle if it is there....   }else{   // it isnt there}
You should go with Arek the Absolute's solution: it's pretty efficient, as in fact it's using the bits of the integer you're passing as booleans. Unless that doesn't suit your needs, I'd say, go for it.


You need to define a few constants, the values you want to check for, and these have to be powers of two. In pseudo-code:
SMART = 1;
STRONG = 2;
FAST = 4;

You can then use a single integer to pass a combination of these values:
CreateMonster("Fast, smart monster", FAST | SMART);

The binary OR operator combines the two values 1 and 4, to 5. You could use + as well but it messes up when you use one of the constants twice, so the OR operator is safest. Binary, the integer is now 101, where the left-most bit is the FAST flag, the center bit is the STRONG flag and the right-most bit the SMART flag. As you can see, the FAST and SMART bits are set, just as you wanted.
You can check the integer as following:

if(integer & SMART != 0)
// the SMART bit is set

What it does is bitwise AND'ing the integer, so if the SMART bit is 1, then the result isn't 0. You can check the integer for every constant you want to check for this way. Keep in mind that the number of flags you can pass is limited to the size of the variabele in bits.
Create-ivity - a game development blog Mouseover for more information.
Quote:
Example
X = 1 and 2 and 3 and 4.
If( input == x)
{
//do what ever…
}



If it's only a couple of numbers, maybe use a switch statement:

switch( input ){  case 1:  case 2:  case 3:  case 4:    doWhatever();    break;  default:    doSomethingElse();    break;}


-JR
I'd consider using an enum with a switch statement. Like this:
enum value_type {VALUE_ONE   = 1,                 VALUE_TWO   = 2,                 VALUE_THREE = 3,                 VALUE_FOUR  = 4};value_type input;// Get inputswitch(input){     case VALUE_ONE:          //do stuff          break;     case VALUE_TWO:          //do stuff          break;     case VALUE_THREE:          //do stuff          break;     case VALUE_FOUR:          //do stuff}

Quote:Original post by Arek the Absolute
Logical operators, in this case logical OR: ||

if (input == 1 || input == 2)

Similarly, there is also logical AND: &&

if (input > 0 && input <= 10)

for instance, checks whether the variable is within a certain range.


QFT! There's been alot of other posts that are needlessly complicated and unreadable! Stick with what's easy, readable, and fast. I think some people took the thread title literally, while the actual post was about something a little different.
Ok this is what I got so far from all the replyes that I have got.

vector<int> list;int main(int argc, char *argv[]){   //had to put this loop first before the veriabls to get it to work    for (int x = 0; x <= 11; x++)     {         list.push_back(x);         }         int input ;   int Armor = 0;   int gold = 10;   int stat1 = 0;   int stat2 = 0;   int armorclass = 0;   int IteamPrice = 0;   string ArmorName = "Unarmored";cout<<"0.  Unarmored		AC: +0		Max Dex: INF"<<endl<<"1.  Padded Armor	AC: +1		Max Dex: +8		5g"<<endl<<"2.  Leather Armor       AC: +2		Max Dex: +6		10g"<<endl<<"3.  Hide Armor		AC: +3          Max Dex: +4             15g"<<endl<<"4.  Studded Leather	AC: +3		Max Dex: +5		25g"<<endl<<"5.  Scale Mail		AC: +4		Max Dex: +3		50g"<<endl<<"6.  Chain Shirt		AC: +4		Max Dex: +4		100g"<<endl<<"7.  Chainmail		AC: +5		Max Dex: +2		150g"<<endl			<<"8.  Breastplate		AC: +5		Max Dex: +3		200g"<<endl			<<"9.  Splint Mail		AC: +6		Max Dex: +0		225g"<<endl			<<"10. Banded Mail		AC: +6		Max Dex: +1		250g"<<endl			<<"11. Half-Plate		AC: +7		Max Dex: +0		600g"<<endl			<<"12. Full Plate		AC: +8		Max Dex: +1		1000g"<<endl;       cout<<"Pick your Armor type "<<endl<<endl;    cout<<endl<<"Tour curent armor type is "<<ArmorName<<endl;	cout<<"You have "<<gold<<" Gold left."<<endl;    cout<<"Choice: ";  vector<int>::iterator x = find( list.begin(), list.end(), input );      cin>>input;       if (x == list.end() )    {                           if(Armor != input)             {                                            if(gold >= 9)//how to put iteam price here from reading the input                    {                           //the meat or center of my function                           //every thing else was to check to make                           //sure the player could get this far                           //now that you are here need to beable to                           //add states and take away gold ect                                                      gold -= IteamPrice;                           stat1 = 10;                           stat2 = 20;                           armorclass = 5;                           ArmorName = " test";                                                                                  }                              else                         {                             cout<<"you dont have enogh gold";                                                          }                         }                             else              {                  cout<<"you have that already doop";                  }                                          }    else    {                cout<<"nope"<<endl;                main(0,0);        }        system("PAUSE");    return EXIT_SUCCESS;}



Now all I have to do is take the number inputed and find the corsponding veribel and then pull that states(for a lack of a better term) from it and fill out the veribles with the need info

can any one aid me on how to do it I think I almost got it but it is just on the tip of my brain and cant quit get it.

Thanks for all the help so far.

Edit:
Never mid I just add a switch statment and it all works together
so I just combined all the options and it all worked

Thanks again for all the help

And if you want to see the end code just let me know.

This topic is closed to new replies.

Advertisement