1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;
void mainMenu();
void FoodInformation();
void FoodInfoInput(string FoodArray[][31]);
void FoodInfoOutput(string FoodArray[][31]);
void transform(std::string &str, int (*myFunction)(int));
bool add(bool &repeat,string FoodArray[8][31],int groupChoice, int &FoodNum);
bool sub(bool &repeat,string FoodArray[8][31],int groupChoice);
int main()
{
mainMenu();
system("pause");
return 0;
}
void mainMenu()
{
int choice;
cout << "Main Menu:" << endl
<< "1. Food" << endl;
cin >> choice;
switch (choice)
{
case 1:
FoodInformation();
break;
default:
cout << "Input Error. Please enter a valid action." << endl;
break;
}
}
void FoodInformation()
{
string FoodArray[8][31];
FoodArray[1][0]= "Fruits";
FoodInfoInput(FoodArray);
bool repeat;
do
{
repeat = false;
system("CLS");
int groupChoice;
int FoodNum= 1;
do
{
cout << "Food Group: " << endl
<< "1. Fruits " << endl
<< "Choose a Group: ";
cin >> groupChoice;
cin.ignore(255,'\n');
system("CLS");
if(groupChoice==1)
{
cout << "Fruits: " << endl;
while (FoodArray[1][FoodNum] != "")
{
cout << FoodArray[1][FoodNum] << endl;
FoodNum++;
}
break;
}
}while(true);
system("CLS");
if(!add(repeat,FoodArray,groupChoice,FoodNum))
sub(repeat,FoodArray,groupChoice);
}while(repeat);
}
void FoodInfoInput(string FoodArray[][31])
{
ifstream inFoodInfo;
inFoodInfo.open("FoodData_DoNotOpen.dat");
for (int count_= 1; count_ < 8; count_++)
{
for (int count= 1; FoodArray[count_][count- 1]!= ""; count++)
getline(inFoodInfo, FoodArray[count_][count]);
}
inFoodInfo.close();
}
void FoodInfoOutput(string FoodArray[][31])
{
ofstream outFoodInfo;
outFoodInfo.open("FoodData_DoNotOpen.dat");
for (int count_= 1; count_ < 8; count_++)
{
for (int count= 1; FoodArray[count_][count- 1]!= ""; count++)
outFoodInfo << FoodArray[count_][count] << endl;
}
outFoodInfo.close();
}
void transform(std::string &str, int (*myFunction)(int))
{
for(int i=0;i<str.size();++i)
str[i] = myFunction(str[i]);
}
bool add(bool &repeat,string FoodArray[8][31],int groupChoice, int &FoodNum)
{
string addFood;
cout << "Add a food (yes/ no) ";
getline(cin,addFood);
transform(addFood, toupper);
if (addFood == "YES")
{
repeat = true;
string FoodName;
cout << "Enter food name: ";
getline(cin, FoodName);
transform(FoodName, tolower);
FoodName[0]= toupper(FoodName[0]);
string FoodCheck;
cout << "Are you sure you wish to add \"" << FoodName << "\" to the menu? ";
getline(cin,FoodCheck);
transform(FoodCheck, toupper);
if (FoodCheck == "YES")
{
while (FoodArray[groupChoice][FoodNum] != "")
{
FoodNum++;
}
FoodArray[groupChoice][FoodNum]= FoodName;
}
FoodInfoOutput(FoodArray);
return true;
}
return false;
}
bool sub(bool &repeat,string FoodArray[8][31],int groupChoice)
{
string removeFood;
cout << "Remove a food (yes/ no) ";
getline(cin,removeFood);
transform(removeFood, toupper);
if (removeFood == "YES")// This is the misfunctional if statement
{
repeat = true;
string FoodName;
cout << "Enter food name: ";
getline(cin, FoodName);
transform(FoodName, tolower);
FoodName[0]= toupper(FoodName[0]);
for (int count = 0; count < 31; count++)// This is the misfunctional for statement
{
if (FoodArray[groupChoice][count] != FoodName && count == 30)
{
cout << "Could not find item with name \"" << FoodName << "\" on the menu" << endl;
}
if (FoodArray[groupChoice][count] == FoodName)
{
string FoodCheck;
cout << "Are you sure you wish to remove \"" << FoodName << "\" from the menu? ";
getline(cin,FoodCheck);
transform(FoodCheck, toupper);
if (FoodCheck == "YES")
{
while (FoodArray[groupChoice][count] != "")
{
FoodArray[groupChoice][count]= FoodArray[groupChoice][count+ 1];
count++;
}
}
FoodInfoOutput(FoodArray);
break;
}
}
return true;
}
return false;
}
|