Help Needed - Dictionary System

Apr 10, 2020 at 11:46pm
Hi guys, I'm new to C++ and was given an assignment to write program that will parse the dictionary file (dictionary.txt). It will load each record from the file into a new instance of WORD class and add this instance to a list of Word objects contained in an instance of DICTIONARY class. 3 basic task it has to perform.

1. Prompt the user enter the word. If that exact word = dictionary, print the word, definition, pre-pended with the word type with following scheme: Noun (n.), Adjective (adj.) etc. If the word not in dictionary, print 'word not found'.

2. Find longest word in the dictionary and print it to screen in the format of Task 1 (word, definition, word type)

3. Find the word(s) ends with 'logy' and contain 7 letters or less in total.

***with the current dictionary.txt format**************************************

abderite
An inhabitant of Abdera in Thrace.
n

abdest
Purification by washing the hands before prayer; -- a Mohammedan rite.
n
---------------------------------------------------------------------------
Notes about dictionary.txt
1. 106184 word definition
2. text format (ascii)
3. 4 lines per definition - the word, the definition, the type, blank line
4. word - only uses characters A-Z a-z and the hyphen '-'
5. word - no punctuation or similar
6. word - abbreviation are given without '.'(e.g. --> eg)
7. word - no more than 45 characters
8. word - all in LOWER CASE, even proper nouns
9. word - no words presented with spaces, either joined or hyphen is used.

I've completed Dictionary.h

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Dictionary
{
private:
string word;
string definition;
string wordType;

public:
//constructor
Dictionary(string word, string definition, string wordType)
{
Dictionary::word = word;
Dictionary::definition = definition;
Dictionary::wordType = wordType;
}

// getter methods for the class
string getWord()
{
return word;
}

string getDefinition()
{
return definition;
}

string getWordType()
{
return wordType;
}

bool CheckWord ()
{
if (word == getWord())
{
return 1;
}
return 0;
}


and I'm stuck at my main.cpp. Really appreciate any of your help? Just a guide will do me good. Thanks. Sorry I'm really new to C++.
Last edited on Apr 11, 2020 at 12:19am
Apr 11, 2020 at 1:14am
Steps are laid out for you. Ask the user for a word. Open a file stream, and go through the .txt until you find the word, or until you reach the end (meaning it wasn't in there).

Then, you go through the .txt and search for the longest word, this varies depending on the format of the .txt. But surely there will be a way to differentiate every word in the .txt. You'll take the word into a string, and you'll see how big the string is. Keep track of the largest one you encounter along the way.

Finally, using a similar method, you'll take in the words into a string, you'll parse the last 4 characters of the string like this:

1
2
3
4
5
6
7
if(word[word.size()] == "y")
{
    if(word[word.size()-1) == "g")
    {
        //And so on..
    }
}



If you get stuck doing a particular part of it, like using filestreams, then ask away.
Apr 11, 2020 at 2:46am
Thank you for your reply. just wondering how do i make it to read the records consistently given my txt format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	//Constructor to load dictionary records
	int main()
	{
		string word;
		ifstream dicFile("dictionary.txt");

		//open the file
		if (dicFile.is_open())
		{
			string buffer;
			string currentWord = "none";
			string currentDefinition = "none";
			string currentWordType = "none";

			// read the records
			while (!dicFile.eof())
			{
				//first line holds the name
				getline(dicFile, buffer);
				currentWord = buffer;

				//second line holds the definition
				getline(dicFile, buffer);
				currentDefinition = buffer;

				//3rd line holds the wordType
				getline(dicFile, buffer);
				currentWordType = buffer;

			}
			dicFile.close();
			printAllWords();
		}
		else cout << "Unable to open file";


	}


Last edited on Apr 11, 2020 at 2:48am
Apr 11, 2020 at 3:35am
Your question only states what the txt format is, you haven't shown it, so this may be slightly wrong if I misinterpreted:


Hazel
A color
Adjective

charcoal
.....and so on


I assume your txt looks something like this.


Your code looks fine for this, but you forgot one thing. You have an empty line to get rid of. The first time it loops, it'll be fine. But the time after it'll be a line behind. Instead of grabbing the word, it'll grab an empty line.

Add a getline(dicFile, buffer); to the end of the while loop. You need to grab 4 lines per loop, not 3.
Apr 11, 2020 at 5:00am
You need to redesign your classes. A dictionary is a collection of words. I would guess that it contains some container (e.g. vector). Your WORD class would contain actual word, definition and type.

Later, your read of a single word should involve 4 lines and shouldn't use .eof(). Use the condition of the stream after an attempted read to detect the end of file. After a WORD is read correctly it should be added to your DICTIONARY.
Apr 11, 2020 at 10:52am
Anyone able to detect why do my loop keep going on?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Dictionary.h"

using namespace std;

class DictionarySystem
{
private:
	vector<Dictionary>words;
public:

	DictionarySystem(string filename)
	{
		ifstream dicFile(filename);

		if (dicFile.is_open())
		{
			string word;
			string currentWord = "none";
			string currentDefinition = "none";
			string currentWordType = "none";


			while (!dicFile.eof())
			{
				getline(dicFile, word);
				currentWord = word;

				getline(dicFile, word);
				currentDefinition = word;

				getline(dicFile, word);
				currentWordType = word;

				getline(dicFile, word);


				//cerr << currentWord << endl; 
				//cerr << currentDefinition << endl; 
				//cerr << currentWordType << endl;



			}
			cout << "Load File Successfully! Please wait.\n" << endl;
			dicFile.close();
		}
		else cout << "Unable to open file!\n\n";

	}


	void queryWord()
	{
		string queryWord;
		//ask for the search word
		cout << "\n\n\n\n";
		cout << "******** WORD SEARCH ********\n\n\n";
		cout << "Enter the word you wish to search :";
		cin >> queryWord;
		for (Dictionary word : words)
		{
			if (word.getWord().find(queryWord)!= string::npos)
			{
				//found a matching word
				cout << "Match Found: \n";
				cout << word.getWord() << word.getDefinition() << word.getWordType() << endl;
			}
			else cout << "Word not Found! Try again.\n";
		}
	}


	void run()
	{
		bool exit = 0;
		char selection;
		while (!exit)
		{
			cout << "\n\n\n\n\n\n";
			cout << "**** WELCOME TO DICTIONARY ******\n\n\n";
			cout << "1. Word Searching\n";
			cout << "2. Longest Characters Word\n";
			cout << "3. End with 'LOGY' & <= 7 letters Words\n";
			cout << "4. Exit\n";
			cout << "Enter your choice in numerical and press ENTER (e.g. 1,2,3,4): ";
			cin >> selection;
			switch (selection)
			{
			case '1':
				queryWord();
				break;
		
			case '4':
				exit = 1;
				break;

			default:
				cout << "Invalid option, try again\n";
			}

		}
	}
};





Load File Successfully! Please wait.







**** WELCOME TO DICTIONARY ******


1. Word Searching
2. Longest Characters Word
3. End with 'LOGY' & <= 7 letters Words
4. Exit
Enter your choice in numerical and press ENTER (e.g. 1,2,3,4): 1




******** WORD SEARCH ********


Enter the word you wish to search :a






**** WELCOME TO DICTIONARY ******


Apr 11, 2020 at 5:08pm
Use true and false instead of numbers so that you can actually see what's going on.
Topic archived. No new replies allowed.