So for my last assignment i have to create a program that allows the user to input 100 foods, enter calorie amount and then using bubble sort and binary search it has to be able to display them back to the user. I have all of this done except the food does not match up with the calories.


any help is greatly appreciated!!

#include <iostream>
#include <string>

using namespace std;

int main()
{
 string foods [100];
 string product;
 double calories [100];
 int counter = -1;
 bool done1 = false;
 bool done = false;
 do
 {
  counter++;
  cout << "Enter a menu item (enter 'done' when finished): ";
  getline(cin,foods [counter]);
  if (foods [counter] == "done"){
	  done1 = true;
  }
  else{
	  cout << "Enter the number of calories: ";
	  cin >> calories [counter];
	  cin.ignore();
  }
 } while (done1 == false);
int i;
int j;
string tmp;
//bubble sort
for (i=0; i<counter; i++) {
	for (j=0; j<counter-i; j++)
		if (foods[j+1]< foods[j]) {
			tmp = foods[j];
			foods[j] = foods[j+1];
			calories[j] = calories[j+1];
			foods[j+1] = tmp;
		}
		cout << foods[j] << " " << endl;
}
//end bubble sort, start binary search
while (done == false){
	int upperbound = counter;
	 cout << "Enter a product to look up: ";
	 getline(cin,product);
	 if (product == "done"){
		 done = true;
		 break;
	 }
	 int lowerbound = 0;
	 int position;
	 position = ( lowerbound + upperbound) / 2;
	 while((foods[position] != product) && (lowerbound <= upperbound))
	 {
		if (foods[position] > product)
		{
			upperbound = position - 1;
		} 
		else
		{
			lowerbound = position + 1;
		}
		position = (lowerbound + upperbound) / 2;
	 }
	 if (foods[position] == product){
			 cout << foods[position] << " has " << calories[position] << " calories." << endl;
			 cout << foods[1] << calories[1] << position;
		 }
	 if (lowerbound > upperbound)
		{
            cout << product << " was not found." << endl;
	 }}

}

the problem is that the bubble sort causes my calories to not line up with the food anymore, anyone know how to be able to fix this?

Create a structure containing both the food and its caleries. something like this

struct food{
       string item;
       int cal;
};

Then create an array of that structure store all the value into the array ie food[0].item = "crisps" & food[0].cal = 200 etc.

Then use food.cal to do your check on etc and re order the food array so for example swap food[0] with food[1].

I hope that makes sense lol. I does in my head but i'm bad at writing things on paper

Chris

Put the foods and the calories together in an array of struct and sort the struct.

He never taught us about structures and last time i learned something on my own and used it in my program he took off points :(. so i doubt he'd like me doing this.

In that case i think this is your problem

tmp = foods[j];
			foods[j] = foods[j+1];
			calories[j] = calories[j+1];
			foods[j+1] = tmp;

You need to put the value of calories[j] back into calories[j+1]

Chris

No problem, beat the teacher! Whenever you do a swap in your foods array, do the same in your calories array. Don't mix them.

ahh yess! thank you guys very much! :)

No problem, beat the teacher! Whenever you do a swap in your foods array, do the same in your calories array. Don't mix them.

I am having the same problem as he was. I guess were in the same class. I'm not sure how to fix it though, please help.

commented: If you're having the same problem start reading this thread CAREFULLY from the first post :angry: +0

Please ignore my message. I figured this out yesterday from the help given above. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.