ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have 16 different variables --- and I want each variable to have a value, for example:
variable 1 : oranges, pears.
variable 2: apples, grapes
variable 3 : bannas, pears and oranges.
( Forgive me for my crude explination )
The enphisis is that each variable has serveral values each.
Now, how the hell would I do that ? / Is it possible ?
with that, I need a if statement, or some say of checking what fruits belong to what variables... if you understand ?
many thanks, ive been scratching my head for about 6 hours for a way around the problem, and this is my solution, just how to implement it is rather beyond me.
Thanks in advance.
Last edited by xconspirisist; 11-03-2003 at 05:25 PM.
you are trying to have these variables hold a given attribute (in this case "apples",or "bananas"...etc) and after these variables are assigned these attributes (each variable can hold more than one attribute) you would like to show which attributes are held in which variables (because each attribute can be used in multiple variables) ...am i close there ?
I think im trying to glorify something quite simple now I look at it again...
okey, the value of a variable, say the variable is fruit, and the value is apples. well I want the value, to be split into four different sections...
like :
fruit = 1,0,1,1
above ive replaced the value (fruit) with four different boolean values. See I want some code to read those values and see option one is on, option two is off, option three is on, and option four is on. So I could just have those boolean values as a intiger, like 1011, and use string malipulation - but isnt that really weak / corny ?
I could use seperate variables for each value, but I want to do this at LEAST 16 times, and thats a hell of a lot of variables...
Are you thinking labout something like a bit mask (a simple example below)
Code:
typedef unsigned short Fruit;
const Fruit apple = 1 << 0;
const Fruit pear = 1 << 1;
const Fruit banana = 1 << 2;
const Fruit orange = 1 << 3;
const Fruit mango = 1 << 4;
// etc for all 16 fruits
// Load up my fruit basket... using bit-wise OR
Fruit my_basket = apple | orange | mango;
// Test with bit-wise AND
if( my_basket & orange ) cout << "Fit and healthy\n"
else cout << "Oh dear, I have scurvy\n";
Well, I am not exactly clear how much reporting, storing, searching etc. is required ... I had thought maybe a std::multimap so that both the location of any fruit and also the contents of each basket can be listed - the problem is not just one of what want to be done but also in what context it is applied - so start simple and work up from there
Absolutly wicked I havnt tried any of these as of yet, im currently at school, but thanks so far. Ill let you know asap on which works / seems most appropreate. Cheers people, I very much appreciate your efforts
Okey, ive used the code posted by jhorvath, its almost exactly what I wanted, my greatest thanks go out to you. Cheers.
however... I just need a little more code, im sorry to ask for copy + paste code, but I really want the bulk of this section of my program done. So, without further a-do :
I need to know how IF a basket contains an apple, orange, grapefuit, etc. It needs to be boolean aswell...
i figured that much ...the code you need is already there ...you just have to find it. :)
open your favorite c++ book/tutorial ..whatever ..and read up on classes.
in short time you will have found the problem and fixed it ...i hope ;)
hint:: you have to move one line of code...
when you figure it out ...let us know why you did what you did, to make it work :)
enjoy,
ps..you're welcome
pss.... when a 'basket' contains a given fruit already(say apples), and you try to do something like ::
'basket.add(apples | bananas);`
bananas doesn't exist in the 'basket' yet ...but apples does ..it *should* reject the apples and add the bananas but it doesn't ...same thing with the 'basket.remove()' method. that is exercise #2 :)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.