binary search for word in dictionary

I need to search for a word in a file and return if its in the file or not, but i'm not sure how to manipulate this code to do so, (this is from my teacher and this is what he told us to do) the file is in alphabetical order already as well and i do not know how to write a binary search at all.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

bool openfile(ifstream &handle, string path);
bool search(const vector<string>& v_word, const string& word);

int main(void)
{
	vector<string> v_word; // Declare a vector for the dictionary
	string word;
	vector<string>::iterator it;

	ifstream dictionary;
	string filename;
	unsigned int j = 0, k = 0;


	cout << "Enter the full path for the file you want to open" << endl;
	getline(cin, filename);

	if (!openfile(dictionary, filename))
		return 1;

	// Now go through the whole dictionary and read in the words
	while (!dictionary.eof())
	{
		dictionary >> word;
		//cout << word << endl;
		v_word.push_back(word);
		k++;
	}

	cout << "There are " << k << " words in the dictionary." << endl << endl;

	cout << "Enter a word to look up in the dictionary." << endl;
	cin >> word;
	// Now search if word is in dictionary

	

	cout << endl << endl << endl;
	system("pause");
	return 0;
}

bool openfile(ifstream &handle, string path)
{
	//	handle.open("C:/Temp/dictionary_four_letter_words.txt");
	handle.open(path);
	if (!handle)
	{
		cout << "Unable to open file" << path << endl;
		system("pause");
		return false;
	}
	else
	{
		cout << path << " Opened" << endl << endl;
	}
	return true;
}

bool search(const vector<string>& v_word, const string& word)
{
	vector<string> v_word; // Declare a vector for the dictionary
	string word;
	vector<string>::iterator it;
	

	if (search(v_word, word)){
		word = v_word;
		cout << " Word was found" << endl;

	}
		// found
	else{
		cout << " Word not in dictionary" << endl;

	}
	// not found


	return word;
}
Last edited on
Topic archived. No new replies allowed.