I have a project where I would develop a nutrition label and a recipe list in a C++ class. I am facing problems with these specific functions. The choice value is when the user decided earlier if they would like to work with nutrition labels or recipes.
#include <iostream> #include <iomanip> #include <fstream> // Code for manually adding to each arrays 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; } // adding a file to the bottom of each array 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; } // editing an array element 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; } // deleting an array element 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; } // saving the arrays to the original files 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; }
Finding which line to work on
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 }
Nutrition Labels
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
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 # ```