Code:
#include <string.h>
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
struct ingredient
{
int quantity;
char name[41];
};
struct instruction
{
char instr[81];
};
struct recipe
{
char name[71], type[31], dateAdded[9];
ingredient ingredients[26];
instruction instructions[26];
int difficulty, peopleServed, numIngredient, numInstructions;
};
void addRecipe(int *current, recipe *aryCurr);
void deleteRecipe(int *current, recipe *aryCurr);
void findRecipe(int *current, recipe *aryCurr);
void viewRecipe(int *current, recipe *aryCurr);
void main()
{
recipe Recipes[100], *currRecipe;
int currentRecipe=0, *ptrCurrent, choice =0, x,g;
//LOAD RECIPES
ifstream CoolFile("C:\\recipes.dat");
if (CoolFile)
{
CoolFile >> currentRecipe;
for (x =0; x <=currentRecipe; x++)
{
CoolFile.getline(Recipes[x].name, 70);
CoolFile.getline(Recipes[x].type, 30);
CoolFile >> Recipes[x].difficulty;
CoolFile >> Recipes[x].peopleServed;
CoolFile.getline(Recipes[x].dateAdded, 8);
CoolFile >> Recipes[x].numIngredient;
CoolFile >> Recipes[x].numInstructions;
for (g=0;g <=((Recipes[x].numIngredient)-1);g++)
{
CoolFile.getline(Recipes[x].ingredients[g].name, 40);
CoolFile >> Recipes[x].ingredients[g].quantity;
}
for (g=0;g <=((Recipes[x].numInstructions)-1);g++)
{
CoolFile.getline(Recipes[x].instructions[g].instr, 80);
}
}
CoolFile.close();
}
else
{
cout << "\nFile couldn't be loaded... creating new.\n";
CoolFile.close();
}
/*
//DEBUG INFO
strcpy(Recipes[0].dateAdded, "04/18/87");
strcpy(Recipes[0].name, "Cookies");
strcpy(Recipes[0].type, "Cookie");
Recipes[0].difficulty = 2;
Recipes[0].peopleServed = 3;
Recipes[0].numIngredient = 2;
Recipes[0].numInstructions = 2;
strcpy(Recipes[0].ingredients[0].name, "Cow");
Recipes[0].ingredients[0].quantity = 2;
strcpy(Recipes[0].ingredients[1].name, "Donkey");
Recipes[0].ingredients[1].quantity = 3;
strcpy(Recipes[0].instructions[0].instr, "Kill me");
strcpy(Recipes[0].instructions[1].instr, "And make this program work.");
//DEBUG OVER
*/
cout << "ERIK'S RECIPE PROGRAM\n-------------------------------------------\n";
cout << "\n1. Add a recipe\n2. Delete a recipe\n3. Find/View a recipe\n4. Exit\n\n";
cin >> choice;
switch (choice)
{
case 1:
currentRecipe++;
currRecipe = &Recipes[currentRecipe];
ptrCurrent = ¤tRecipe;
addRecipe(ptrCurrent, currRecipe);
main();
break;
case 2:
//deleteRecipe();
break;
case 3:
currRecipe = &Recipes[0];
ptrCurrent = ¤tRecipe;
viewRecipe(ptrCurrent, currRecipe);
break;
case 4:
cout << "\n\n\n\n Are you sure you want to exit? (Y/N)> ";
char chooser;
cin >> chooser;
cout << "\n";
if (chooser == 'y'|| chooser == 'Y')
exit(1);
else
cout << "\n\n\n\n\n\n";
main();
break;
default:
cout << "\n\n\n\n\n\n";
main();
break;
}
}
void addRecipe(int *current, recipe *aryCurr)
{
char junk[3];
cout << "\n\n\n\t\t--Add a recipe--\n\n";
cout << "Please type the name of the recipe:\n";
cin.getline(junk, 2);
cin.getline(aryCurr->name, 70);
cout << "\nNow please enter the recipe type:\n";
cin.getline(aryCurr->type, 30);
cout << "\nPlease enter the difficulty of the recipe from 1-10\n";
while (aryCurr->difficulty < 0 || aryCurr->difficulty > 10)
cin >> aryCurr->difficulty;
cout << "\nNow please enter the amount of people served by the recipe:\n";
aryCurr->peopleServed = -1;
while (aryCurr->peopleServed < 1)
cin >> aryCurr->peopleServed;
strcpy(aryCurr->dateAdded, "04/18/87");
cout << "Please type the ingredients. Type \"*\" to stop.\n";
int numberOfIngredients=0, numberOfInstructions=0, amount =0;
char ingredient[41], instruction[81];
strcpy(ingredient, "NULL");
strcpy(instruction, "NULL");
while (strcmp(ingredient, "*") != 0 || numberOfIngredients < 26)
{
cout << "Name of ingredient?\n";
cin.getline(junk, 2);
cin.getline(ingredient, 40);
if(!strcmp(ingredient, "*"))
break;
cout << "Quantity of ingredient?\n";
cin >> amount;
strcpy(aryCurr->ingredients[numberOfIngredients].name, ingredient);
aryCurr->ingredients[numberOfIngredients].quantity = amount;
numberOfIngredients++;
}
cin.getline(junk, 2);
while (strcmp(instruction, "*") != 0 || numberOfInstructions < 26)
{
cout << "\nPlease type the instruction\n";
cin.getline(instruction, 80);
if(!strcmp(instruction, "*"))
break;
strcpy(aryCurr->instructions[numberOfInstructions].instr, instruction);
numberOfInstructions++;
}
aryCurr->numIngredient = numberOfIngredients +1;
aryCurr->numInstructions = numberOfInstructions +1;
int g;
//WRITE FILE
ofstream CoolFile("C:\\recipes.dat");
if (CoolFile)
{
CoolFile << aryCurr->name << endl;
CoolFile << aryCurr->type << endl;
CoolFile << aryCurr->difficulty << endl;
CoolFile << aryCurr->peopleServed << endl;
CoolFile << aryCurr->dateAdded << endl;
CoolFile << aryCurr->numIngredient << endl;
CoolFile << aryCurr->numInstructions << endl;
for (g=0;g <=numberOfIngredients;g++)
{
CoolFile << aryCurr->ingredients[g].name << endl;
CoolFile << aryCurr->ingredients[g].quantity << endl;
}
for (g=0;g <=numberOfInstructions;g++)
{
CoolFile << aryCurr->instructions[g].instr << endl;
}
CoolFile.seekp(0);
CoolFile << *current << " " << endl;
}
}
CoolFile.close();
cin.ignore(INT_MAX,'\n');
main();
}
void viewRecipe(int *current, recipe *aryCurr)
{
int choice, x, listed=-1, listedKey[16], recipeChoice = 20, searchDifficulty;
char chooser, junk[3];
cout << "\n\n\n\t\t--Find/View a recipe--\n\n";
cout << "1. View by name\n2. View by ID\n3. View by type\n4. View by difficulty\n5. Exit\n";
cin >> choice;
switch (choice)
{
case 1:
char *searchName, junk[3];
searchName = (char*)malloc(81);
cout << "\nPlease type the name of the recipe:\n";
cin.ignore(INT_MAX,'\n');
cin.getline(searchName, 80);
cin.ignore(INT_MAX,'\n');
for(x=0;x <= *current;x++)
{
if (strstr(strupr((aryCurr + x)->name), strupr(searchName)))
{
listed++;
cout << "\n" << listed+1 << ": " << aryCurr->name;
listedKey[listed] = (aryCurr+x)->id;
if(listed > 15)
{
cout << "\nContinue searching? (Y/N)";
cin >> chooser;
cin.ignore(INT_MAX,'\n');
if (chooser=='y' || chooser =='Y')
{
listed = 0;
cout << "\n\n\n\n\n\n\n\n\n\n";
}
else
break;
}
}
}
if (listed ==-1)
{
cout << "\nNo recipes matched!\n\n\n\n\n\n";
main();
}
cout << "\nType the number of recipe to view\n";
while (recipeChoice > (listed+1) || recipeChoice < 1)
cin >> recipeChoice;
cin.ignore(INT_MAX,'\n');
aryCurr += listedKey[recipeChoice-1];
cout << "\n\n\nID: " << aryCurr->id;
cout << "\nName: " << aryCurr->name;
cout << "\nType: " << aryCurr->type;
cout << "\nDifficulty: " << aryCurr->difficulty;
cout << "\nDate Added: " << aryCurr->dateAdded;
cout << "\nPeople Served: " << aryCurr->peopleServed;
cout << "\nNumber of ingredients: " << aryCurr->numIngredient;
cout << "\nNumber of instructions: " << aryCurr->numInstructions;
cout << "\n\nPress enter to see the ingredients\n";
cin.getline(junk,2);
cout << "\n\n # Ingredient\n------------------------------------------------\n";
for (x=0; x <= (aryCurr->numIngredient -1); x++)
{
cout << "[" << aryCurr->ingredients[x].quantity << "] " << aryCurr->ingredients[x].name << "\n";
}
cout << "\n\nPress enter to see the instructions\n";
cin.getline(junk,2);
cout << "\n\t\tINSTRUCTIONS\n-------------------------------------------------\n";
for (x=0; x <= (aryCurr->numInstructions -1); x++)
{
cout << "*Step " << x+1 << ")" << " " << aryCurr->instructions[x].instr << "\n";
}
cout << "\n\nPress enter to return to main menu.\n\n\n\n\n\n\n\n";
cin >> junk;
cin.ignore(INT_MAX,'\n');
main();
break;
case 2:
int searchID;
cout << "\nPlease type the recipe ID:\n";
cin >> searchID;
cin.ignore(INT_MAX,'\n');
if (searchID > *current)
{
cout << "No such ID exists!!";
main();
}
aryCurr += searchID;
cout << "\n\n\nID: " << aryCurr->id;
cout << "\nName: " << aryCurr->name;
cout << "\nType: " << aryCurr->type;
cout << "\nDifficulty: " << aryCurr->difficulty;
cout << "\nDate Added: " << aryCurr->dateAdded;
cout << "\nPeople Served: " << aryCurr->peopleServed;
cout << "\nNumber of ingredients: " << aryCurr->numIngredient;
cout << "\nNumber of instructions: " << aryCurr->numInstructions;
cout << "\n\nPress enter to see the ingredients\n";
cin.getline(junk,2);
cout << "\n\n # Ingredient\n------------------------------------------------\n";
for (x=0; x <= (aryCurr->numIngredient -1); x++)
{
cout << "[" << aryCurr->ingredients[x].quantity << "] " << aryCurr->ingredients[x].name << "\n";
}
cout << "\n\nPress enter to see the instructions\n";
cin.getline(junk,2);
cout << "\n\t\tINSTRUCTIONS\n-------------------------------------------------\n";
for (x=0; x <= (aryCurr->numInstructions -1); x++)
{
cout << "*Step " << x+1 << ")" << " " << aryCurr->instructions[x].instr << "\n";
}
cout << "\n\nPress enter to return to main menu.\n\n\n\n\n\n\n\n";
cin >> junk;
cin.ignore(INT_MAX,'\n');
main();
break;
case 3:
char *searchType;
searchType = (char*)malloc(31);
cout << "\nPlease enter the type of the recipe:\n";
cin.ignore(INT_MAX,'\n');
cin.getline(searchType, 30);
cin.ignore(INT_MAX,'\n');
for(x=0;x <= *current;x++)
{
if (strstr(strupr((aryCurr + x)->type), strupr(searchType)))
{
listed++;
cout << "\n" << listed+1 << ": " << (aryCurr+x)->type << "|" << (aryCurr+x)->name;
listedKey[listed] = (aryCurr+x)->id;
if(listed > 15)
{
cout << "\nContinue searching? (Y/N)";
cin >> chooser;
if (chooser=='y' || chooser =='Y')
{
listed = 0;
cout << "\n\n\n\n\n\n\n\n\n\n";
}
else
break;
}
}
}
if (listed ==-1)
{
cout << "\nNo recipes matched!\n\n\n\n\n\n";
main();
}
cout << "\nType the number of recipe to view\n";
while (recipeChoice > (listed+1) || recipeChoice < 1)
cin >> recipeChoice;
cin.ignore(INT_MAX,'\n');
aryCurr += listedKey[recipeChoice-1];
cout << "\n\n\nID: " << aryCurr->id;
cout << "\nName: " << aryCurr->name;
cout << "\nType: " << aryCurr->type;
cout << "\nDifficulty: " << aryCurr->difficulty;
cout << "\nDate Added: " << aryCurr->dateAdded;
cout << "\nPeople Served: " << aryCurr->peopleServed;
cout << "\nNumber of ingredients: " << aryCurr->numIngredient;
cout << "\nNumber of instructions: " << aryCurr->numInstructions;
cout << "\n\nPress enter to see the ingredients\n";
cin.getline(junk,2);
cout << "\n\n # Ingredient\n------------------------------------------------\n";
for (x=0; x <= (aryCurr->numIngredient -1); x++)
{
cout << "[" << aryCurr->ingredients[x].quantity << "] " << aryCurr->ingredients[x].name << "\n";
}
cout << "\n\nPress enter to see the instructions\n";
cin.getline(junk,2);
cout << "\n\t\tINSTRUCTIONS\n-------------------------------------------------\n";
for (x=0; x <= (aryCurr->numInstructions -1); x++)
{
cout << "*Step " << x+1 << ")" << " " << aryCurr->instructions[x].instr << "\n";
}
cout << "\n\nPress enter to return to main menu.\n\n\n\n\n\n\n\n";
cin.ignore(INT_MAX,'\n');
cin >> junk;
cin.ignore(INT_MAX,'\n');
main();
break;
case 4:
//int searchDifficulty=-1;
cout << "\nPlease type the desired difficulty:\n";
while (searchDifficulty < 1 || searchDifficulty > 10)
cin >> searchDifficulty;
cin.ignore(INT_MAX,'\n');
for(x=0;x <= *current;x++)
{
if ((aryCurr+x)->difficulty = searchDifficulty)
{
listed++;
cout << "\n" << listed+1 << ": " << aryCurr->name;
listedKey[listed] = (aryCurr+x)->id;
if(listed > 15)
{
cout << "\nContinue searching? (Y/N)";
cin >> chooser;
if (chooser=='y' || chooser =='Y')
{
listed = 0;
cout << "\n\n\n\n\n\n\n\n\n\n";
}
else
break;
}
}
}
if (listed ==-1)
{
cout << "\nNo recipes matched!\n\n\n\n\n\n";
main();
}
cout << "\nType the number of recipe to view\n";
while (recipeChoice > (listed+1) || recipeChoice < 1)
cin >> recipeChoice;
cin.ignore(INT_MAX,'\n');
aryCurr += listedKey[recipeChoice-1];
cout << "\n\n\nID: " << aryCurr->id;
cout << "\nName: " << aryCurr->name;
cout << "\nType: " << aryCurr->type;
cout << "\nDifficulty: " << aryCurr->difficulty;
cout << "\nDate Added: " << aryCurr->dateAdded;
cout << "\nPeople Served: " << aryCurr->peopleServed;
cout << "\nNumber of ingredients: " << aryCurr->numIngredient;
cout << "\nNumber of instructions: " << aryCurr->numInstructions;
cout << "\n\nPress enter to see the ingredients\n";
cin.getline(junk,2);
cout << "\n\n # Ingredient\n------------------------------------------------\n";
for (x=0; x <= (aryCurr->numIngredient -1); x++)
{
cout << "[" << aryCurr->ingredients[x].quantity << "] " << aryCurr->ingredients[x].name << "\n";
}
cout << "\n\nPress enter to see the instructions\n";
cin.getline(junk,2);
cout << "\n\t\tINSTRUCTIONS\n-------------------------------------------------\n";
for (x=0; x <= (aryCurr->numInstructions -1); x++)
{
cout << "*Step " << x+1 << ")" << " " << aryCurr->instructions[x].instr << "\n";
}
cout << "\n\nPress enter to return to main menu.\n\n\n\n\n\n\n\n";
cin.ignore(INT_MAX,'\n');
cin >> junk;
cin.ignore(INT_MAX,'\n');
main();
break;
default:
main();
break;
}
}