int main ()
{
string fooditem [101];
int calories [101];
int z = -1;
string productlookup [101];
do
{
z++;
cout << "Enter a menu item (enter 'done' when finished): ";
getline (cin, fooditem [z]);
if (fooditem[z] != "done")
{
cout << "Enter the number of calories: ";
cin >> calories[z];
cin.ignore();
}
}
while (fooditem[z] != "done");
do
{
cout << "Enter a product to look up: ";
getline (cin, productlookup [0]);
for (int y = 0; y <= z; y++)
{
if (productlookup[0] == fooditem [y])
{
cout << fooditem[y] << " has " << calories[y] << " calories" << endl;
}
else
{
cout << productlookup [0] << " was not found." << endl;
break;
}
}
}
while (productlookup [0] != "done");
}
What the program should do is tell me the item I looked up is not found. I am assuming it has something to do with how I set up my if-else scenario. But basically...I need a nudge in the right direction (not necessarily the answer) as to how I can get the program to tell me that the answer was not found or if the answer was found, tell the user how many calories are associated with the array value. Thanks all...appreciate the help.
I'm not sure quite what you're trying to do but I'll analyze your second do while as I would if I were seeking in an array...
Firstly why is it that you created an array for the variable productlookup when you only ever use the first element?
Anyway, in that for loop, you should only display output if you found something right? So insert a boolean variable somewhere in there and initialize it to false. At the start of each iteration of the do while, set it to false again, while in the for loop, you should set it to true every time you create output for a match. At the end of your do while, before the next iteration, check if that boolean is false and, if so, indicate that no matches were found.
Besides that I can't see what other errors there are. (You haven't actually told us what your problems are so the case is difficult to diagnose.)