Bubble Sorting Difficulties

Hello all,
I tried posting this on the beginners forum but wasn't having a ton of luck with it so maybe this forum will work better.

I have the code mostly working. I only have a couple little issues. When the bubble sort displays its data, the first entry is not included. Do I need to change something in the "for" specifications to make this work?
Secondly, also in the bubble sort, "done" is included as a type of food. How can I make sure that "done" isn't displayed in this bubble sort?

Thanks so much,
Sarah

Here is what I currently have:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 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[100];
	string selection;
	int calories[100];
	int counter=-1;
	bool done1=false;
	bool done=false;
	do
	{
		counter++;
		cout<< "Enter a menu item (enter 'done' when finished): ";
		getline(cin,food[counter]);
		if (food[counter] != "done")
		{
		cout << "Enter the number of calories: ";
		cin >> calories[counter];
		cin.ignore();
		}
	} while (food[counter] != "done");

	cout << "HERE IS THE SORTED DATA:" << endl;
	
	/*Output for this does not display the first entry and displays 
	the loop-terminating "done" as a food. */
	
	int i, j;
	string tmp;
	{
		for (i=0; i<counter; i++)
		{
			for (j=0; j<counter-i; j++)
				if (food[j] > food[j+1]) 
				{
					tmp=food[j];
					food[j] = food[j+1];
					food[j+1] = tmp;
					
						swap(calories[j], calories[j+1]);
				}
			
		}
	}
	
	
	for(i=0; i<counter; i++)
	{
		cout << food[i] << " has " << calories[i] << " calories." <<endl;
	}
	
	
	cout << "NOW YOU CAN SEARCH FOR DATA:" <<endl;
		
	do
	{
		cout <<"Enter a product to look up: ";
		getline(cin,selection);	
		bool found = false;
		for (int y=0; y<counter; y++)
			if (selection == food[y])
			{
				cout << food[y] << " has " << calories [y] << " calories." <<endl;
				found = true;
				break;
			} 
			if ((!found) && (selection != "done"))
			{
				cout << selection << " was not found." << endl;
			} 
			if (selection == "done")
				break;
	} while (selection != "done");

}
<deleted a silly guess>

in line 42 it should be counter-1. Now when i = 0, food[counter-1] gets swapped with food[counter] (which is 'done')

The sorting could be improved a little. Take a look at this: http://en.wikipedia.org/wiki/Bubble_sort#Implementation
Awesome-- thanks so much for your help with this!
Topic archived. No new replies allowed.