further troubles with sets

This is a discussion on further troubles with sets within the C++ Programming forums, part of the General Programming Boards category; Ok guys, I will post a bunch of code below. The first is my main.cpp, and the second is Sets.h. ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    further troubles with sets

    Ok guys, I will post a bunch of code below. The first is my main.cpp, and the second is Sets.h.

    What I can't figure out is how to keep a running order of the sets. How can I create new sets and keep the ones allready created? If I use linked list that will have pointers to the set arrays some of my set manipulations will not meet time reqs.

    Right now the create() fuction sends the ints to a stack which then creates the array.

    any ideas?
    Code:
    updated code below
    Last edited by axon; 12-03-2003 at 10:25 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Among other things,

    You need an instance of Sets to stick around for your entire program. I assume you want the user to run commands against the same group of sets, so you want them to exist throughout the program (or at least until they are all destroyed by the user). The way you have it now, you create an instance of Sets called object on the stack inside your getCommand function. But as soon as the function ends, the Sets object is destroyed. Every time you call getCommand, another different Sets object is created.

    There are many possible solutions: you can create an instance of Sets in the main function and pass it as a parameter to getCommand; you can create a global instance of Sets; you can make the instance of Sets inside getCommand static; you can move the getCommand functionality inside of your Sets class; you can separate your getCommand functionality into two parts - one that gets the command and one that processes it - and then find a way to pass the command to the part that processes it; etc.

    Once you do that, you can then use your Sets::createSet function to create a set when the user asks you to create one, and that way each created set will be saved inside the Sets object. Since you will have fixed it so that the Sets object sticks around for the entire program, you will always have access to the sets that have been created.

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    thanks jlou, I did change the instance of object to be in main, and then I pass it to getCommand().

    Now how would I make a new set in the masterArray that holds all 20 sets. below is the new SingleSet class, and the creat() function from Sets class
    Code:
    updated code below
    edit
    I've also changed allSets to be of type SingleSet. But the above code gives me errors.
    Last edited by axon; 12-03-2003 at 10:25 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>allSets[id] = new SingleSet;
    I don't think you can use the new operator for that since it isn't a pointer

    >>data.setId( id );
    Where is data declared?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well I've changed SingleSet allSets[20] to SingleSet* allSets[20] and I don't have the error anymore; however, everytime that an int is read from the user creat() is invoked, so the sets become of single elements.

    if input is
    > create 1 2 3

    which should result in a single set containing (1 2 3)
    I get 3 sets, each set gets its repective int.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    >>Where is data declared?
    in Sets class

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    damnit, why is this program giving me such problems...on the surface it seemed so simple! anyways...int the previous versions the id was not being updated and ideally it should have been. So I made ID as a variable in main and just passing it through. But I still can't figure out how to create a single set from a single lines input....

    below is updated code:
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    
    #include "sets.h"
    
    int setNumberOfSets( int );
    //if nummber of digits specified by user setNints to that number
    
    void getCommand( bool &, Sets, SingleSet, int&);
    //get input from user and send it to the class Sets
    
    
    int main()
    {
    	int nInts = 9;		//default number of integers 0...9
    	int commandLine = 0;
    	int setLimit = 19;	//20 workable sets at a time
    	int ID = 0;
    	bool isDone = true;
    	string command;
    	Sets object;
    	SingleSet startSet;
    	
    	nInts = setNumberOfSets( commandLine );
    	
    	
    	cout << "Welcome to Setman" << endl;
    	//start main application loop here
    	while ( isDone )
    	{
    		getCommand( isDone, object, startSet, ID );
    	}
    
    	return 0;
    }
    
    //=================================================
    int setNumberOfSets( int digits )
    {
    	if( digits != 0 )
    		return (digits-1);
    }
    
    //=================================================
    void getCommand( bool &isDone, Sets object, SingleSet startSet, int &ID )
    {
    	int firstVal = 0;	//these will hold the values following each command 
    	int secondVal = 0;	//except create, setids, and quit
    	int x, i=0, j=1;	//simple counter
    	int totMembers = 0;
    	int member;
    	char buff[256];		//buffer for creation of a set
    	char *p;
    
    
    	
    	string command;		//holds the first word of the user inputed command
    	string set;
    	
    	cout << ">  ";
    	cin >> command;
    
    	//create a set
    	if( command == "create" )
    	{
    		cout << endl << command << endl;
    		
    		gets( buff );
    
    		p = strtok( buff, " " );
    
    		while( p != NULL )
    		{
    			if( sscanf( p, "%d", &x ) )
    			{
    				member = atoi( p );
    				//startSet.push( member );
    				object.createSet( member, startSet, ID );
    			}
    
    			else
    				printf( "error on token %d: integer expected" );
    
    			i++;
    			p = strtok( NULL, " " );
    		}
    	}
    	//destroy set
    	else if( command == "destroy" )
    	{
    		cin >> firstVal;
    		object.set( firstVal );
    		cout << endl << "destroy";
    	}		
    	//empty a set
    	else if( command == "empty" )
    	{
    		cin >> firstVal;
    		object.set( firstVal );
    		cout << endl << command;
    	}	
    	//size of set
    	else if( command == "size" )
    	{
    		cin >> firstVal;
    		object.set( firstVal );
    		cout << endl << command;
    	}
    	//print set elements
    	else if( command == "printset" )
    	{
    		cin >> firstVal;
    		object.set( firstVal );
    		cout << endl << command;
    	}
    	//print out set ids 0..existing set
    	else if( command == "setids" )
    	{
    		cout << endl << command;
    	}
    	//determine if x is in a set
    	else if( command == "member" )
    	{
    		cin >> firstVal >> secondVal;
    		object.set( firstVal );
    		cout << endl << command;
    	}
    	//join to sets
    	else if( command == "union" )
    	{
    		cin >> firstVal >> secondVal;
    		object.set( firstVal, secondVal );
    		cout << endl << command;
    	}
    	//find intersection of two sets
    	else if( command == "intersection" )
    	{
    		cin >> firstVal >> secondVal;
    		object.set( firstVal, secondVal );
    		cout << endl << command;
    	}
    	//get difference of two sets
    	else if( command == "difference" )
    	{
    		cin >> firstVal >> secondVal;
    		object.set( firstVal, secondVal );
    		cout << endl << command;
    	}
    	if( command == "quit" )
    	{
    		cout << endl << command << endl;
    		isDone = false;
    	}
    }
    Code:
    //FILE: sets.h
    
    //==============================================
    //=============={ SINGLESET CLASS }=============
    //==============================================
    class SingleSet
    {
    private:
    	int setArray[100];
    	int top;
    	int id;
    
    public:
    	SingleSet();
    	void push( int );
    	void setId( int );
    	int getId(){ return id;}
    	bool isEmpty();
    	
    };
    
    //===============================
    SingleSet::SingleSet()
    {
      top = -1;
    }
    
    //===============================
    bool SingleSet::isEmpty() 
    {
      return (top == -1);
    }
    
    //===============================
    void SingleSet::push( int member )
    {
      top++;
      setArray[top] = member;
    }
    
    //===============================
    void SingleSet::setId( int ident )
    {
    	id = ident;
    }
    
    
    //==============================================
    //=============={ SETS CLASS }==================
    //==============================================
    class Sets
    {
    private:
    	int value1;
    	int value2;
    	SingleSet *allSets[20];
    
    public:
    	//declaring constructor
    	Sets();
    	
    	//declaring set functions
    	void set( int );
    	void set( int, int );
    
    	//declaring get functions
    	int getVal1() { return value1; }
    	int getVal2() { return value2; }
    
    	//declaring other functions
    	void createSet( int, SingleSet, int & );
    	void size( int* );
    	void print( int* );
    
    	
    };
    
    
    
    //=========================
    Sets::Sets()
    {
    	value1 = value2 = 0;
    	
    	//for( int i=0; i<20; i++)
    		//allSets[i] = NULL;
    }
    
    //=========================
    void Sets::set( int val1 )
    {
    	value1 = val1;
    }
    
    //=========================
    void Sets::set( int val1, int val2 )
    {
    	value1 = val1;
    	value2 = val2;
    }
    
    //===============================
    void Sets::createSet( int member, SingleSet data, int &ID )
    {
        allSets[ID] = new SingleSet;
    	data.setId( ID );
    	
    	cout << "   New set created. Set ID: " << data.getId() << endl;
    	ID++;
    }
    
    //===============================
    void Sets::size( int* set )
    {
    	//check if set exists, if it doesn't return error message
    	int size = 0;
    	//size = strlen( set );
    
    	cout << "   this set has " << size << " elements.";
    	cout << endl;
    }
    
    //==================================
    void Sets::print( int* set )
    {
    
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Hrm...Umm, I'm a bit confused...What is your problem??

    And I don't see data declared from what you posted, maybe posting your whole code will help clear things up

    Edit: You read my mind lol
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    here is a link to the assignment write up...hopefully it opens up:
    http://blackboard.uic.edu/courses/1/...05253_1/p3.pdf

    if that doesn't work try here:
    assignment
    Last edited by axon; 12-03-2003 at 10:04 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ok, so you want to create a set when a user enters a line like:

    create 12

    (with set ID being 12?)


    In any case, this doesn't look quite right:
    Code:
    void Sets::createSet( int member, SingleSet data, int &ID )
    {
        allSets[ID] = new SingleSet;
    	data.setId( ID );
    	
    	cout << "   New set created. Set ID: " << data.getId() << endl;
    	ID++;
    }
    Also, I don't think you want to make allSets an array of pointers, so you could change it back to what you had before and do something like:
    Code:
    void Sets::createSet( int member, int &ID )
    {
        allSets[++length].setID( ID );
    
    	cout << "   New set created. Set ID: " << allSets[length].getId() << endl;
    
    }
    It also seems best to store how many sets you have created within the Sets class (the length variable), and then when you create a set you can do it like above. Then you need to make a function in your Sets class which loops through the allSets array until it finds a Set with the given ID...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    no, thats not what I want to do. ID is just an individual sets place in the masterArray which holds 20 sets. When a set is created it gets the first empty ID...ie 0,1....20

    the commmand:
    > create 1 8 7 4
    should ideally create a set with all these members ( 1 8 7 4 )
    and if it is the first set created its ID should be zero

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Oh...Then you shouldn't have to pass it as a parameter, just store the number of sets created, and if you add (or remove?) just adjust it accordingly

    You shouldn't need to store the ID in the SingleSet class, just use it as the array index for allSets (assuming its a valid index)

    So your createSet() function will probably take an array of ints

    >>create 1 8 7 4

    Put the 1 8 7 and 4 in an array and pass it to the funtion

    I hope I'm starting to catch your drift
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    well this thread is becoming very long so I'll delete the old versions of the code. I think I've figure out a solution for the last problem about keeping a single line input a single set....but now I don't know how to invoke the stack call....

    Code:
    void Sets::createSet( int member, SingleSet data, int &ID )
    {
    	if( data.getId() != ID )
    	{
    		allSets[ID] = new SingleSet;
    		data.setId( ID );
    	}
    	//int member should be passed to push() here
    	cout << "   New set created. Set ID: " << data.getId() << endl;
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  14. #14
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Put the 1 8 7 and 4 in an array and pass it to the funtion

    I hope I'm starting to catch your drift
    this is not as simple as it sounds...but this part does it:
    Code:
    if( command == "create" )
    	{
    		cout << endl << command << endl;
    		
    		gets( buff );
    
    		p = strtok( buff, " " );
    
    		while( p != NULL )
    		{
    			if( sscanf( p, "%d", &x ) )
    			{
    				member = atoi( p );
    				//startSet.push( member );
    				object.createSet( member, startSet, ID );
    			}
    
    			else
    				printf( "error on token %d: integer expected" );
    
    			i++;
    			p = strtok( NULL, " " );
    		}
    		ID++; //update ID number
    	}

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  15. #15
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Obviously we are going about this in different ways, lol...I was thinking something like:
    Code:
    class Sets
    {
    ...
    private:
    int length;
    
    SingleSet allSets[20];
     
    }
    
    void Sets::createSet( int *data, int n) //n being how many numbers inputted
    {
    ++length;
    for (int i=0;i<n;i++)
    allSets[length].push(data[i]);
    		cout << "   New set created. Set ID: " << length << endl;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  2. Stl sets and custom classes.
    By cunnus88 in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2006, 11:58 PM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 05:37 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21