The Assignment is as follows:

Write a program that allows the user to enter foods and their calories into two arrays. Allow for UP TO 100 entries, although the actual number may be way less. When the user types "done" then stop asking for new entries. Next, prompt the user for a food item; when entered, sequentially search the list and display the item and the number of calories. If the item is not found, indicate that. Only exact matches need to work, so upper/lower case matters. Stop prompting for items to search for when the user enters "done". Use the following screen shots as a guide.


my program is

#include <iostream>

#include <string>

using namespace std;

int main()
{
	string items[101], product;
	int calories[101];
	int c = -1;
	do
	{
		c++;
		cout << "Enter a menu it (enter 'done' when finished): ";
		getline(cin, items[c]);
		if (items[c] != "done")
		{
			cout << "Enter the number of calories: ";
			cin >> calories[c];
			cin.ignore();
		}		
	} 
	while (items[c] != "done");

	// Sort

	cout << "*** DATA ENTRY FINISHED ***" << endl;	

	for (int x = 1; x < c; x++)
	{
		for (int y = 0; y < c-1; y++)
		{
			if (items[y] > items[x])
			{
				string tmpfood = items[x];
				int tmpcal = calories[x];
				items[x] = items[y];
				calories[x] = calories[y];
				items[y+1] = tmpfood;
				calories[y+1] = tmpcal;
			}
		}
	}

	do
	{
		cout << "Enter a product to look up: ";
		getline(cin, product);

		for (int b = 0; b < c; b++)
		{
			if (product == items[b])
			{
				cout << product << " has " << calories[b] << " calories." << endl;
			}
			else if (product != items[b])
			{
				cout << product << " was not found. " << endl;
			}
		}
	}
	while (product != "done");
					
}

my almost works but for some reason after i look up the product it checks each value inputted in the array. How do I get it to find the specific item in the array?

Recommended Answers

All 2 Replies

hello...
please elaborate the problem that u r facing in this program.
the doubt u mentioned in the last 2 lines isin't clear enough..

my almost works but for some reason after i look up the product it checks each value inputted in the array. How do I get it to find the specific item in the array?

Your last loop prints out "not found" for every item that doesn't match. In reality, that else statement should be "isn't the item wanted" to be accurate. And with that knowledge, is it a useful thing to display?

Your assignment (as posted) does not mention sorting, so it's really not necessary. A linear search is not enhanced by sorted values.

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.