The purpose is to enter menu items and their calorie content [done, works], sort the array [done, not tested], and use a binary search to recall the array [errors]. The following are the current errors I get. My big issue, I think, is trying to get the right info into the function and getting the function variables defined correctly, but I admit arrays have been tough for me to grasp.

line 12: error C2143: syntax error : missing ')' before ';'
line 12: error C2059: syntax error : ')'
line 13: error C2470: 'key' : looks like a function definition, but there is no parameter list; skipping apparent body

#include <iostream>
#include <string>
using namespace std;
void BinarySearch (string list; int size; string key)   // line 12
{                                                       // line 13
	int left, right, mid;
	left = 0;
	right = size - 1;
	while (left <= right)
	{
		mid = (int) ((left+right)/2);
		if (key == list[mid])                   // line 20
		{
			return mid;
		}
		else if (key > list[mid])
			left = mid + 1;
		else
			right = mid - 1;
	}
	return -1;
}
int main () // Enters a number into an array then finds it.
{
	string food[100];
	string target;
	int cal[100];
	int i = -1;
	do
	{
		i++;
		cout << "Enter a menu item (enter 'done' when finished): "; getline (cin, food[i]);
		if (food[i] != "done")
		{
			cout << "Enter the number of calories: "; cin >> cal[i];
			cin.ignore();
		}
	} while (food[i] != "done");
	cout << endl << "HERE IS THE SORTED DATA:" << endl;
	for (int x = 1; x < i; x++)
		for (int y = 0; y < i; y++)
		{
			if (food[x] < food[y])
			{
				string tmp = food[x];
				food[x] = food[y];
				food[y] = tmp;
				int tmp2 = cal[x];
				cal[x] = cal[y];
				cal[y] = tmp2;
			}
		}
		for (int x = 0; x < i; x++)
			cout << food[i] << " has " << cal[i] << " calories." << endl;
		cout << endl << "NOW YOU CAN SEARCH FOR DATA:" << endl;
	do
	{
		int loc;
		cout << "Enter a product to look up: "; getline (cin, target);
		if (target != "done")
		{
			loc = BinarySearch (food, i, target);      // send to function
			if (loc > -1)
				cout << food[loc] << " has " << cal[loc] << " calories." << endl;
			else 
				cout << target << " was not found." << endl;
		}
		else 
		{
		}
	} while (target != "done");
}

Parameters in a function are separated by ',' not by ';'

Well, I did what you said (not sure why I thought that was right) and now I have about 30 errors, mostly...

Line 20: error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'std::string'

...and I have no idea what it even means.

The errors can be very cryptic, but trust me... after a while you will see at a glance what this means.

You are defining key as string, and list as string.
Then you compare key with list[mid], do you see how that could go wrong?

What is the type of list[mid]?


In short, the compiler doesn't have a comparison function in the string class that can compare a string with the type of `list[mid]`.


Edit: When posting code, please use 'CPP' syntax colouring.. it makes things easier to read:
code=cpp <-- put that between the [] braces, instead of just code.

Ok, I am going to put out what I see then you can tell me how I am wrong (because I obviously am wrong). List[mid] is a string (though mid by itself is not) because it is the array and has the food array as an original input to the function. Is it that using mid throws it off? That doesn't seem to be the case when the same setup is used in int main with food and i (food) so that shouldn't be it. Should I be using char instead of string? Not sure how that would matter but I do confess I don't use char often. Those are my only guesses and they don't seem like very good ones.

Also, I will try to remember about code=cpp as I've never heard of that one before.

Ok, it goes wrong in your assumption that list[mid] is a string, it is in fact a char.

The difference with food is that food is defined as an array of strings, and list is merely a string.

So I am going to guess that you want list to be an array of strings as well (I didn't look much at your code, this is just a guess based on the name 'list').

You may want to look at 'vector' classes in C++, they are much easier to handle than bare arrays usually.

Vectors are not a part of the course I am taking but I will look it up in any case and see what I can find.

I changed strings to chars in the function. I did not, however, understand vectors as we've not done this in class and I am already a little in over my head on arrays. I am cut down to 1 error though:

line 70 : error C2664: 'BinarySearch' : cannot convert parameter 1 from 'std::string [100]' to 'char []'

which is...

loc = BinarySearch (food, i, target);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.