c_string or const char?

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
Which line is line 72?

Use code tags. http://www.cplusplus.com/articles/jEywvCM9/

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
#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;
}




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
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
I do see the problem. I am calling an array of string and not a string. I have made some changes, however i've tried a few things and can't seem to correct it. This is what my code looks like now....


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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

void getFileName (string& fileName)
{
	cout << "Enter File Name: ";
	getline (cin, fileName);
}

int binarySearch(string artistList[], int first, int last, string aString)
{
	int loc=0, mid=last/2;

	if(first>last)
		loc=-1;
	else
	{
		mid = (first+last)/2;
		if(aString == artistList[mid])
			loc = mid;
		else if(aString < artistList [mid])
			loc = binarySearch(artistList, first, mid-1, aString);
		else 
			loc = binarySearch(artistList,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 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(string artistList[MAX], 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;
}




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.
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?
Yes, it should be from the input file
Topic archived. No new replies allowed.