Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <stdlib.h>
- #include <iomanip>
- #include <conio.h>
- using namespace std;
- const int _name = 0;
- const int _a = 1;
- const int _b = 2;
- const int _c = 3;
- const int _d = 4;
- const int _e = 5;
- const int _f = 6;
- int answer;
- class record
- {
- string name,a,b,c,d,e,f;
- public:
- string get_var(int id)
- {
- switch (id)
- {
- case _name:
- return name;
- case _a:
- return a;
- case _b:
- return b;
- case _c:
- return c;
- case _d:
- return d;
- case _e:
- return e;
- case _f:
- return f;
- }
- }
- void set_var(string var, int id)
- {
- switch (id)
- {
- case _name:
- name=var;
- break;
- case _a:
- a=var;
- break;
- case _b:
- b=var;
- break;
- case _c:
- c=var;
- break;
- case _d:
- d=var;
- break;
- case _e:
- e=var;
- break;
- case _f:
- f=var;
- break;
- }
- }
- };
- void split_line(record *row, string line)
- {
- size_t len=0;
- for(int i=0;i<7;i++)
- {
- len=line.find(" ");
- row->set_var(line.substr(0,len),i);
- line=line.substr(len+1,line.length());
- }
- }
- int main()
- {
- int x=0;
- int n=49;
- int input;
- while(x!=1)
- {
- cout << "Welcome to the McDonald's Food Database" << endl;
- cout << "Press 1 view database" << endl;
- cout << "Press 2 view fields" << endl;
- cout << "Press 3 for search" << endl;
- cout << "Press 4 for adding" << endl;
- cout << "Press 5 to exit"<< endl;
- cin >> answer;
- system("cls");
- vector <record> records;
- record row;
- ifstream data("McDonalds.txt");
- if (data.good())
- {
- while(!data.eof())
- {
- string line;
- getline(data,line);
- split_line(&row,line);
- records.push_back(row);
- }
- if (answer==1)
- {
- for (int a=0; a<n+1; a++)
- {
- cout<<setw(32)<<records[a].get_var(_name)<<setw(5)<<records[a].get_var(_a)<<setw(5)<<records[a].get_var(_b)<<setw(5)<<records[a].get_var(_c)<<setw(5)<<records[a].get_var(_d)<<setw(5)<<records[a].get_var(_e)<<setw(5)<<records[a].get_var(_f)<<endl;
- }
- cout<<"press esc to return to menu"<<endl;
- do
- {
- input = getch();
- }while(input !=27);
- }
- else if (answer==2)
- {
- int a=0;
- cout<<"press enter to edit record"<<endl;
- cout<<"press esc to return to menu"<<endl;
- do
- {
- input = getch();
- if (a<=n)
- {
- if (input==13)
- {
- system("cls");
- a=a-1;
- int alter;
- string change;
- cout<<"Name: "<<records[a].get_var(_name)<<endl;
- cout<<"Calories: "<<records[a].get_var(_a)<<endl;
- cout<<"Fat from Calories: "<<records[a].get_var(_b)<<endl;
- cout<<"Fat (g): "<<records[a].get_var(_c)<<endl;
- cout<<"Saturated Fat: "<<records[a].get_var(_d)<<endl;
- cout<<"Trans Fat: "<<records[a].get_var(_e)<<endl;
- cout<<"Cholesterol: "<<records[a].get_var(_f)<<endl;
- cout<<""<<endl;
- cout<<"Please specify what you would like to alter:"<<endl;
- cout<<"1. Name"<<endl;
- cout<<"2. Calories"<<endl;
- cout<<"3. Fat from Calories"<<endl;
- cout<<"4. Fat (g)"<<endl;
- cout<<"5. Saturated Fat"<<endl;
- cout<<"6. Trans Fat"<<endl;
- cout<<"7. Cholesterol"<<endl;
- cout<<""<<endl;
- cin>>alter;
- cout<<"specify what you would like to replace with:"<<endl;
- cin>>change;
- ofstream myfile ("McDonalds.txt");
- if (myfile.is_open())
- {
- if(alter==1)
- {
- records[a].get_var(_name)=change;
- }
- else if(alter==2)
- {
- records[a].get_var(_a)=change;
- }
- else if(alter==3)
- {
- records[a].get_var(_b)=change;
- }
- else if(alter==4)
- {
- records[a].get_var(_c)=change;
- }
- else if(alter==5)
- {
- records[a].get_var(_d)=change;
- }
- else if(alter==6)
- {
- records[a].get_var(_e)=change;
- }
- else if(alter==7)
- {
- records[a].get_var(_f)=change;
- }
- myfile.close();
- }
- }
- if (input==80)
- {
- cout<<"Name: "<<records[a].get_var(_name)<<endl;
- cout<<"Calories: "<<records[a].get_var(_a)<<endl;
- cout<<"Fat from Calories: "<<records[a].get_var(_b)<<endl;
- cout<<"Fat (g): "<<records[a].get_var(_c)<<endl;
- cout<<"Saturated Fat: "<<records[a].get_var(_d)<<endl;
- cout<<"Trans Fat: "<<records[a].get_var(_e)<<endl;
- cout<<"Cholesterol: "<<records[a].get_var(_f)<<endl;
- cout<<""<<endl;
- a=a+1;
- if(a>n)
- {
- a=a-1;
- }
- }
- else if(input==72)
- {
- cout<<"Name: "<<records[a].get_var(_name)<<endl;
- cout<<"Calories: "<<records[a].get_var(_a)<<endl;
- cout<<"Fat from Calories: "<<records[a].get_var(_b)<<endl;
- cout<<"Fat (g): "<<records[a].get_var(_c)<<endl;
- cout<<"Saturated Fat: "<<records[a].get_var(_d)<<endl;
- cout<<"Trans Fat: "<<records[a].get_var(_e)<<endl;
- cout<<"Cholesterol: "<<records[a].get_var(_f)<<endl;
- cout<<""<<endl;
- a=a-1;
- if(a<0)
- {
- a=0;
- }
- }
- else if(input!=80 || 72)
- {
- system("cls");
- }
- }
- }while(input !=27);
- }
- else if (answer==3)
- {
- string chorroberru;
- int y;
- size_t found;
- cout<<"search by name (replace spaces with dashes)"<<endl;
- cin >> chorroberru;
- cout<<setw(32)<<"NAME"<<" CAL "<<" CFAT "<<" FAT "<<"SAT "<<" TRAN "<<"CHOLESTEROL"<<endl;
- for(int i=0; i<(int)records.size(); i++)
- {
- found = records[i].get_var(_name).find(chorroberru);
- if (found!=string::npos)
- {
- cout<<setw(32)<<records[i].get_var(_name)<<setw(5)<<records[i].get_var(_a)<<setw(5)<<records[i].get_var(_b)<<setw(5)<<records[i].get_var(_c)<<setw(5)<<records[i].get_var(_d)<<setw(5)<<records[i].get_var(_e)<<setw(5)<<records[i].get_var(_f)<<endl;
- }
- }
- cout<<"press esc for exit"<<endl;
- do
- {
- input = getch();
- cout<<input;
- }while(input !=27);
- }
- else if (answer==4)
- {
- int edit;
- cout<<"Press 1 for Adding a new Record";
- cin>>edit;
- if(edit=1)
- {
- string first;
- int ca;
- int cafat;
- int tfat;
- int satu;
- int trans;
- int chole;
- cout<<"type what you would like to add"<<endl;
- cin>>first;
- cin>>ca;
- cin>>cafat;
- cin>>tfat;
- cin>>satu;
- cin>>trans;
- cin>>chole;
- ofstream myfile ("McDonalds.txt", ios::out | ios::app | ios::binary);
- if (myfile.is_open())
- {
- myfile << first<<" "<<ca<<" "<<cafat<<" "<<tfat<<" "<<satu<<" "<<trans<<" "<<chole<<endl;
- myfile.close();
- n=n+1;
- }
- else cout << "Unable to open file";
- }
- cout<<"press esc for exit"<<endl;
- do
- {
- input = getch();
- }while(input !=27);
- }
- else if (answer=5)
- {
- x=1;
- }
- system("cls");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement