0
\$\begingroup\$

I have a project where I would develop a nutrition label and a recipe list in a C++ class. I am facing problems when I run past void menu. The choice integer is when the user decided earlier if they would like to work with nutrition labels or recipes so that the program knows which array to work on.

I'm just asking for help on fixing any mistakes I made. I am a beginner in writing code so I'll gladly take any tips in the right direction.

Full code:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

void initialization(int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10], 
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void getLabel(int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10]);
void getRecipe(int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void menu(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void save(int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void add(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void addManual(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void addFile(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void edit(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void del(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void print(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);
void view(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100]);

int main()
{
    char recipes[50][50];               // array for recipe names
    char reciIngred[50][10][100];       // array for recipe ingredients
    char itemName[50][100];             // array for line integer
    char nutriIngred[50][10][500];      // array for nutrition ingredients
    char component[50][20];             // array for nutrition names
    double measurement[50][10];         // array for nutrition values
    char metric[50][10][10];                // array for nutrition units of measurement
    int dailyPercent[50][10];           // array for nutrition daily percent of nutritional needs
    int choice = 0;                     // user's answer
    int labeltotal = 0;                 // total amount of nutrition labels
    int recipetotal = 0;                // total amount of recipes

    initialization(labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
        recipetotal, recipes, reciIngred);
    do
    {
        cout << "Beginning of the program" << endl;
        cout << "Would you like to work with: " << endl
            << "1 - Labels" << endl << "2 - Recipes" << endl << "3 - Save" << endl << "4 - Quit" << endl;
        cin >> choice;

        switch (choice)
        {
        // nutrition label
        case 1: 
        {
            menu(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
                recipetotal, recipes, reciIngred);
            break;
        }
        // recipe
        case 2:
        {
            menu(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
                recipetotal, recipes, reciIngred);
            break;
        }
        // saving
        case 3: 
        {
            save(labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
                recipetotal, recipes, reciIngred);
            break;
        }
        case 4:
        {
            cout << "goodbye" << endl;
            break;
        }
        default: 
        {
            cout << "error" << endl;
        }
        }

    } while (choice != 4); // loops until 4 has been entered

    system("pause");
    return 0;
}

void initialization(int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    ifstream fin, fin2;

    // open files
    fin.open("Labels.txt");
    fin2.open("recipes.txt");
    fin >> labeltotal;              // total amount of labels in first line
    fin2 >> recipetotal;            // total amount of recipes in first line
    fin.ignore();
    fin2.ignore();

    for (int i = 0; i < labeltotal; i++)
    {
        fin.getline(itemName[i], 100);
        for (int j = 0; j < 10; j++)
        {
            fin >> nutriIngred[i][j];
        }
        for (int n = 0; n < 26; n++)
        {
            fin.getline(component[n], 20);
            fin >> measurement[i][n];
            fin >> metric[i][n];
            fin >> dailyPercent[i][n];
        }
    }
    for (int k = 0; k < recipetotal; k++)
    {
        fin2 >> recipes[k]; // get name of the recipes

        for (int l = 0; l < 10; l++)
        {
            fin2 >> reciIngred[k][l];
        }
    } 

    // close files
    fin.close();
    fin2.close();

    return;
}

void getLabel(int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10])
{
    for (int i = 0; i < labeltotal; i++)
    {
        cout << "Label line: " << i << endl;
        cout << itemName[i] << endl;
        for (int j = 0; j < 10; j++)
        {
            if (nutriIngred[i][j][0] != '#')
            {
                cout << nutriIngred[i][j] << " ";
            }
            else
            {
                break;
            }
        }
        cout << endl;
        for (int n = 0; n < 26; n++)
        {
            cout << " " << component[n] << ": " << measurement[i][n] << " " << metric[i][n] << " ";
            if (dailyPercent[i][n] != -1)
            {
                cout << dailyPercent[i][n];
            }
            else
            {
                break;
            }
            cout << endl;
        }
    }

    cout << endl;
    cout << "What label line would you like to work with: ";
}

void getRecipe(int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    for (int k = 0; k < recipetotal; k++)
    {
        cout << "recipe line: " << k << endl;
        for (int l = 0; l < recipetotal; l++)
        {
            cout << recipes[k][l];
        }
        for (int m = 0; m < 10; m++)
        {
            cout << " " << reciIngred[k][m] << endl;
        }
    }

    cout << "What recipe line would you like to work with?" << endl;        // ask user what recipe to work on

}

void menu(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    int option;

    cout << "Menu function" << endl;        // display function name
    cout << "Make a menu choice:" << endl   // display choices
        << "1 - add " << endl << "2 - edit " << endl << "3 - delete" << endl << "4 - print" << endl << "5 - view" << endl;
    cin >> option;

    switch (option)     // switch function for user's choice
    {
    case 1:
    {
        add(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
            recipetotal, recipes, reciIngred);
        break;
    }

    case 2:
    {
        edit(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
            recipetotal, recipes, reciIngred);
        break;
    }

    case 3:
    {
        del(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
            recipetotal, recipes, reciIngred);
        break;
    }

    case 4:
    {
        print(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
            recipetotal, recipes, reciIngred);
        break;
    }

    case 5:
    {
        view(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
            recipetotal, recipes, reciIngred);
        break;
    }

    default:
    {
        cout << "Bad entry choice" << endl;
    }
    }

    return;
}

void add(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    char quitpro = 'n';
    int option;
    int itemIndex = 0;

    cout << "adding function" << endl;

    do
    {
        cout << "Make a choice: " << endl
            << "1 - quit" << endl << "2 - add manually" << endl << "3 - add from a file" << endl;
        cin >> option;

        switch (option)
        {
        case 1:
        {
            cout << "Are you sure you want to quit? ";
            cin >> quitpro;
            break;
        }
        case 2:
        {
            addManual(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
                recipetotal, recipes, reciIngred);
            break;
        }
        case 3:
        {
            addFile(choice, labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent,
                recipetotal, recipes, reciIngred);
            break;
        }

        default:
        {
            cout << "error input" << endl;
        }
        }

        for (int i = 0; i < 50; i++)
        {
            cout << itemName[i] << component[i] << measurement[i] << metric[i] << dailyPercent[i] << endl;
        }

    } while (quitpro == 'n' || quitpro == 'N');

    return;
}

void addManual(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    cout << "Add manual function" << endl;

    switch (choice)
    {
    case 1:
    {
        for (int i = labeltotal + 1; i < (labeltotal + 2); i++)
        {
            cout << "Enter additional item name: ";
            cin >> itemName[i];
            while (itemName[i] != "#")
            {
                for (int j = 0; j < 10; j++)
                {
                    cout << "Insert additional ingredients: ";
                    cin >> nutriIngred[i][j];
                }

                cout << "Enter additional component: ";
                cin >> component[i];
                for (int n = 0; n < 26; n++)
                {
                    cout << "insert additional measurement: ";
                    cin >> measurement[i][n];
                    cout << "insert additional metric: ";
                    cin >> metric[i][n];
                    cout << "insert additional daily percent: ";
                    cin >> dailyPercent[i][n];
                }
            }
        }
        break;
    }
    case 2:
    {
        for (int k = (recipetotal + 1); k < (recipetotal + 2); k++)
        {
            cout << "insert additional recipe: ";
            cin >> recipes[k];

            for (int l = 0; l < 10; l++)
            {
                cout << "insert additional ingredients: ";
                cin >> reciIngred[k][l];
            }
        }
        break;
    }
    default:
    {
        cout << "error" << endl;
    }
    }

    labeltotal = (labeltotal + 1);
    recipetotal = (recipetotal + 1);
    return;
}

void addFile(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    int x = 0, y = 0;
    ifstream fin, fin2;

    // open file
    fin.open("labelsInput.txt");
    fin2.open("recipesInput.txt");
    fin >> x;
    fin2 >> y;

    cout << "Add from file function" << endl;

    // if nutrition was chosen in the beginning
    if (choice == 1)
    {
        for (int i = labeltotal; i < (labeltotal + x); i++)
        {
            fin >> itemName[i];
            for (int n = 0; n < 26; n++)
            {
                fin.getline(nutriIngred[i][n], 500);
                fin >> component[n];
                fin >> measurement[i][n];
                fin >> metric[i][n];
                fin >> dailyPercent[i][n];
            }
        }
    }

    // if recipe was chosen in the beginning
    else if (choice == 2)
    {
        for (int k = (recipetotal + 1); k < (recipetotal + y); k++)
        {
            fin2 >> recipes[k]; // get name of the recipes

            for (int l = 0; l < 10; l++)
            {
                fin2 >> reciIngred[k][l];
            }
        }
    }

    labeltotal = (labeltotal + x);
    recipetotal = (recipetotal + y);
    return;
}

void edit(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    char quitpro = 'n';
    int option;
    int itemIndex = 0;
    int newItem, newIngred, newComponent, newMeasurement, newMetric, newDaily, newRecipe, newtotal;

    do
    {
        cout << "edit function" << endl;
        cout << "Make a choices: " << endl
            << "1 - stop" << endl << "2 - continue editing" << endl;
        cin >> option;

        switch (option)
        {
        case 1:
        {
            cout << "Are you sure you want to quit? (Y/N)" << endl;
            cin >> quitpro;
            break;
        }
        case 2:
        {
            if (choice == 1)
            {
                getLabel(labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent);
                cin >> itemIndex;

                cout << "Insert new item name: ";
                cin >> newItem;
                cout << "Insert new ingredients list: ";
                cin >> newIngred;
                cout << "Insert new component: ";
                cin >> newComponent;
                cout << "Insert new measurement: ";
                cin >> newMeasurement;
                cout << "New metric: ";
                cin >> newMetric;
                cout << "New daily percentage: ";
                cin >> newDaily;

                for (int i = 0; i < labeltotal; i++)
                {
                    if (i = itemIndex)
                    {
                        itemName[i][100] = newItem;
                        nutriIngred[i][10][500] = newIngred;
                        component[i][10] = newComponent;
                        measurement[i][10] = newMeasurement;
                        metric[i][10][10] = newMetric;
                        dailyPercent[i][10] = newDaily;
                    }
                }
            }
            else if (choice == 2)
            {
                getRecipe(recipetotal, recipes, reciIngred);
                cin >> itemIndex;

                cout << "Insert new recipe name: ";
                cin >> newRecipe;
                cout << "Insert new ingredients: ";
                cin >> newIngred;

                for (int k = 0; k < recipetotal; k++)
                {
                    if (k = itemIndex)
                    {
                        recipes[k][50] = newRecipe;
                        reciIngred[k][10][100] = newIngred;
                    }
                }
            }
            break;
        }
        }

    } while (quitpro == 'n' || quitpro == 'N');

    return;
}

void del(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    char quitpro = 'n';
    int option;
    int itemIndex = 0;
    int newItem, newIngred, newComponent, newMeasurement, newMetric, newDaily;

    cout << "delete function" << endl;
    do
    {
        cout << "Make a choice: " << endl
            << "1 - stop" << endl << "2 - continue deleting" << endl;
        cin >> option;

        switch (option)
        {
        case 1:
        {
            cout << "Are you sure you want to quit? (Y/N)" << endl;
            cin >> quitpro;
            break;
        }
        case 2:
        {
            if (choice == 1)
            {
                getLabel(labeltotal, itemName, nutriIngred, component, measurement, metric, dailyPercent);
                cin >> itemIndex;

                for (int i = 0; i < labeltotal; i++)
                {
                    if (i = itemIndex)
                    {
                        itemName[i][100] = 0;
                        nutriIngred[i][10][500] = 0;
                        component[i][10] = 0;
                        measurement[i][10] = 0;
                        metric[i][10][10] = 0;
                        dailyPercent[i][10] = 0;
                    }
                }
            }
            else if (choice == 2)
            {
                getRecipe(recipetotal, recipes, reciIngred);
                cin >> itemIndex;
                for (int k = 0; k < recipetotal; k++)
                {
                    if (k = itemIndex)
                    {
                        recipes[k][50] = 0;
                        reciIngred[k][10][100] = 0;
                    }
                }
            }
            break;
        }
        }

    } while (quitpro == 'n' || quitpro == 'N');

    return;
}

void print(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    ofstream fout, foutt;
    fout.open("tempLabels.txt");
    foutt.open("tempRecipes.txt");

    cout << "Printing to file" << endl;
    switch (choice)
    {
    case 1:
    {
        fout << labeltotal << endl;
        for (int i = 0; i < labeltotal; i++)
        {
            fout << itemName[i] << endl;
            for (int j = 0; j < 10; j++)
            {
                    fout << nutriIngred[i][j] << " ";
            }
            fout << endl;
            for (int n = 0; n < 26; n++)
            {
                fout << component[n] << measurement[i][n] << " " << metric[i][n] << " " << dailyPercent[i][n];
            }
            fout << endl;
        }
        break;
    }
    case 2:
    {
        foutt << recipetotal << endl;
        for (int k = 0; k < recipetotal; k++)
        {
            foutt << recipes[k] << " ";
            for (int l = 0; l < 10; l++)
            {
                foutt << reciIngred[k][l] << " ";
            }
            foutt << endl;
        }
        break;
    }
    default:
    {
        cout << "error" << endl;
    }
    }

    fout.close();
    foutt.close();
    return;
}

void view(int & choice, int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    cout << "Viewing on screen" << endl;
    // display new array on console

    switch (choice)
    {
    case 1:
    {
        for (int i = 0; i < labeltotal; i++)
        {
            cout << itemName[i] << endl;
            for (int j = 0; j < 10; j++)
            {
                if (nutriIngred[i][j][0] != '#')
                {
                    cout << nutriIngred[i][j] << " ";
                }
                else
                {
                    break;
                }
            }
            cout << endl;
            for (int n = 0; n < 26; n++)
            {
                cout << component[n] << ": " << measurement[i][n] << " " << metric[i][n] << " ";

                if (dailyPercent[i][n] != -1)
                {
                    cout << dailyPercent[i][n];
                }
                cout << endl;
            }
        }
        cout << endl;
        break;
    }

    case 2:
    {
        for (int k = 0; k < recipetotal; k++)
        {
            cout << recipes[k] << ": ";
            for (int l = 0; l < 10; l++)
            {
                if (reciIngred[k][l][0] != '#')
                {
                    cout << reciIngred[k][l] << " ";    //temp
                }
                else
                {
                    break;
                }
            }
            cout << endl;
        }
        break;
    }
    default:
    {
        cout << "error" << endl;
    }
    }
    return;
}

void save(int & labeltotal, char itemName[][100], char nutriIngred[][10][500], char component[][20], double measurement[][10], char metric[][10][10], int dailyPercent[][10],
    int & recipetotal, char recipes[][50], char reciIngred[][10][100])
{
    ofstream fout, fout2;
    fout.open("Labels.txt");
    fout2.open("Recipes.txt");

    cout << "Saving function" << endl;

    fout << labeltotal << endl;
    for (int i = 0; i < labeltotal; i++)
    {
        fout << itemName[i] << endl;
        for (int j = 0; j < 10; j++)
        {
            fout << nutriIngred[i][j] << " ";
        }
        fout << endl;
        for (int n = 0; n < 26; n++)
        {
            fout << component[n] << " " << measurement[i][n] << " " << metric[i][n] << " " << dailyPercent[i][n] << endl;
        }
        fout << endl;
    }

    fout2 << recipetotal << endl;
    for (int k = 0; k < recipetotal; k++)
    {
        fout2 << recipes[k] << " ";
        for (int l = 0; l < 10; l++)
        {
            fout2 << reciIngred[k][l] << " ";
        }
        fout2 << endl;
    }
    fout2 << endl;

    // close files
    fout.close();
    fout2.close();
    return;
}

Nutrition Labels

In order:
* labeltotal
* itemName
* nutriIngred
* Component measurement metric dailyPercent

1
Cereal
Wheat, milk
calories 120 kcals -1
fatCalories 15 kcals 2
totalFat 1.5 g 2
satFat 2 g 3
transFat 3 g 4
polyFat 4 g 5
monoFat 5 g 6
cholesterol 6 g 7
sodium 7 g 8
potassium 9 g 10
totalCarbs 10 g 11
dietaryFiber 11 g 12
sugar 12 g 13
otherCarbs 13 g 14
protein 14 g 15
vitaminA 15 g 16
vitaminC 16 g 17
calcium 17 g 18
iron 18 g 19
thiamine 19 g 20
riboflavin 20 g 21
niacin 21 g 22
vitaminB6 22 g 23
folicAcid 23 g 24
phosphorus 24 g 25
zinc 25 g 26

Recipes
* In order:
* recipetotal
* recipes reciIngred

10
Tacos tortilla, lettuce, cheese, meat #
Doritos cheese powder, chip #
Salad lettuce #
Tostitos tortilla, salsa #
Pizza cheese, pepperoni #
Pepsi co2 #
Coke co2 #
Sprite co2, lime #
Pretzels salt, pretzel #
\$\endgroup\$
  • 1
    \$\begingroup\$ Please add the main function to your code, so that we can actually run your program during the review. \$\endgroup\$ – Roland Illig May 11 '19 at 4:25
  • 1
    \$\begingroup\$ I am facing problems when I run past void menu What kind of problems? Does this work? \$\endgroup\$ – yuri May 11 '19 at 5:34
  • \$\begingroup\$ I'm saying that some of the void functions past menu don't work like addFile. \$\endgroup\$ – Axrinn May 11 '19 at 5:36
1
\$\begingroup\$

Looking at your code, it's indeed obvious that you are still learning C++. Actually, I call it C with some sparkles of C++, as I recognize several constructs that one tries to avoid in C++ in favor of better encapsulated constructs. I'll point them out later.

First of all, I like to congratulate you for your naming. I barely see beginners that take the time to choose good names for the variables. All companies have IDEs that can help you type these, so keep up the good work and use good names.

That said, there were a few things that I wouldn't consider good C++: char recipes[][50] to name one. From what I can deduce, this should be std::vector<std::string>. This can prevent a lot of confusion, has a dynamic size (you can prealloc) and the std::string part of it allows you to simply a lot of string operations.

Secondly, I'm noticing that you have several variables that go hand-in-hand. itemName, nutriIngred ... Even in C, one would recommend putting this in structs. In C++, we use classes/structs. (They are almost the same, however, C++ allows you to put methods to both structs and classes).

For example:

class Ingredient
{
    std::string name;
    std::string metric;
    double measurement;
    int dailyPercent;
};
class Recipe
{
    std::string name;
    std::vector<Ingredient> ingredients;
};

I suggest you already start by trying to incorporate these classes, so the data is more structured. Once we have that, you can post a second iteration, in which we can look at splitting the functions into smaller logical blocks.

|improve this answer|||||
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.