Sorting Two Arrays

I am having issues with this program I've been working on. The program is supposed to take a menu item (up to 100 items) and number of calories from the user. After the user is done entering this information, they should enter "done" and then they will be able to enter a menu item and the number of calories will be displayed.

I am having trouble with the final output. The number of calories is only displayed for one of the items entered and not the other ones. Also, I cannot get the program to end once "done" is entered in the last part.

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
/* Write a program that allows the user to enter foods and their calories, up to 100 entries.  
When the user enters "done" then stop asking for new entries.  Prompt the user for 
a food item and when entered, display the number of calories.  If the item is not found,
say so.
*/ 


#include <iostream>
#include <string>
using namespace std;
int main()
{
	string food[101];
	string selection;
	int calories[101];
	int x=-1;
	do
	{
		x++;
		cout<< "Enter a menu item (enter 'done' when finished): ";
		getline(cin,food[x]);
		if (food[x] != "done")
		{
		cout << "Enter the number of calories: ";
		cin >> calories[x];
		cin.ignore();
		}
	} while (food[x] != "done");

	cout << "*** DATA ENTRY FINISHED ***" <<endl;
	/* Problems with the output: the number of calories doesn't always come up--
	one food entered will show the number of calories.
	Entering "done" doesn't end the program.
	*/
	for (int y=0; y<x; y++)
	do
	{
		cout <<"Enter a product to look up: ";
		getline(cin,selection);	
			if (selection == food[y])
			{
				cout << food[y] << " has " << calories [y] << " calories." <<endl;
			
			} 
			if ((selection != food[y]) && (selection != "done"))
			{
				cout << selection << " was not found." << endl;
			} 
					
		
	} while (selection != "done");



}
Well from what I gather, your loops to search for the products seems to be a bit off.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for (int y=0; y<x; y++) //I think this loop should be the inner loop.
//Reason being, every time the user enters "Done" this loop finishes 1 iteration, so you
//would need to enter done 100 times if there were 100 menu items!

	do //While this Do loop should be on the outside.
	{
		cout <<"Enter a product to look up: ";
		getline(cin,selection);	
			if (selection == food[y])
			{
				cout << food[y] << " has " << calories [y] << " calories." <<endl;
			
			} 
			if ((selection != food[y]) && (selection != "done"))
			{
				cout << selection << " was not found." << endl;
			} 
					
		
	} while (selection != "done"); //The reason this does not end the loop, I believe is due to
                                           //the fact that it is ending one iteration of the Do loop and the
                                           //outer for loop will still run for the number of products held in
                                           // your array. 


Try something more along the lines of this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
do
{
	cout <<"Enter a product to look up: ";
	getline(cin,selection);
        bool found = false; //Has the product been found?
	
	for (int y=0; y<x; y++)
	{
		
		if (selection == food[y]) //Is this the selection?
			{
				cout << food[y] << " has " << calories [y] << " calories." <<endl;
				found = true; //We found it so set found to true.
				break; //End the loop since we found what we wanted.
			}
	}
	if(!found) //If found is not true, then we did not find our product.
	{
		cout << selection << " was not found." << endl;
	}
		
} while (selection != "done"); //Keep in mind, if the user enters "done"
				//All of the above code runs through at least
				//once. 


My recommendation would be to change the Do While loop, into a While loop that way when the user enters "Done" the program will halt and do no searching.

Also, just a side note. Are you trying to create arrays of size 100? Or 101? Because the way you have it set up:

1
2
3
        string food[101]; //You've declared the food array to hold 101 elements.
	string selection;
	int calories[101]; //Same with this one. 


Hope this helps, if you have any questions or if I screwed up feel free to ask/point it out :)
Great, thanks for your help. I changed a few things (got rid of the do while loop) and it's working well.
Thanks again!
Topic archived. No new replies allowed.