I'm having trouble using array and enum...

I'm trying to make a simple food database using a maximum of 50 foods stored.

The options are: a) add a food, d) delete a food, l) list all foods, m) "meal mode" and q) to quit.

Q to quit is simple, it's the other things involving arrays are the trouble...

A is a series of cout/cin statements including:
char name[SIZE]; // name of the food
Category category; // what kind of food***
float calories; // calories
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams

***Category was defined by:
enum Category {
unknown = -1, meat, poultry, seafood, dairy, vegetable,
fruit, grain, sweet, nCategory
};

M, D and L list what has been added, D gives the additional option of removing an item and M lists by food name (assigned a number starting at 0) and displays the remaining info of the food that's picked. I don't know how to implement any of this stuff as it all involves arrays, any advice?

This is my code so far:

#include <iostream>
using namespace std;

int main(void)
{
char choice;
bool terminate = false
char name[SIZE]; // name of the food
Category category; // what kind of food
enum Category {
unknown = -1, meat, poultry, seafood, dairy, vegetable,
fruit, grain, sweet, nCategory };

float calories; // calories
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams

while(!terminate)
{
cout << "Enter an option for the database:\n"
<< "(a) to add a food\n"
<< "(d) to delete a food\n"
<< "(l) to list all food items\n"
<< "(m) to enter Meal Mode\n"
<< "(q) to quit\n";

cin >> choice;

switch(choice)
{
case 'a':
case 'A':
cout << "name:";
cin >> ;
cout << "category:\n" << "meat = 0, poultry = 1, seafood = 2, dairy = 3, "
<< "vegetable = 4, fruit = 5, grain = 6, sweet = 7";
cin >> category;
//if (category == 0 || category == 1 || category == 2 || category == 3 || category == 4 || category == 5 || category == 6 || category == 7)
cout << "calories:\n";
cin >> calories;
cout << "carbohydrates:\n";
cin >> carbohydrates;
cout << "fat:\n";
cin >> fat;
cout << "cholesterol:";
cin >> cholesterol;
cout << "sodium:";
cin >> sodium;
cout << "protein:";
cin >> protein;
break;

case 'd':
case 'D':
break;

case 'l':
case 'L':
break;

case 'm':
case 'M':
break;

case 'q':
case 'Q':
terminate = true;
break;

default:
cout << "ERROR: You entered an illegal option. Please try again";
break;
}
return 0;

}
Read category choice as an integer then cast it to Category.

1
2
3
4
5
6
7
int categoryChoice ;
Category category;
..........
.......
cout << "Category : " ;
cin >> categoryChoice ;
category = static_cast< Category>( categoryChoice );
So that will assign it's number, I'm also wondering how to store all that data from choice A in one array value like I just did for the food name. How do I associate the name with it corresponding calories, fat, etc?

NEW:

#include <iostream>
using namespace std;
const int SIZE = 50;
int main(void)
{
char choice;
bool terminate = false
char name[SIZE]; // name of the food
enum Category {
unknown = -1, meat, poultry, seafood, dairy, vegetable,
fruit, grain, sweet, nCategory };
Category category; // what kind of food
float calories; // calories
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams

while(!terminate)
{
cout << "Enter an option for the database:\n"
<< "(a) to add a food\n"
<< "(d) to delete a food\n"
<< "(l) to list all food items\n"
<< "(m) to enter Meal Mode\n"
<< "(q) to quit\n";

cin >> choice;

switch(choice)
{
case 'a':
case 'A':
int categoryChoice;
for (int i = 0; i < SIZE; i++)
{
cout << "name:\n";
cin >> name[i];
)
cout << "category:\n" << "meat = 0, poultry = 1, seafood = 2, dairy = 3, "
<< "vegetable = 4, fruit = 5, grain = 6, sweet = 7";
cin >> categoryChoice;
static_cast<Category>(categoryChoice);
cout << "calories:\n";
cin >> calories;
cout << "carbohydrates:\n";
cin >> carbohydrates;
cout << "fat:\n";
cin >> fat;
cout << "cholesterol:";
cin >> cholesterol;
cout << "sodium:";
cin >> sodium;
cout << "protein:";
cin >> protein;
break;

case 'd':
case 'D':
//list, numbered by index at 0, "deleted: [food]"
for (int i = 0; i < SIZE; i++)
{
cout << name[i];
}

break;

case 'l':
case 'L':
//list, "index: 0 [food1], index: 1 [food2], etc..."
break;

case 'm':
case 'M':
//while
cout << "foods entered:\n";
cout << "pick foods for meal (-1 to end):\n"; //list foods
cout << "nutrition statistics for your meal:\n"; //list food
cout << "another meal (y or n)?\n";
cin >> meal_yn;
if (meal_yn == n || meal_yn == N)
{
cout >> "Press any key to continue . . .\n";
//break out
}
break;

case 'q':
case 'Q':
terminate = true;
break;

default:
cout << "ERROR: You entered an illegal option. Please try again";
break;
}
return 0;

}

You can use struct to keep them together.It's the easiest way.You can create a struct like
1
2
3
4
5
6
7
8
9
10
11
12
13
enum Category{ 
	unknown = -1, meat, poultry, seafood, dairy, vegetable,fruit, grain, sweet
};

typedef struct {
	float calories; // calories
	float carbonhydrates; // grams
	float fat; // grams
	float cholesterol; // grams
	float sodium; // grams
	float protein; // grams
	Category category ;
}Food;
Would I then make Food[50] or something? In order to display everything within each Food element do I call it as "Food"?

Still confused, sorry
Food is a new data type. When you declare an integer array do you write .
int[50]

or

int array_name[50];

??
First ,you should search some struct code samples.
In our example.It should look like
 
Food foodList[ 50 ] ;

If you don't want to use struct . You can keep each attribute as a single array.
1
2
3
4
5
6
float calories[50];
float fat[50];
float protein[50];
..
..
Category category[50];

But it's more confusing.I suggest using struct rather than to keep each attribute as a single array.
Last edited on
Topic archived. No new replies allowed.