Hi,

I am loading in a dictionary text file of English words, but I am not supposed to load any words if they contain an apostrophe. I am loading them in as strings, and I cannot figure out how not to load the string if it contains an apostrophe. Here is my code. Thanks.

class Dictionary
{
private:
	ifstream in;
	string word;
	int numOfWords;
 
public:
	Dictionary();
	~Dictionary(void);
	void Load(const char* filename);
};
void Dictionary::Load(const char* filename)
{
	numOfWords = 0;

	in.open(filename);
	if (!in.is_open())
	{
		cout << "ERROR! Cannot locate file: " << filename << '\n';
		return;
	}
	HTable<string> hTable(NUMOFBUCKETS,Hash);
	while(true)
	{
		getline(in, word, '\n');
		if(in.eof())
			break;

		// All words read in must be 3 to 6 characters long
		if(word.length() > 2 && word.length() < 7)
		{
				hTable.insert(word);
				++numOfWords;
		}
	}
	cout << "Number of words loaded into dictionary Hash table: " << numOfWords << endl;
}

You already check to see if the word has 3-6 characters. Also check for an ' too before inserting the word.

////////////////////////////////////////////////////
// Function: Load Dictionary from Text File
////////////////////////////////////////////////////
void Dictionary::Load(const char* filename, HTable<string> &hash)
{
	ifstream in;
	string word;
	size_t found;
		found = word.find('\'');
	numOfWords = 0;

	in.open(filename);
	if (!in.is_open())
	{
		cout << "ERROR! Cannot locate file: " << filename << '\n';
		return;
	}

	while(true)
	{
		getline(in, word, '\n');
		if(in.eof())
			break;

		// All words read in must been 3 to 6 characters long and must not contain an apostrophe
		if(word.length() > 2 && word.length() < 7 && found == string::npos)
		{
				hash.insert(word);
				++numOfWords;
		}
	}
	in.close();
	cout << "Number of words loaded into dictionary Hash table: " << numOfWords << endl;
}

Thanks guys! ^_^

////////////////////////////////////////////////////
// Function: Load Dictionary from Text File
////////////////////////////////////////////////////
void Dictionary::Load(const char* filename, HTable<string> &hash)
{
	ifstream in;
	string word;
	size_t found;
	numOfWords = 0;

	in.open(filename);
	if (!in.is_open())
	{
		cout << "ERROR! Cannot locate file: " << filename << '\n';
		return;
	}

	while(true)
	{
		getline(in, word, '\n');
		if(in.eof())
			break;

		found = word.find('\'');
               // All words read in must been 3 to 6 characters long and must not contain an apostrophe
		if(word.length() > 2 && word.length() < 7 && found == string::npos)
		{
				hash.insert(word);
				++numOfWords;
		}
	}
	in.close();
	cout << "Number of words loaded into dictionary Hash table: " << numOfWords << endl;
}

Oops, had a small mistake.

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.