Hi guys, i'm new here and this is my first post.
This is my problem: i got an assignment like this: create a set contain of UNIQUE uppercase character, then do some operation on it, like, add element, check if element are in the set or not? how many elements there are in the set?
Here is what i manage to do on my own, but i got stuck on the Distinct characters, and when adding an element, if the element are already in the set, it's suppose to not add in, but in my code, it's the element still got added in the set.
Looking forward to the replies, and thanks you guys in advance!

Sorry for the long code!

#include <iostream>
using namespace std;

const int MAX = 25;
//define structures for link list
struct Node;
typedef Node* NodePtr;
typedef void* VoidPtr;

struct Node
{
	VoidPtr data;
	NodePtr next;
};
//function prototype
void constructSet (NodePtr&, int);
void AddElmnt (NodePtr&, VoidPtr);
void printSet (const NodePtr&);
int AddNewElmnt (NodePtr&, VoidPtr, int size);
void CheckElmnt (const NodePtr&);
int compare (VoidPtr, VoidPtr);
//main function
int main ()
{
	int size, msize, opt;
	char answer;
	char *elmnt;
	void* vp [MAX];
	
	srand (time (NULL));
	
	cout << "\tCSCI124, Assignment 2, Part 2\n" << endl;
	do
	{
		cout << "Enter a maximun size for the set: ";
		cin >> msize;
		size = rand () % msize;
		NodePtr head = NULL;
		
		constructSet (head, size);
		cout << "\nHere is an example on set of uppercase letters\n" << endl;
		cout << "SET S = ";
		printSet (head);
		
		do
		{
			cout << "\nWish to try the following operations?" << endl;
			cout << "1. Add an element to the set" << endl;
			cout << "2. Check an element in set" << endl;
			cout << "3. Check the cardinality" << endl;
			cout << "9. Quit\n" << endl;
			cout << "Your choice: ";
			cin >> opt;
			cout << "-----------------------------------------------------" << endl;
			switch (opt)
			{
			case 1:
			{
				elmnt = new char;
				cout << "Enter an element: ";
				cin >> *elmnt;
				size = AddNewElmnt (head, elmnt, size);
				cout << "Set S = ";
				printSet (head);
				cout << "...................................................." << endl;
			}
			break;
			
			case 2:
			{
				cout << "Current Set S = ";
				printSet (head);
				CheckElmnt (head);
				cout << "\n...................................................." << endl;
			}
			break;
			
			case 3:
			{
				cout << "Current Set S = ";
				printSet (head);
				cout << "S has " << size << " elements" << endl;
				cout << "...................................................." << endl;
			}
			break;
			
			case 9:
				break;
				
			default:
				cout << "Please enter a correct option!\n" << endl;
			}
		}
		while (opt != 9);
		do
		{
			cout << "\nDo you want to do it again (Y/N)? ";
			cin >> answer;
			if (answer != 'y' && answer != 'n')
				cout << "please answer with a correct answer!\n" << endl;
		}
		while (answer != 'y' && answer != 'n');
	}
	while (answer != 'n');
}
//create the set with random generated character (failed at generated random but unique character, need help here)
void constructSet (NodePtr& head, int size)
{
	char* elmnt;
	char x;
	VoidPtr vp;	   	   
	for (int i = 0; i < size; i++)
	{
		elmnt = new char;
		*elmnt = (char) (rand () % 26 + 65);
		vp = elmnt;
		AddElmnt (head, vp);
	}	 
}

void AddElmnt (NodePtr& head, VoidPtr elmnt)
{
	NodePtr temp = new Node;
	temp -> data = elmnt;
	temp -> next = head;
	head = temp;
}

void printSet (const NodePtr& head)
{
	NodePtr temp;
	char elmnt;
	int count = 0;
	temp = head;
	cout << "{";
	while (temp != NULL)
	{
		elmnt = *(static_cast <char *>(temp -> data));
		if (count > 0)
			cout << ", " << elmnt;
		else cout << elmnt;
		count++;
		temp = temp -> next;
	}
	cout << "}" << endl;
}
//add new element to the end of the set/list (need help here)
int AddNewElmnt (NodePtr& head, VoidPtr elmnt, int size)
{
	NodePtr temp = new Node;
	temp -> data = elmnt;
	temp -> next = NULL;
	size = size + 1;
	NodePtr curr;
	if (head == NULL)
		head = temp;
	else
	{
		curr = head;
		while (curr -> next != NULL)
			curr = curr -> next;
		curr -> next = temp;
	}
	return size;
}
//check if the element is in the set or not
void CheckElmnt (const NodePtr& head)
{
	NodePtr temp;
	char elmnt;
	int elhav = 0;
	cout << "Which element?: ";
	cin >> elmnt;
	temp = head;
	while (temp != NULL)
	{
		if (elmnt == *(static_cast <char *>(temp -> data)))
			elhav = 1;
		temp = temp -> next;
	}
	if (elhav == 1)
		cout << elmnt << " is in S";
	else cout << elmnt << " is not in S";
}
//failed attempt to compare and discard already have element
/*int compare (VoidPtr vp1, VoidPtr vp2)
{
	char elmnt1 = *(static_cast <char *> (vp1));
	char elmnt2 = *(static_cast <char *> (vp2));
	if (elmnt1 != elmnt2)
		return 1;
	else if (elmnt1 == elmnt2)
		return -1;
}*/

In AddNewElmnt() you never checked the list to see if the character entered is already there. Check before adding.

Problem solved! thx you gus a lot!

void constructSet (NodePtr& head, int size)
{
	int elhav = 0;
	char* elmnt;
	char x;
	VoidPtr vp;
	for (int i = 0; i < size; i++)
	{
		elmnt = new char;
		if (i == 0)
		{
			*elmnt = (char) (rand () % 26 + 65);
			vp = elmnt;
		}
		else
		{
			do
			{	 
				*elmnt = (char) (rand () % 26 + 65);
				vp = elmnt;
				elhav = checkSet(head, vp);
			}
			while (elhav != 0);
		}
			AddElmnt (head, vp);
	}
}
//check for repeat character
int checkSet (NodePtr& head, VoidPtr elmnt)
{
	int elhav = 0;
	NodePtr temp = head;
	while (temp != NULL)
	{
		if (compare (elmnt, temp -> data) == -1)
		{
			elhav = 1;
			temp = NULL;
		}
		else
		{
			elhav = 0;
			temp = temp -> next;
		}
	}
	return elhav;
}

int AddNewElmnt (NodePtr& head, VoidPtr elmnt, int size)
{
	NodePtr checks;
	checks = head;
	int elhav = 0;
	while (checks != NULL && elhav == 0)
	{
		elhav = compare (elmnt, checks -> data);
		checks = checks -> next;
	}
	if (elhav != -1) //check before add new element, thx to WaltP hint
	{
		NodePtr temp = new Node;
		temp -> data = elmnt;
		temp -> next = NULL;
		size = size + 1;
		NodePtr curr;
		if (head == NULL)
			head = temp;
		else
		{
			curr = head;
			while (curr -> next != NULL)
				curr = curr -> next;
			curr -> next = temp;
		}
	}
	return size;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.