0

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;
}
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Wolf CCMLG
Featured Replies
  • 1
    jonsca 1,059   6 Years Ago

    You could use .find() - see the examples at [url]http://www.cplusplus.com/reference/string/string/find/[/url] (there's an overload for a character to be matched) Read More

0

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

0
////////////////////////////////////////////////////
// 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! ^_^

0
////////////////////////////////////////////////////
// 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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.