I am writing a program that reads in from a file (60,000) words. I think I got my code figured out except for the getline part. I will show you what I have ...what am i doing wrong with the getline part?
[code=c++]
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int binarySearch(string [], int, string);
const int size=60000;
int iCompares=0;
int main ()
{
string totalwords[size];
int result;
string word;
fstream SortedBinary;
SortedBinary.open("SortedBinary.txt");
if (!SortedBinary)
{
//Error
cout<< "There was an error unable to read file!";
return -1;
}
cout<< "Enter the word you wish to search for: ";
cin >> word;
//I thought this was the correct format but it doesn't work
//getline(SortedBinary, size, " ")
result=binarySearch( totalwords,size, word);
if (result==-1)
{
cout<< "Sorry, that word is not in this file. ";
}
else
{
cout<< word << " was found";
cout<< "This search conducted" << iCompares << "compares";
}
return 0;
}
int binarySearch(string array [], int element, string value)
{
int first=0;
int last = element-1;
int middle,
position=-1;
bool found=false;
while (!found && first<=last)
{
iCompares+=2;
middle=(first+last)/2;
if(array[middle] ==value)
{
found=true;
position=middle;
}
else if (array[middle]>value)
last=middle-1;
else
first =middle+1;
}
iCompares--;
return position;
}