Can someone help me understand how to stop the loop when the user search for an item?
I noticed that if I use only int x=0; the program works as it is supposed to but then won't work for any other item entered if that item does not match with the first item in the array list...if I use for (int x=0; x < i;) then the loop wont stop...
this is the code where i am trying to stop the loop:
[code]
cout << "Product look up: ";
getline (cin, lookup);
for (int x = 0; x < i;)
//for ( int y = 1; y < i-1; y++)
if (fruits[x] == lookup)
{
cout << fruits[x] << " has " << cal[x] << " cal" << endl;
}
else
if (fruits[x] != lookup)
{
cout << lookup << " was not found in database";
}
Thank you for the replies. I corrected the ++x increment.
The problem is actually with the items lookup...
the program should cout only "apples contains 150 cal" and cout "item was not found in database" only if I type an item that is not in the array list...
1 2 3 4 5
elseif (fruits[x] != lookup)
{
cout << lookup<< " was not found in database";
}
int found = -1;
int lengthOfFoodList = 100;
cout << "Enter a product to look up: ";
getline (cin, searchItem);
while ( searchItem != "done" )
{
for ( int y = 0; y < lengthOfFoodList ); y++ )
{
if ( foods[ y ] == searchItem )
found = y;
}
if ( y == -1 )
cout << searchItem << " was not found";
else
cout << foods[ y ] << " has " << calories[ y ] << " calories" << endl;
}
cout << "End"