stopping a loop

Apr 12, 2009 at 1:14pm
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";
}

}
while (lookup != "I am done");
Last edited on Apr 18, 2009 at 6:17pm
Apr 12, 2009 at 1:26pm
the code looks fine.. whats the problem. as soon you enter "done" it will break.
Apr 12, 2009 at 3:09pm
x isn't being incremented, should be
for (int x = 0; x < i;++x)
Apr 12, 2009 at 3:53pm
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
else
		if (fruits[x] != lookup)
		{
			cout << lookup<< " was not found in database";
		}




Last edited on Apr 18, 2009 at 6:16pm
Apr 12, 2009 at 4:00pm
ooopss... :o i missed that..!! sorry.
Apr 12, 2009 at 8:17pm
yes, I realized you missed something...
Last edited on Apr 18, 2009 at 6:17pm
Apr 13, 2009 at 7:39am
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
int main ()
{
	string foods[100];
	int calories[100];
	string searchItem;
	int i = -1;
	do
	{
		i++;
		cout << "Enter a menu item (enter 'done' when finished): ";
		getline (cin, foods[i]);
		if (foods[i] != "done")
		{
			cout << "Enter the number of calories: ";
			cin >> calories[i];
			cin.ignore();
		}
	}
	while (foods[i] != "done");

	for (int x = 0; x < i; x++)
	cout << foods[x] << " " << calories[x] << endl;

	bool bFound = false;

	do
	{

		cout << "Product look up: ";
		cin.ignore();
		getline (cin, searchItem);

		for (x = 0; x < i;x++)
		{
			if (foods[x] == searchItem)
			{
				cout << foods[x] << " has " << calories[x] << " calories" << endl;
				bFound = true;
				break;
			}
		}

		if(bFound == false)
		{
			cout << searchItem << " was not found" << endl;			
		}

		bFound = false;
	
	}
	while (searchItem != "done");

	
	return 0;
}


see this and tell what was missing??!!! :)
Apr 13, 2009 at 9:43am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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"
Topic archived. No new replies allowed.