array set with strings

i can't write code that intakes strings

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1958 Views - Last Post: 12 November 2008 - 07:28 AM Rate Topic: -----

#1 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

array set with strings

Post icon  Posted 11 November 2008 - 08:46 PM

Hi everyone and thanks again for helping me out on my last project i am very appreciative. now i have a problem creating and managing an array set that intakes strings instead of integers. i know how to write code that intakes integers as i will paste right now but i have no clue how to intake strings. please help thanks. here is my code that can intake integers. i have an add, remove, constructor, destructor, copy constructor, and size methods in my member functions. my code runs fine and all but i need to intake string instead thanks.

here is the assignment that i need to do.

Implement a Set class, where a set is an unordered collection of none or more elements with no duplicates. For this project, the elements should be of strings, i.e. names. The class should have the following data elements: the size of the set, and the list of names stored in the set. You may dynamic array to represent the list of names.

here are the methods i need for the project

Create and delete a Set. //include proper constructors and destructor.
• Add a new element to a Set.
• Remove an element from a Set.
• Return the number of elements in the Set.
• Compute the intersection of two Sets, that is, the set of elements that belongs both Sets. Use * operator function, i.e. S1*S2 = S1S2.
• Compute the union of two Sets, that is, the set of elements that belong to both Sets. Use + operator function, i.e. S1+S2 = S1S2.
• Compute the difference of two Sets. Use – operator function, i.e. S1-S2 represents the set of elements that belong to S1 but not to S2.
• Overloading an assignment operator that will copy one set to another.
• Overloading output operator << that will display all elements of a set.


and this is the output i need:

(1) Create a set CS256 containing the following names {“Joe”, “April”, “Mike”, “Justin”, “Wendy”, “Frank”, “Ted”, “Brent”, “Charlie”, “Cindy”}
(2) Create a set CS241 containing the following names {“Mike”, “Paul”, “Rita”, “Frank”, “Joshua”, “June”, “Cindy”, “Tomas”, “Robert”, “Jeff”, “Mandy”, “Eric”}
(3) Later “Ashley” and “Ken” added to CS256 while “Ken” and “Lena” added to CS241.
(4) All students in CS241 joined a new class CS299. (i.e. copy CS241 to CS299.)
(5) “Frank” dropped CS241.
(6) Calculate and print out how many students as well as names of students in (a) CS241, CS256, and CS299 respectively; (B) in either CS241 or CS256; © in both CS241 and CS256; and (d) in CS299 but not in CS241.


#include <iostream>
#include <string>

using namespace std;

class Set
{
public:
	Set();
	Set(int capacityValue);
	void add(int element);
	void addMany(int elements);
	bool full() const { return (capacity == used);}
	bool remove(int target);
	int getCapacity() const {return capacity;}
	int getNumberUsed() const {return used;}
	double& operator[](int index);
	int size() const {return used;}
	Set(const Set& Object);
	//union
	//difference
	//intersect
	//friend ostream&operator<<(ostream&, const Set&);
	~Set();
	
private:
	double *a;
	int capacity;
	int used;
};

	Set::Set():capacity(10), used(0)
	{
		a = new double[capacity];
	}
	
	Set::Set(int size) : capacity(size), used(0)
	{
		a = new double[capacity];
	}

	Set::Set(const Set& Object)
		:capacity(Object.getCapacity()), used(Object.getNumberUsed())
	{
		a = new double[capacity];
		for (int i = 0; i < used; i++)
			a[i] = Object.a[i];
	}

	void Set::add(int element)
	{
		if(used <= capacity)
		{
			cout << "exceeded the limit of capacity\n";
			exit(0);
		}
		int index;
		for (index = 0; (index < used) && (element != a[index]);
			index++);
		if (index == used)
		{
		   a[index] = element;
		   index++;
		}
	}
	
	void Set::addMany(int elements)
	{
		for(int i = 0; i < elements; i++)
			add(i);
	}

	

	bool Set::remove(int target)
	{
		int index;
		for (index = 0; (index < used) && (target != a[index]); 
			index++);
		if (index == used)
		return false;
		else
		{ 
			used--;
			a[index] = a[used];
			return true;
		}
	}

	double& Set::operator[](int index)
	{
		if(index >= used)
		{
			cout << "Illegal index in Set.\n";
			exit(0);
		}
		return a[index];
	}

	Set::~Set()
	{
		delete [] a;
	}

	void testSet();

	int main()
	{
		cout << "This program tests the class Set.\n";

		char ans;
		do
		{
			testSet();
			cout << "Test again? (y/n)";
			cin >> ans;
		}while (( ans == 'y') || (ans == 'Y'));

		return 0;
	}

	void testSet()
	{
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: array set with strings

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 08:53 PM

Here's an idea to get you on the right track:

int main()
{
	char name[255];
	string list[5];
	for (int i = 0; i < 5; i ++)
	{
		cout <<"\nEnter a name: ";
		cin.getline(name, 255);
		list[i] = name;
	}

	for (int i =0; i < 5; i++)
	{
		cout << list[i] << endl;
	}
	return 0;
}



I would just implement that into your class and boom--done!
Was This Post Helpful? 0
  • +
  • -

#3 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 09:05 PM

wow that works like a charm but i think my professor wants us to make a driver like something like this, well if i can even use it like this that is.

testSet()
{
	  cout << "tests the add function";
	  Set CS256= new Set();
	  a1.add("Mike");
	  a1.add("Justin");
	  cout << "here is the array class of CS256: " << a1; 
}



i think it has something to do with this method that i need to write.

Overloading output operator << that will display all elements of a set.

im pretty sure i need to display it with the output operator.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 09:14 PM

OK, this would work assuming you have to make it part of a class. If not this gives a good overview on operator overloading.
Was This Post Helpful? 0
  • +
  • -

#5 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 09:28 PM

thanks, okay i read your snippets and tried to implement it into my code but i get an error.

.\lab3.cpp(23) : error C2804: binary 'operator <<' has too many parameters

know i need only one


i wrote
ostream& operator<<(ostream& theStream, const Set& theNames)
	{
		theStream << theNames.getNumberUsed();
		return theStream;
	}


Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 09:30 PM

That's a weird error. Can I see an updated code listing?
Was This Post Helpful? 0
  • +
  • -

#7 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 09:40 PM

sure here is my code

and this was the error

.\lab3.cpp(23) : error C2804: binary 'operator <<' has too many parameters
i put an error sign next to the error so you don't have to look for it.


#include <iostream>
#include <string>

using namespace std;

class Set
{
public:
	Set();
	Set(int capacityValue);
	void add(int element);
	void addMany(int elements);
	bool full() const { return (capacity == used);}
	bool remove(int target);
	int getCapacity() const {return capacity;}
	int getNumberUsed() const {return used;}
	double& operator[](int index);
	int size() const {return used;}
	Set(const Set& Object);
	//union
	//difference
	//intersect
	ostream& operator<<(ostream&, const Set&); // error
	~Set();
	
private:
	double *a;
	int capacity;
	int used;
};

	Set::Set():capacity(10), used(0)
	{
		a = new double[capacity];
	}
	
	Set::Set(int size) : capacity(size), used(0)
	{
		a = new double[capacity];
	}

	Set::Set(const Set& Object)
		:capacity(Object.getCapacity()), used(Object.getNumberUsed())
	{
		a = new double[capacity];
		for (int i = 0; i < used; i++)
			a[i] = Object.a[i];
	}

	void Set::add(int element)
	{
		if(used <= capacity)
		{
			cout << "exceeded the limit of capacity\n";
			exit(0);
		}
		int index;
		for (index = 0; (index < used) && (element != a[index]);
			index++);
		if (index == used)
		{
		   a[index] = element;
		   index++;
		}
	}
	
	void Set::addMany(int elements)
	{
		for(int i = 0; i < elements; i++)
			add(i);
	}

	

	bool Set::remove(int target)
	{
		int index;
		for (index = 0; (index < used) && (target != a[index]); 
			index++);
		if (index == used)
		return false;
		else
		{ 
			used--;
			a[index] = a[used];
			return true;
		}
	}

	double& Set::operator[](int index)
	{
		if(index >= used)
		{
			cout << "Illegal index in Set.\n";
			exit(0);
		}
		return a[index];
	}

	ostream& operator<<(ostream&, const Set& theNames)
	{
		cout << theNames.getNumberUsed();
		return;
	}

	Set::~Set()
	{
		delete [] a;
	}

	void testSet();

	int main()
	{
		cout << "This program tests the class Set.\n";

		char ans;
		do
		{
			testSet();
			cout << "Test again? (y/n)";
			cin >> ans;
		}while (( ans == 'y') || (ans == 'Y'));

		return 0;
	}

	void testSet()
	{
	   char name[255];  
	  string list[5];  
	 for (int i = 0; i < 5; i ++)  
	 {  
		 cout <<"\nEnter a name: ";  
		 cin.getline(name, 255);  
		 list[i] = name;  
	 }  
   
	 for (int i =0; i < 5; i++)  
	 {  
		 cout << list[i] << endl;  
	 }  
	}



Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 09:49 PM

You also didn't return the stream in that, but I fixed it.

The issue is that you need to declare the overlaoded operator to be a 'friend' to your class:

#include <iostream>
#include <string>

using namespace std;

class Set
{
public:
    Set();
    Set(int capacityValue);
    void add(int element);
    void addMany(int elements);
    bool full() const { return (capacity == used);}
    bool remove(int target);
    int getCapacity() const {return capacity;}
    int getNumberUsed() const {return used;}
    double& operator[](int index);
    int size() const {return used;}
    Set(const Set& Object);
    //union
    //difference
    //intersect
    friend ostream& operator<<(ostream& theStream, const Set& theNames); // FIXED
    ~Set();
    
private:
    double *a;
    int capacity;
    int used;
};

    Set::Set():capacity(10), used(0)
    {
        a = new double[capacity];
    }
    
    Set::Set(int size) : capacity(size), used(0)
    {
        a = new double[capacity];
    }

    Set::Set(const Set& Object)
        :capacity(Object.getCapacity()), used(Object.getNumberUsed())
    {
        a = new double[capacity];
        for (int i = 0; i < used; i++)
            a[i] = Object.a[i];
    }

    void Set::add(int element)
    {
        if(used <= capacity)
        {
            cout << "exceeded the limit of capacity\n";
            exit(0);
        }
        int index;
        for (index = 0; (index < used) && (element != a[index]);
            index++);
        if (index == used)
        {
           a[index] = element;
           index++;
        }
    }
    
    void Set::addMany(int elements)
    {
        for(int i = 0; i < elements; i++)
            add(i);
    }

    

    bool Set::remove(int target)
    {
        int index;
        for (index = 0; (index < used) && (target != a[index]);
            index++);
        if (index == used)
        return false;
        else
        {
            used--;
            a[index] = a[used];
            return true;
        }
    }

    double& Set::operator[](int index)
    {
        if(index >= used)
        {
            cout << "Illegal index in Set.\n";
            exit(0);
        }
        return a[index];
    }

    ostream& operator<<(ostream& theStream, const Set& theNames)
    {
        cout << theNames.getNumberUsed();
        return theStream; // FIXED!!
    }

    Set::~Set()
    {
        delete [] a;
    }

    void testSet();

    int main()
    {
        cout << "This program tests the class Set.\n";

        char ans;
        do
        {
            testSet();
            cout << "Test again? (y/n)";
            cin >> ans;
        }while (( ans == 'y') || (ans == 'Y'));

        return 0;
    }

    void testSet()
    {
       char name[255];  
      string list[5];  
     for (int i = 0; i < 5; i ++)  
     {  
         cout <<"\nEnter a name: ";  
         cin.getline(name, 255);  
         list[i] = name;  
     }  
  
     for (int i =0; i < 5; i++)  
     {  
         cout << list[i] << endl;  
     }  
    }




The program runs well for me now. Was this helpful? :)

This post has been edited by KYA: 11 November 2008 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#9 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 10:00 PM

wow that was very easily over looked by me lol, but thanks a bunch. now i want to know if i did my add function right i fixed it from this:

void Set::add(int element)
	{
		if(used <= capacity)
		{
			cout << "exceeded the limit of capacity\n";
			exit(0);
		}
		int index;
		for (index = 0; (index < used) && (element != a[index]);
			index++);
		if (index == used)
		{
		   a[index] = element;
		   index++;
		}
	}



to this:

void Set::add(char element)
	{
		if(used <= capacity)
		{
			cout << "exceeded the limit of capacity\n";
			exit(0);
		}
		int index;
		for (index = 0; (index < used) && (element != a[index]);
			index++);
		if (index == used)
		{
		   a[index] = element;
		   index++;
		}
	}



i only changed the argument from integer to character do you know if i can do this??? thanks again.

oh and do you know how to write a driver for this program???

i was wondering if i can use it like this or am i wrong???

testSet()
{
	  cout << "tests the add function";
	  Set CS256= new Set();
	  a1.add("Mike");
	  a1.add("Justin");
	  cout << "here is the array class of CS256: " << a1;
}


Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 10:04 PM

The only problem with the char comparison is that the values at each array index are string rather then a single char. A char[] or string would be better suited. Because char element will never be equal to a string. i.e. 'a' != "Mike"

also, the driver currently in place doesn't even use your class.
Was This Post Helpful? 0
  • +
  • -

#11 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 10:16 PM

thanks for helping me for so long man i appreciate it. okay i changed alot of things mainly from int to char i have noted what i have changed on the side. is this what your talking about.


#include <iostream>
#include <string>

using namespace std;

class Set
{
public:
	Set();
	Set(int capacityValue = 10);
	void add(char element); // changed
	void addMany(char elements); // changed
	bool full() const { return (capacity == used);}
	bool remove(char target); // changed
	int getCapacity() const {return capacity;}
	int getNumberUsed() const {return used;}
	char& operator[](char index); // changed
	int size() const {return used;}
	Set(const Set& Object);
	//union
	//difference
	//intersect
	friend ostream& operator<<(ostream& theStream, const Set& theNames);
	~Set();
	
private:
	char *a; // changed
	char capacity; // changed
	char used; // changed
};

	Set::Set():capacity(10), used(0)
	{
		a = new char[capacity]; // changed
	}
	
	Set::Set(int size) : capacity(size), used(0)
	{
		a = new char[capacity]; // changed
	}

	Set::Set(const Set& Object)
		:capacity(Object.getCapacity()), used(Object.getNumberUsed())
	{
		a = new char[capacity]; // changed
		for (int i = 0; i < used; i++)
			a[i] = Object.a[i];
	}

	void Set::add(char element) // changed
	{
		if(used <= capacity)
		{
			cout << "exceeded the limit of capacity\n";
			exit(0);
		}
		int index;
		for (index = 0; (index < used) && (element != a[index]);
			index++);
		if (index == used)
		{
		   a[index] = element;
		   index++;
		}
	}
	
	void Set::addMany(char elements) // changed
	{
		for(int i = 0; i < elements; i++)
			add(i);
	}

	

	bool Set::remove(char target) // changed
	{
		int index;
		for (index = 0; (index < used) && (target != a[index]); 
			index++);
		if (index == used)
		return false;
		else
		{ 
			used--;
			a[index] = a[used];
			return true;
		}
	}

	char& Set::operator[](char index) // changed
	{
		if(index >= used)
		{
			cout << "Illegal index in Set.\n";
			exit(0);
		}
		return a[index];
	}

	ostream& operator<<(ostream& theStream, const Set& theNames)
	{
		theStream << theNames.getNumberUsed();
		return theStream;
	}

	Set::~Set()
	{
		delete [] a;
	}

	void testSet();

	int main()
	{
		cout << "This program tests the class Set.\n";

		char ans;
		do
		{
			testSet();
			cout << "Test again? (y/n)";
			cin >> ans;
		}while (( ans == 'y') || (ans == 'Y'));

		return 0;
	}

	void testSet()
	{
	   char name[255];  
	  string list[5];  
	 for (int i = 0; i < 5; i ++)  
	 {  
		 cout <<"\nEnter a name: ";  
		 cin.getline(name, 255);  
		 list[i] = name;  
	 }  
   
	 for (int i =0; i < 5; i++)  
	 {  
		 cout << list[i] << endl;  
	 }  
	}


Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 10:21 PM

What you changed really doesn't address the problem. Have the driver create a new set and add stuff to it. Only then will you see whether comparing a char to a string will work. (which it won't but it is better to see it in action).
Was This Post Helpful? 0
  • +
  • -

#13 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 10:33 PM

okay i tried using the driver testSet() but i get an error when trying to create a new set

.\lab3.cpp(130) : error C2668: 'Set::Set' : ambiguous call to overloaded function
.\lab3.cpp(10): could be 'Set::Set(int)'
.\lab3.cpp(9): or 'Set::Set(void)'
while trying to match the argument list '(void)'
.\lab3.cpp(130) : error C2668: 'Set::Set' : ambiguous call to overloaded function
.\lab3.cpp(10): could be 'Set::Set(int)'
.\lab3.cpp(9): or 'Set::Set(void)'
while trying to match the argument list '(void)'
.\lab3.cpp(131) : error C2664: 'Set::addMany' : cannot convert parameter 1 from 'const char [5]' to 'char'

#include <iostream>
#include <string>

using namespace std;

class Set
{
public:
	Set();
	Set(int capacityValue = 10);
	void add(char element);
	void addMany(char elements);
	bool full() const { return (capacity == used);}
	bool remove(char target);
	int getCapacity() const {return capacity;}
	int getNumberUsed() const {return used;}
	char& operator[](char index);
	int size() const {return used;}
	Set(const Set& Object);
	//union
	//difference
	//intersect
	friend ostream& operator<<(ostream& theStream, const Set& theNames);
	~Set();
	
private:
	char *a;
	char capacity;
	char used;
};

	Set::Set():capacity(10), used(0)
	{
		a = new char[capacity];
	}
	
	Set::Set(int size) : capacity(size), used(0)
	{
		a = new char[capacity];
	}

	Set::Set(const Set& Object)
		:capacity(Object.getCapacity()), used(Object.getNumberUsed())
	{
		a = new char[capacity];
		for (int i = 0; i < used; i++)
			a[i] = Object.a[i];
	}

	void Set::add(char element)
	{
		if(used <= capacity)
		{
			cout << "exceeded the limit of capacity\n";
			exit(0);
		}
		int index;
		for (index = 0; (index < used) && (element != a[index]);
			index++);
		if (index == used)
		{
		   a[index] = element;
		   index++;
		}
	}
	
	void Set::addMany(char elements)
	{
		for(int i = 0; i < elements; i++)
			add(i);
	}

	

	bool Set::remove(char target)
	{
		int index;
		for (index = 0; (index < used) && (target != a[index]); 
			index++);
		if (index == used)
		return false;
		else
		{ 
			used--;
			a[index] = a[used];
			return true;
		}
	}

	char& Set::operator[](char index)
	{
		if(index >= used)
		{
			cout << "Illegal index in Set.\n";
			exit(0);
		}
		return a[index];
	}

	ostream& operator<<(ostream& theStream, const Set& theNames)
	{
		theStream << theNames.getNumberUsed();
		return theStream;
	}

	Set::~Set()
	{
		delete [] a;
	}

	void testSet();

	int main()
	{
		cout << "This program tests the class Set.\n";

		char ans;
		do
		{
			testSet();
			cout << "Test again? (y/n)";
			cin >> ans;
		}while (( ans == 'y') || (ans == 'Y'));

		return 0;
	}

	void testSet()
	{
	   Set CS256 = new Set(); 
	   CS256.addMany("John");
	   
	}


Was This Post Helpful? 0
  • +
  • -

#14 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: array set with strings

Posted 11 November 2008 - 10:40 PM

see? You need to make Add()'s parameter a char* or a char[], etc...

Also, you need to explicitly state which constructor to call so you won't be an ambiguous error.
Was This Post Helpful? 0
  • +
  • -

#15 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: array set with strings

Posted 11 November 2008 - 10:53 PM

i tried putting my void testSet() into the class

class Set
{
public:
void testSet();
}

and then used it to see the Set class

void Set::testSet()
	{
	   Set CS256 = new Set(); 
	   CS256.addMany("John");
	   
	}



but im still getting an ambiguous error
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2