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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping datas together..

Status
Not open for further replies.

Sidro

MIS
Sep 28, 2002
197
US
Hi,
Im new to C++ and have so many questions..
Lets say I wanted to do a comparison of a group of datas. What are the best ways to do them in C++? For example...
I have a bunch of if statements.

if variableA is Monday,Tuesday,Wednesday cout<<&quot;Chicken&quot;;
if variableB is Thursday,Friday,Saturday cout<<&quot;Beef&quot;;

Is there a way to group Monday,Tuesday,Wednesday into
one variable? Thursday,Friday,Saturday into one variable and compare the two variables.Example, heres what Im thinking...

Variable1=Monday,Tuesday,Wednesday;
Variable2=Thursday,Friday,Saturday;
UserInputVariable=Tuesday;

if(UseInputVariable==variable1)
{
cout<<&quot;Chicken&quot;;
}
if(UserInputVariable==Variable2)
{
cout<<&quot;Beef&quot;;
}

Can we do something like what I've presented?Or are there better ways?

 
Something like this (tell me if there are errors).

const int days = 3;

char *variables1[days] = {{&quot;Monday&quot;},
{&quot;Tuesday&quot;},
{&quot;Wednesday&quot;}};

char *variables2[days] = {{&quot;Thursday&quot;},
{&quot;Friday&quot;},
{&quot;Saturday&quot;}};

char *currentDay = &quot;Tuesday&quot;;

for( int i = 0; i < days; i++ )
//checks to see if values are equal
if ( strcmp( currentDay, variables ) == 0 )
cout << &quot;Chicken&quot; << endl;
else if ( strcmp( currentDat, variables ) == 0 )
cout << &quot;Beef&quot; << endl;

Something similar to this will do. (strcmp is in string.h)

 
Another way ...

enum day {Sunday=0,Monday=1,Tuesday=1,Wednesday=1,
Thursday=2,Friday=2,Saturday=2};

day UserDay; // declared enum day.
//...

UserDay=Tuesday;

switch(UserDay)
{
case Sunday:
cout << &quot;A day to fast.&quot; << endl;
case Monday:
case Tuesday:
case Wednesday:
cout << &quot;Eat Chicken&quot; << endl;
break;
case Thursday:
case Friday:
case Saturday:
cout << &quot;Eat Beef&quot; << endl;
break;
default:
cout << &quot;Bad Day&quot; << endl;
}

//** CAUTION:
day today = 2; //** ERR can't assign int ot enum
int bigday = today //** O.K.

goto google.com and search on: C++ enum for more stuff.
Good Luck,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top