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?
/* 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>
usingnamespace 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");
}