0

I am having trouble with an array. I have to enter some food items and their calorie values and then when finished I have to re enter the food item and then search the array to output their calorie values. My problem is how do I search an array. here is what I have so far

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string food[100];
	string searchvalue;
	int calories[100];
	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 << '\n' << "*** DATA ENTRY FINISHED ***" << endl;

	do
	{
		for (
		cout << "Enter a product to look up: ";
		getline(cin, food[x]);
		if(food[x] != "done")
		{
		cout << food[x] << " has " << calories[x] << " calories." << endl;
		}
		}while(food[x] != "done");
}

Edited by Nick Evan: Added code-tags

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by rinku2142
0

I am having trouble with an array. I have to enter some food items and their calorie values and then when finished I have to re enter the food item and then search the array to output their calorie values. My problem is how do I search an array. here is what I have so far

#include <iostream>
#include <string>

using namespace std;

int main()
{
string food[100];
string searchvalue;
int calories[100];
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 << '\n' << "*** DATA ENTRY FINISHED ***" << endl;

do
{
for (
cout << "Enter a product to look up: ";
getline(cin, food[x]);
if(food[x] != "done")
{
cout << food[x] << " has " << calories[x] << " calories." << endl;
}
}while(food[x] != "done");
}

look you are doing couple of mistakes before i tell you that you were suppose to post the code using code tags so do that ist
try to follow............
1) you are storing menu items in food[x]..and then again you are storing values of products in food[x]. dont do that!!!! declare variable like product with type string...
2) in search part try to compate product string with food[x] and use a increment counter to access values of food[x].e.g

if(product==food[j])

under loop

please post your code using code tags...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.