Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created March 24, 2015 16:23
Show Gist options
  • Save krysseltillada/d96f69ba38bea50e100e to your computer and use it in GitHub Desktop.
Save krysseltillada/d96f69ba38bea50e100e to your computer and use it in GitHub Desktop.
a food type info storing exercise program in c++
/// a food type info storing exercise written in c++
/// if some peole encounter a exercise problem like this it may be helpful
/// this program will do its job properly according to the problem given
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int identifier;
int test;
int numberofitem = 0;
int counter = 1;
class Foodtype
{
private:
string typeoffood;
string basicfood;
string prepfood;
string basicfoodcatg[5] = {"dairy", "meat", "fruit", "vegetable", "grain"};
public:
Foodtype(string = "none", string = "none", string = "none");
void Displayitembasic();
void Displayitemprep();
void Inputitem();
void Modifyitem();
void Delitem();
int Inputcheckprep();
void Inputcheckbasic();
void Displaycheck();
};
Foodtype::Foodtype(string defaulttype, string defaultbasic, string defaultprep)
{
typeoffood = defaulttype;
basicfood = defaultbasic;
prepfood = defaultprep;
}
void Foodtype::Displayitemprep()
{
cout << "\ndisplay the info for food item: " << counter
<< "\ntype of food: " << typeoffood
<< "\nname of the prepared food: " << prepfood << endl;
return;
}
void Foodtype::Displayitembasic()
{
cout << "\ndisplay the info for food item: " << counter
<< "\ntype of food: " << typeoffood
<< "\nwhat type of basic food: " << basicfood << endl;
return;
}
void Foodtype::Inputitem()
{
int r = 1;
cout << "\nfor food item no: " << counter
<< "\nwhat type of food: prepared or basic: ";
cin >> typeoffood;
std::transform (typeoffood.begin(), typeoffood.end(), typeoffood.begin(), ::tolower);
if(typeoffood == "prepared"){
identifier = 1;
cout << "what kind of food: ";
cin >> prepfood;
}
else if(typeoffood == "basic"){
int m = 1;
while(m){
identifier = 2;
cout << "\ndairy, vegetable, meat, fruit, grain: ? ";
cin >> basicfood;
std::transform(basicfood.begin(), basicfood.end(), basicfood.begin(), ::tolower);
for(int c = 0; c < 5; c++)
{
if(basicfood == basicfoodcatg[c]){
return;
}
}
cout << "invalid input: " << endl;
}
}
else{
cout << "\ninvalid input: ";
}
return;
}
void Foodtype::Delitem()
{
char key;
cout << "\ndelete this food item number: y or n \n" << counter;
cin >> key;
while(1){
if(tolower(key) == 'y'){
typeoffood = "none";
basicfood = "none";
prepfood = "none";
break;
}
else if(tolower(key) == 'n'){
cout << "\ninfo was not deleted: " << endl;
break;
}
else{
cout << "\ninvalid input: " << endl;
continue;
}
}
return;
}
void Foodtype::Modifyitem()
{
Inputitem();
return;
}
void Foodtype::Displaycheck()
{
if(identifier == 1){
Displayitemprep();
}
else if(identifier == 2){
Displayitembasic();
}
else{
cout << "\nno info: " << endl;
}
return;
}
int main()
{
int n = 1;
int f = 1;
int key;
int x = 0;
cout << "types of food info storing program: \n";
cout << "\nenter the number of items to store: ";
cin >> numberofitem;
Foodtype fooditem[numberofitem];
while(f){
cout << "\nmain menu: \n"
<< "--------- \n"
<< "1.add a food item.\n"
<< "2.exit.\n"
<< endl;
cin >> key;
if(key == 1)
{
int l = 1;
int counterloop = 0;
int a = 1;
while(l)
{
fooditem[counterloop].Inputitem();
fooditem[counterloop].Displaycheck();
while(a){
int num;
cout << "\nmain menu\n "
<< "---------\n"
<< "1.add another item \n"
<< "2.modify\n"
<< "3.delete\n"
<< "4.display\n"
<< "5.exit\n" << endl;
cin >> num;
if(counterloop < numberofitem){
if(num == 1){
counterloop++;
counter++;
if(counterloop == numberofitem){
counterloop--;
while(1)
{
int key2;
cout << "full storage cannot store some info: " << endl;
cout << "\nmain menu\n "
<< "---------\n"
<< "1.add another item \n"
<< "2.modify\n"
<< "3.delete\n"
<< "4.display\n"
<< "5.exit\n" << endl;
cin >> key2;
if(key2 == 1){
continue;
}
else if(key2 == 2){
fooditem[counterloop].Modifyitem();
fooditem[counterloop].Displaycheck();
continue;
}
else if(key2 == 3){
fooditem[counterloop].Delitem();
fooditem[counterloop].Displaycheck();
continue;
}
else if(key2 == 4){
fooditem[counterloop].Displaycheck();
}
else if(key2 == 5){
cout << "exiting the program: " << endl;
return 0;
}
else{
cout << "invalid input: " << endl;
continue;
}
}
}
fooditem[counterloop].Inputitem();
fooditem[counterloop].Displaycheck();
continue;
}
else if(num == 2){
fooditem[counterloop].Modifyitem();
fooditem[counterloop].Displaycheck();
continue;
}
else if(num == 3){
fooditem[counterloop].Delitem();
fooditem[counterloop].Displaycheck();
continue;
}
else if(num == 4){
fooditem[counterloop].Displaycheck();
continue;
}
else if(num == 5)
{
cout << "program exit: ";
return 0;
}
else{
cout << "invalid input:" << endl;
continue;
}
}
else{
cout << "full: " << endl;
continue;
}
}
counter++;
}
}
else if(key == 2)
{
cout << "exiting the program: ";
f = 0;
break;
}
else{
cout << "invalid input: " << endl;
continue;
}
}
cout << "exiting the program: " << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment