Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************************************
- * CSCI 124 - ass2.cpp - Contains function definitions for pet database program
- * Gareth Woodorth- gw305
- * 29/8/12
- ***********************************************************************************/
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- // ============== Constants ==========================================
- const char cDataFileName[] = "pets.txt";
- const int cMaxRecs = 100;
- const int cMaxChars = 30;
- // ============= User Defined types ==================================
- enum StatusType{Lost,Found};
- enum PetType{Dog,Cat};
- enum GenderType{Male,Female,Unknown};
- struct AgeType{ int Yrs; int Mths;}; // -1 if unknown
- struct PetRecord{
- StatusType Status; // Lost or Found
- PetType Type; // Dog or Cat
- GenderType Gender; // Male, Female or Unknown
- char Breed[cMaxChars]; // Bread of dog or cat. eg Bull terrier
- AgeType Age; // Age in years and mths. (-1 -1 if unknown)
- char Colour[cMaxChars]; // Colour of pet. eg tan
- char Location[cMaxChars]; // Suburb where found or lost. eg Mt Keira
- char PhoneNo[cMaxChars]; // Ph. of person to contact
- };
- // ============= Global Data =========================================
- PetRecord *gPetRecs[cMaxRecs];
- int gNumRecs=0;
- // ============= Private Function Prototypes =========================
- void DisplayRecord(int i); // Displays record i on screen
- void lowercase(char str[]); // changes all strings to lowercase
- void DeleteRecord(int i);
- // ============= Public Function Definitions =========================
- void ReadFile()
- {
- ifstream fin;
- fin.open(cDataFileName);
- if (!fin.good()){
- cerr << "Could not open file!\n";
- exit(1);
- }
- gNumRecs=0;
- int i;
- for(i=0;i<cMaxRecs;i++){
- char Tmp[cMaxChars];
- fin >> Tmp;
- if(fin.fail())break;
- gPetRecs[i] = new PetRecord;
- switch(Tmp[0]){
- case'l': gPetRecs[i]->Status = Lost; break;
- case'f': gPetRecs[i]->Status = Found;break;
- }
- fin >> Tmp;
- switch(Tmp[0]){
- case'c': gPetRecs[i]->Type = Cat; break;
- case'd': gPetRecs[i]->Type = Dog; break;
- }
- fin >> Tmp;
- switch(Tmp[0]){
- case'm': gPetRecs[i]->Gender = Male; break;
- case'f': gPetRecs[i]->Gender = Female; break;
- default: gPetRecs[i]->Gender = Unknown; break;
- }
- fin.ignore(); //eat tailing '\n'
- fin.getline(gPetRecs[i]->Breed,cMaxChars);
- fin >> gPetRecs[i]->Age.Yrs>>gPetRecs[i]->Age.Mths;
- fin.ignore(); //eat tailing '\n'
- fin.getline(gPetRecs[i]->Colour,cMaxChars);
- fin.getline(gPetRecs[i]->Location,cMaxChars);
- fin.getline(gPetRecs[i]->PhoneNo,cMaxChars);
- }
- gNumRecs=i;
- fin.close();
- cout<< "\nThere are "<< gNumRecs <<" records in the Lost and Found Pet Database\n";
- }
- void DisplayRecs()
- {// Displays records one at a time
- for(int i=0;i<gNumRecs;i++){
- DisplayRecord(i);
- cout<<"Display next record (y/n)> ";
- char Ans;
- cin>>Ans;
- cout<<endl;
- if(Ans=='n') return;
- }
- }
- void AddRecord()
- {// Adds a new record to data file
- int i = gNumRecs;
- gPetRecs[i] = new PetRecord;
- char temp;
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
- cout << "+" << setfill(' ')<<setw(38)<< " Add Record to Database" << setfill(' ') <<setw(16) << "+" << endl;
- cout << "+" << setfill(' ')<<setw(40)<<" (Enter your pets details)" << setfill(' ') <<setw(14) << "+" << endl;
- cout << "+" << setfill(' ')<<setw(45)<<" (Enter ? or -1 if detail is unknown)" << setfill(' ') <<setw(9) << "+" << endl;
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
- cout <<"Is the pet lost or found? (l/f) =>";
- cin >> temp;
- switch(temp)
- {
- case 'l': gPetRecs[i]->Status = Lost;break;
- case 'f': gPetRecs[i]->Status = Found;break;
- default: cout << " incorrect status";
- }
- cout <<"Is the pet a dog or cat? (d/c) =>";
- cin >> temp;
- switch(temp)
- {
- case'c': gPetRecs[i]->Type = Cat; break;
- case'd': gPetRecs[i]->Type = Dog; break;
- default: cout << " incorrect type";
- }
- cout <<"What is the gender of the pet (m/f) =>";
- cin >> temp;
- switch(temp)
- {
- case'm': gPetRecs[i]->Gender = Male; break;
- case'f': gPetRecs[i]->Gender = Female; break;
- default: gPetRecs[i]->Gender = Unknown; break;
- }
- cout <<"What breed is the pet? =>";
- cin.ignore();
- cin.getline(gPetRecs[i]->Breed,cMaxChars, '\n');
- lowercase(gPetRecs[i]->Breed);
- cout <<"What age is the pet? (yy mm ) =>";
- cin >> gPetRecs[i]->Age.Yrs>>gPetRecs[i]->Age.Mths;
- cout <<"What colour is the pet? =>";
- cin.ignore();
- cin.getline(gPetRecs[i]->Colour,cMaxChars, '\n');
- lowercase(gPetRecs[i]->Colour);
- cout <<"In what suburb was the pet lost? =>";
- cin.getline(gPetRecs[i]->Location,cMaxChars, '\n');
- lowercase(gPetRecs[i]->Location);
- cout <<"What is your phone number? =>";
- cin.getline(gPetRecs[i]->PhoneNo,cMaxChars, '\n');
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
- cout <<"+" << setfill(' ')<<setw(48)<<"A new record has been added to the database"<< setfill(' ') <<setw(6) <<"+" <<endl;
- cout <<"+" << setfill(' ')<<setw(10)<< gNumRecs <<" records are in the database"<< setfill(' ') << setw(16) <<"+"<<endl;
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
- ofstream fout;
- fout.open(cDataFileName, ios::app);
- switch(gPetRecs[i]->Status)
- {
- case Lost: fout << "lost" << endl;break;
- case Found: fout << "found" << endl;break;
- }
- switch(gPetRecs[i]->Type)
- {
- case Cat: fout << "Cat" << endl;break;
- case Dog: fout << "Dog" << endl;break;
- }
- switch(gPetRecs[i]->Gender)
- {
- case Male: fout << "Male" << endl;break;
- case Female: fout << "Female" << endl;break;
- case Unknown: fout << "Unknown" << endl;break;
- }
- fout << gPetRecs[i]->Breed << endl;
- fout << gPetRecs[i]->Age.Yrs << " " << gPetRecs[i]->Age.Mths << endl;
- fout << gPetRecs[i]->Colour << endl;
- fout << gPetRecs[i]->Location << endl;
- fout << gPetRecs[i]->PhoneNo << endl;
- }
- void lowercase(char string[]) {
- for(int i = 0; string[i]; i++)
- string[i] = tolower(string[i]);
- }
- void SearchArray()
- {
- char temp;
- PetRecord searchdetails;
- bool search;
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
- cout << "+" << setfill(' ')<<setw(35)<< " Search Database" << setfill(' ') <<setw(19) << "+" << endl;
- cout << "+" << setfill(' ')<<setw(40)<<" (Enter your pets details)" << setfill(' ') <<setw(14) << "+" << endl;
- cout << "+" << setfill(' ')<<setw(45)<<" (Enter ? or -1 if detail is unknown)" << setfill(' ') <<setw(9) << "+" << endl;
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
- cout << "search lost or found pets(l/f)=>";
- cin >> temp;
- switch(temp)
- {
- case'l': searchdetails.Status = Lost; break;
- case'f': searchdetails.Status = Found;break;
- }
- cout << " search for cat or dog (c/d)=>";
- cin >> temp;
- switch(temp)
- {
- case'c': searchdetails.Type = Cat; break;
- case'd': searchdetails.Type = Dog; break;
- }
- cout << " search for a gender =>";
- cin >> temp;
- switch(temp)
- {
- case'm': searchdetails.Gender = Male; break;
- case'f': searchdetails.Gender = Female; break;
- default: searchdetails.Gender = Unknown; break;
- }
- cin.ignore();
- cout <<"What breed to search for? =>";
- cin.ignore();
- cin.getline(searchdetails.Breed,cMaxChars,'\n');
- lowercase(searchdetails.Breed);
- cout <<"What age to search for? (yy mm ) =>";
- cin >> searchdetails.Age.Yrs >> searchdetails.Age.Mths;
- cin.ignore();
- cout <<"What colour to search for? =>";
- cin.getline(searchdetails.Colour,cMaxChars, '\n');
- lowercase(searchdetails.Colour);
- cout <<"In what suburb was the pet lost? =>";
- cin.getline(searchdetails.Location,cMaxChars, '\n');
- lowercase(searchdetails.Location);
- for ( int i = 0; i < gNumRecs; i++)
- {
- search = true;
- if ( searchdetails.Status!= gPetRecs[i]->Status) search =false;
- if ( searchdetails.Type!= gPetRecs[i]->Type) search = false;
- switch(searchdetails.Gender){
- case Unknown: break;
- default: if(searchdetails.Gender != gPetRecs[i]->Gender){
- search = false;
- }
- }
- switch(searchdetails.Breed[0]){
- case '?': break;
- default: if(strcmp(gPetRecs[i]->Breed, searchdetails.Breed) !=0){
- search = false;
- }
- }
- switch(searchdetails.Age.Yrs){
- case -1: break;
- default: if(gPetRecs[i]->Age.Yrs != searchdetails.Age.Yrs){
- search = false;
- }
- }
- switch(searchdetails.Age.Mths){
- case -1: break;
- default: if(gPetRecs[i]->Age.Mths != searchdetails.Age.Mths){
- search = false;
- }
- }
- switch(searchdetails.Colour[0]){
- case '?': break;
- default: if(strcmp(gPetRecs[i]->Colour, searchdetails.Colour) !=0){
- search = false;
- }
- }
- switch(searchdetails.Location[0]){
- case '?': break;
- default: if(strcmp(gPetRecs[i]->Location, searchdetails.Location) !=0){
- search = false;
- }
- }
- for ( int i =0; i < gNumRecs;i++)
- if(search == false)
- DeleteRecord(i);
- }
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
- cout <<"+" << setfill(' ')<<setw(42)<<"There are" << gNumRecs << "pets in the database"<< setfill(' ') <<setw(12)<<"+"<<endl;
- cout <<"+" << setfill(' ')<<setw(42)<< "that match the search criteria"<< setfill(' ') << setw(12) <<"+"<<endl;
- cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
- }
- void CleanUp()
- {// Deletes all dynamic data in gPetRecs array
- }
- // ============= Private Functions Definitions =========================
- void DeleteRecord(int RecordNum)
- {
- for( int i = RecordNum; i < gNumRecs; i++)
- gPetRecs[i] = gPetRecs[i+1];
- gNumRecs--;
- }
- void DisplayRecord(int i)
- {// Displays record i on screen
- cout<<"Status: ";
- switch(gPetRecs[i]->Status){
- case Lost: cout << "lost\n"; break;
- case Found: cout << "found\n";break;
- }
- cout<<"Type : ";
- switch(gPetRecs[i]->Type){
- case Cat: cout << "cat\n";break;
- case Dog: cout << "dog\n";break;
- }
- cout<<"Gender: ";
- switch(gPetRecs[i]->Gender){
- case Male: cout << "male\n"; break;
- case Female: cout << "female\n";break;
- default: cout << "unknown\n";break;
- }
- cout<<"Breed: "<<gPetRecs[i]->Breed<<endl;
- cout<<"Age: "<<gPetRecs[i]->Age.Yrs<<" Yrs "<<gPetRecs[i]->Age.Mths<<" Mths\n";
- cout<<"Colour: "<<gPetRecs[i]->Colour<<endl;
- cout<<"Location: "<<gPetRecs[i]->Location<<endl;
- cout<<"Phone No: "<<gPetRecs[i]->PhoneNo<<endl;
- }
Add Comment
Please, Sign In to add comment