Feb 28, 2018 at 1:37pm UTC
Hello, it will not allow me to enter Line numbers, or even preview it at all.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void getFileName (string& fileName)
{
cout << "Enter File Name: ";
getline (cin, fileName);
}
int binarySearch(string list[], int first, int last, string aString)
{
int loc=0, mid=last/2;
if(first>last)
loc=-1;
else
{
mid = (first+last)/2;
if(aString == list[mid])
loc = mid;
else if(aString < list [mid])
loc = binarySearch(list, first, mid-1, aString);
else
loc = binarySearch(list,mid+1, last, aString);
}
return loc;
}
const int MAX = 500;
int main()
{
string str, artistList[MAX];
string fileName = "\0";
ifstream inFile;
int num = 0;
int results;
int first;
int last;
string list;
string aString;
getFileName(fileName);
inFile.open(fileName.c_str() );
if(!inFile)
{
cout << "Can't open file " << fileName << endl;
return -1;
}
getline(inFile, str);
while(!inFile.eof())
{
cout << str << endl;
getline(inFile, str);
}
cout << "Enter Artist Name: ";
getline(cin, str);
results = binarySearch(list, first, last, aString);
if (results == -1)
cout << "Artist Not In List! \n";
else
{
cout << "That artist is located at element " << results << "in the array \n";
}
inFile.close();
system("pause");
return 0;
}
Last edited on Feb 28, 2018 at 1:59pm UTC
Feb 28, 2018 at 2:06pm UTC
int binarySearch(string list[], int first, int last, string aString)
Four parameters.
1) array of string
2) int
3) int
4) string
You trying to call it:
results = binarySearch(list, first, last, aString);
1) list - string
2) first - int
3) last - int
4) aString - string
See the problem?
Last edited on Feb 28, 2018 at 2:06pm UTC
Feb 28, 2018 at 2:12pm UTC
Line 72:
list
will be an empty single string while binarySearch(...) requires an array of strings.
You can simplify the loop on line 63:
1 2 3 4
while (getline(inFile, str)) // Note: getline() return false in case of an error such as eof
{
cout << str << endl;
}
Since
list
needs to be an array you may do the following:
1 2 3 4 5 6 7 8
string list[100];
for (last = 0; (last < 100) && getline(inFile, str); ++last)
{
list[last] = str;
cout << str << endl;
}
...
results = binarySearch(list, 0, last, aString);
Last edited on Feb 28, 2018 at 2:14pm UTC
Feb 28, 2018 at 2:40pm UTC
At line 71:
results = binarySearch(string artistList[MAX], first, last, aString);
That's not the syntax you use for calling a function. You don't specify the argument types, in a function call.
Feb 28, 2018 at 5:38pm UTC
IN addition to what MikeyBoy said, you need to fill the artistList array with values before you can search it. Should it comtain the lines from the input file?
Feb 28, 2018 at 5:51pm UTC
Yes, it should be from the input file