C++ Assigning attributes to variables ?
EDITED :: original post was confusing.
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. :) |
no....i don't get it :) sorry
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 ? |
struct variable1
{ char *oranges; char *pears; }variable01; you talkin like that? |
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... this : struct variable1 { char *oranges; char *pears; }variable01; sounds like a cool idea, could you explain it more ? |
k.. i just spent the last 20 minutes playing with the bitwise operators (never touched them before :)
..keep in mind this is an *example*, not something you want to go show your mother ... Code:
#include <iostream> ps...that 'greedy bastard' thing was a joke ...not meant to offend anyone |
Are you thinking labout something like a bit mask (a simple example below)
Code:
typedef unsigned short Fruit; |
...i'll have to look into bitshiftin next :)
|
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
|
Just for completeness:
Code:
multimap<string, string> all_our_shopping; |
the multimap is much cleaner:)
this isn't even my post, but thanks for showing me that (i'm not that far into c++ yet, just basics). ..haven't touched on iterators either :) cool, |
struct fruit
{ boolean apple; boolean orange; }fruits; fruits.apple = TRUE; fruits.orange = FALSE; if(&included != "apples") { fruits.apple = FALSE; } |
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... so, not : if basket.contents = "apple" more like : if basket.contents.apple = true |
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 :) |
okey d.
/ me reads, thanks man. |
All times are GMT -5. The time now is 04:48 PM. |