Вы находитесь на странице: 1из 5

//Joshua Major

#include<iostream>
#include<string>
#include<fstream>
//In this program, a list is used and modified from a file.
//After the user selects what file they want to open, they
//have a menu of options to select from to access and do
//something with the list. The options are to view the list,
//search the list, add an entry to the list, delete an entry
//to the list, and leave the list. 31 is the maximum around
//of entries that are allowed. When the user decides to add,
//delete, or search an entry, the artifact is searched by name.
// Once the name is found, the rest of the information on that
// artifact follows it. When you input the letter q, the console
//exits.
int ART_ARRAY_SIZE =0; //initializes array size to 0
using namespace std;
struct art
//My struct along with its variables
{
string name;
string month;
int year;
string fName;
string lName;
};
void View( art artifact[]); //4 lines of function prodotypes
void Search(art artifact[]);
void Add(art artifact[]);
void Delete(art artifact[]);
int main()
{
art artifact[31];
string temp;

//Max number of entries

char menu;
int i=0;
ifstream inFile;
string FileName;
cout<<"Please select a file to open: "<<endl;
cin>>FileName;
//inputs filename
inFile.open(FileName.c_str());
while(!inFile.eof())
//while not end of file
{
getline(inFile,artifact[i].name, '\n');

//The 5 artifact details

getline(inFile,artifact[i].month, '\n');
getline(inFile,temp, '\n');
artifact[i].year = atoi(temp.c_str());
getline(inFile,artifact[i].fName, '\n');
getline(inFile,artifact[i].lName, '\n');
i++;
ART_ARRAY_SIZE ++;
}
do
{
cout<<"***************************"<<endl; //menu
cout<<" Artifact Menu \n\n"<<endl;
cout<<"* View List
- V "<<endl;
cout<<"* Search
- S "<<endl;
cout<<"* Add Artifact
- A "<<endl;
cout<<"* Delete Artifact - D "<<endl;
cout<<"* Quit
- Q "<<endl;
cout<<"***************************"<<endl;
cout<< '\n' << '\n' << "Select an option: ";
cin>>menu;
cin.ignore();
switch(menu)
//switch statement for function calls
{
case 'V':
case 'v':
View(artifact);
break;

case 'S':
case 's':
Search(artifact);
break;
case 'A':
case 'a':
Add(artifact);
break;
case'D':
case'd':
Delete(artifact);
break;
default:
cout<<"\nInvalid Request.\n"<<endl;
}
}while(menu!='q' && menu!='Q' );
ofstream outfile;
outfile.open(FileName.c_str());
for(int i =0; i< ART_ARRAY_SIZE; i++)
{
outfile <<artifact[i].name<<endl;
outfile <<artifact[i].month<<endl;
outfile <<artifact[i].year<<endl;
outfile <<artifact[i].fName<<endl;
outfile <<artifact[i].lName;
if(i<ART_ARRAY_SIZE-1)
{
outfile<<endl;
}
}
return 0;
}
void View(art artifact[])
{
for(int count=0;count<ART_ARRAY_SIZE;count++)
{
cout<<artifact[count].name<<endl;
cout<<artifact[count].month<<endl;
cout<<artifact[count].year<<endl;
cout<<artifact[count].fName<<endl;
cout<<artifact[count].lName<<endl;
cout<<endl;
}
}
void Search(art artifact[])

{
string item;
int counter=0;
cout<<"Name an item: ";
getline(cin,item);
int flag=0;
for(int i=0;i<ART_ARRAY_SIZE;i++)
{
if(item==artifact[i].name) //tries to find match from name input
{
flag=1;
cout<<artifact[i].name<<endl; //the 5 details from the artifact found
cout<<artifact[i].month<<endl;
cout<<artifact[i].year<<endl;
cout<<artifact[i].fName<<endl;
cout<<artifact[i].lName<<endl;
}}
if(flag==0)
{
cout<<"No match found."<<endl;
}
}
void Add(art artifact[])
{
string temp;
cout<<"Enter artifact name: ";
//User inputs details of artifact that is wanted
getline(cin,artifact[ART_ARRAY_SIZE].name, '\n');
cout<<"Enter artifact month: ";
getline(cin,artifact[ART_ARRAY_SIZE].month, '\n');
cout<<"Enter artifact year: ";
getline(cin,temp,'\n');
artifact[ART_ARRAY_SIZE].year = atoi(temp.c_str());
cout<<"Enter artifact owner's first name: ";
getline(cin,artifact[ART_ARRAY_SIZE].fName, '\n');
cout<<"Enter artifact owner's last name: ";
getline(cin,artifact[ART_ARRAY_SIZE].lName, '\n');
cout<<endl<<endl;
cout<<"Artifact:"<<endl;
cout<<artifact[ART_ARRAY_SIZE].name<<endl;
cout<<artifact[ART_ARRAY_SIZE].month<<" "<<artifact[ART_ARRAY_SIZE].year+10<<endl;
cout<<artifact[ART_ARRAY_SIZE].fName<<" "<<artifact[ART_ARRAY_SIZE].lName<<endl;
ART_ARRAY_SIZE++;
system("PAUSE");
}
void Delete(art artifact[])
{

int flag=0;
string item;
cout<<"Which artifact do you want to delete?"<<endl; //asks the user what to delete
getline(cin,item);
for(int x=0; x<=ART_ARRAY_SIZE; x++)
{
if(item==artifact[x].name)
{
ART_ARRAY_SIZE--;
artifact[x].name= artifact[ART_ARRAY_SIZE].name; //removes that item
artifact[x].month= artifact[ART_ARRAY_SIZE].month;
artifact[x].year= artifact[ART_ARRAY_SIZE].year;
artifact[x].fName= artifact[ART_ARRAY_SIZE].fName;
artifact[x].lName= artifact[ART_ARRAY_SIZE].lName;
flag=1;
}
}
if(flag==0)
{
cout<<"There is no artifact founder under this name."<<endl;
}
}

Вам также может понравиться