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");
}