Counting the number of binary comparisons

I have my code here that asks the user to input a four letter word and then takes the word and searches through a txt file of 4030 4 letter words to determine if the input was a word or not. After it confirms whether it was a word or not, I need to count the number of comparisons of which I have no idea how to do, so please help me.

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
 #include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
int comp_count=0;
vector<string> sttok(string str) //used to tokenize string separated by space
{
int i=0;
vector<string> res;
string temp="";
for(i=0;i<str.size();i++)
{

if(str[i]==' ')
{
res.push_back(temp);
temp="";
}
else
{
temp.push_back(str[i]);
}
}
res.push_back(temp);
return res;

}


bool binarySearch(vector<string> array,int size ,string key)
{
int start=1, end=size;
int mid=(start+end)/2;

while(start<=end && strcmp(array[mid].c_str(),key.c_str())!=0)
{
comp_count++;
if(strcmp(array[mid].c_str(),key.c_str()) < 0) //tmp.c_str())
{
start=mid+1;
}
else{
end=mid-1;
}
mid=(start+end)/2;
}// While Loop End

if(strcmp(array[mid].c_str(),key.c_str())==0)
return true; //Returnig to main
else
return false;//Returnig to main

cout<<"\n\n\n";
}// binarySearch Function Ends Here

int main() 
{

ifstream is("C:/Temp/dictionary_four_letter_words.txt");
string buffer="";
char c;
while (is.get(c))
buffer+=c;
is.close();
vector<string> words = sttok(buffer);
cout<<"Enter the input word:";
string word;
cin >> word;
for(int i=0;i<words.size();i++)
{
cout << words[i]<<" ";
}
int size = words[words.size()-1].size();
words[words.size()-1].erase(size-1); //removing \n from last word
  
  
if(binarySearch(words,words.size() ,word))
{
cout<<"word is found";
cout<<comp_count;
}
else
{
cout<<"word is not found";
cout<<comp_count;


  
}
system("pause");
return 0;
}
Topic archived. No new replies allowed.