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 )
{
}