LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2003, 02:48 PM   #1
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Rep: Reputation: 30
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.

Last edited by xconspirisist; 11-03-2003 at 05:25 PM.
 
Old 11-03-2003, 02:53 PM   #2
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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 ?
 
Old 11-03-2003, 03:09 PM   #3
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
struct variable1
{
char *oranges;
char *pears;
}variable01;

you talkin like that?
 
Old 11-03-2003, 05:21 PM   #4
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Original Poster
Rep: Reputation: 30
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 ?
 
Old 11-03-2003, 06:44 PM   #5
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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>
using namespace std;

enum { none = 0, apples = 1,bananas = 2,cherries = 4,doodoo = 8 };

class picnicBasket {
        private:
                int contents;
        protected:
                bool has_inside(int what) { return (contents & what) ? true : false; }
        public:
                picnicBasket(void) { contents = none; }
                picnicBasket(int what) {
                        contents = none;
                        add(what);
                }

                void add(int what) {
                        if(!has_inside(what)) (contents |= what);
                        else cout << "You cannot have more than one of the same thing you greedy bastard.\n";
                }

                void remove(int what) {
                        if(has_inside(what)) (contents ^= what);
                        else cout << "You cannot remove something you do not have ..." << endl;
                }

                void emptyBasket(void) { contents = none; }

                void printContents(void) {
                        cout << "This basket has "
                             << (has_inside(apples))   << " apples, "
                             << (has_inside(bananas))  << " bananas, "
                             << (has_inside(cherries)) << " cherries, "
                             << (has_inside(doodoo))   << " doodoo.\n";
                }
                ~picnicBasket(void) { /*...*/ }
};

int main() {
        picnicBasket basket;
        picnicBasket basket2(apples | bananas | cherries | doodoo);

        basket.add(apples);
        basket.add(apples);

        basket.printContents();

        basket2.add(apples);
        basket2.add(bananas);
        basket2.add(cherries);
        basket2.add(doodoo);

        basket2.printContents();

        basket2.remove(apples);
        basket2.remove(apples);
        basket2.printContents();

        basket2.add(apples);
        basket2.printContents();

        basket2.emptyBasket();
        basket2.printContents();
        return 0;
}
have fun :)

ps...that 'greedy bastard' thing was a joke ...not meant to offend anyone

Last edited by jhorvath; 11-04-2003 at 01:46 PM.
 
Old 11-03-2003, 06:50 PM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
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";
Although you may wish to use an "enum"
 
Old 11-03-2003, 06:56 PM   #7
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
...i'll have to look into bitshiftin next :)
 
Old 11-03-2003, 07:13 PM   #8
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
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
 
Old 11-03-2003, 07:33 PM   #9
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Just for completeness:

Code:
    multimap<string, string> all_our_shopping;
    
    all_our_shopping.insert( make_pair( "My Basket", "Apple" ) );
    all_our_shopping.insert( make_pair( "Your Basket", "Banana" ) );
    all_our_shopping.insert( make_pair( "My Basket", "Mango" ) );
    all_our_shopping.insert( make_pair( "Your Basket", "Pineapple" ) );
    all_our_shopping.insert( make_pair( "My Basket", "Pineapple" ) );
    
    string s="Pineapple";
    for( multimap<string, string>::const_iterator iter=all_our_shopping.begin();
         iter!=all_our_shopping.end();
         ++iter )
    {
        if( iter->second == s )
          cout << s << " found in " << iter->first << '\n';
    }
    
    s="My Basket";
    cout << s << " contains: ";
    for( multimap<string, string>::const_iterator iter=all_our_shopping.lower_bound( s );
         iter!=all_our_shopping.upper_bound( s );
         ++iter )
    {
        cout << iter->second << ' ';
    }
    
    cout << endl;
(Obviously these don't have to be strings, just makes things clearer for illustration)

Last edited by dakensta; 11-03-2003 at 07:34 PM.
 
Old 11-03-2003, 08:37 PM   #10
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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,
 
Old 11-03-2003, 10:36 PM   #11
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
struct fruit
{
boolean apple;
boolean orange;
}fruits;

fruits.apple = TRUE;
fruits.orange = FALSE;

if(&included != "apples")
{
fruits.apple = FALSE;
}

Last edited by chewysplace; 11-03-2003 at 10:38 PM.
 
Old 11-04-2003, 03:37 AM   #12
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Original Poster
Rep: Reputation: 30
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
 
Old 11-04-2003, 12:48 PM   #13
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Original Poster
Rep: Reputation: 30
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
 
Old 11-04-2003, 01:26 PM   #14
jhorvath
Member
 
Registered: Sep 2002
Location: OH, USA
Distribution: 2.6.16-1.2096_FC5 #1
Posts: 245

Rep: Reputation: 30
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 :)

Last edited by jhorvath; 11-04-2003 at 01:52 PM.
 
Old 11-04-2003, 01:52 PM   #15
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Original Poster
Rep: Reputation: 30
okey d.

/ me reads, thanks man.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kdesktop attributes rogere Linux - Software 0 12-15-2004 08:10 PM
thread attributes iftiuk Programming 2 07-29-2004 01:41 PM
Name the attributes alaios General 5 10-29-2003 06:50 PM
file attributes binky22 Linux - Newbie 2 09-26-2003 04:17 PM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration