Guest User

Untitled

a guest
Dec 11th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 KB | None | 0 0
  1. /***********************************************************************************
  2.  
  3. * CSCI 124 - ass2.cpp - Contains function definitions for pet database program
  4.  
  5. * Gareth Woodorth- gw305
  6.  
  7. * 29/8/12
  8.  
  9. ***********************************************************************************/
  10.  
  11.  
  12.  
  13. #include <iostream>
  14.  
  15. #include <iomanip>
  16.  
  17. #include <fstream>
  18.  
  19. #include <cstdlib>
  20.  
  21. #include <cstring>
  22.  
  23. using namespace std;
  24.  
  25.  
  26.  
  27. // ============== Constants ==========================================
  28.  
  29.  
  30.  
  31. const char cDataFileName[] = "pets.txt";
  32.  
  33. const int cMaxRecs = 100;
  34.  
  35. const int cMaxChars = 30;
  36.  
  37.  
  38.  
  39.  
  40.  
  41. // ============= User Defined types ==================================
  42.  
  43.  
  44.  
  45. enum StatusType{Lost,Found};
  46.  
  47. enum PetType{Dog,Cat};
  48.  
  49. enum GenderType{Male,Female,Unknown};
  50.  
  51. struct AgeType{ int Yrs; int Mths;}; // -1 if unknown
  52.  
  53.  
  54.  
  55. struct PetRecord{
  56.  
  57. StatusType Status; // Lost or Found
  58.  
  59. PetType Type; // Dog or Cat
  60.  
  61. GenderType Gender; // Male, Female or Unknown
  62.  
  63. char Breed[cMaxChars]; // Bread of dog or cat. eg Bull terrier
  64.  
  65. AgeType Age; // Age in years and mths. (-1 -1 if unknown)
  66.  
  67. char Colour[cMaxChars]; // Colour of pet. eg tan
  68.  
  69. char Location[cMaxChars]; // Suburb where found or lost. eg Mt Keira
  70.  
  71. char PhoneNo[cMaxChars]; // Ph. of person to contact
  72.  
  73. };
  74.  
  75.  
  76.  
  77.  
  78.  
  79. // ============= Global Data =========================================
  80.  
  81.  
  82.  
  83. PetRecord *gPetRecs[cMaxRecs];
  84.  
  85. int gNumRecs=0;
  86.  
  87.  
  88.  
  89.  
  90.  
  91. // ============= Private Function Prototypes =========================
  92.  
  93.  
  94.  
  95. void DisplayRecord(int i); // Displays record i on screen
  96.  
  97. void lowercase(char str[]); // changes all strings to lowercase
  98.  
  99. void DeleteRecord(int i);
  100.  
  101.  
  102.  
  103.  
  104.  
  105. // ============= Public Function Definitions =========================
  106.  
  107.  
  108.  
  109. void ReadFile()
  110.  
  111. {
  112.  
  113. ifstream fin;
  114.  
  115. fin.open(cDataFileName);
  116.  
  117. if (!fin.good()){
  118.  
  119. cerr << "Could not open file!\n";
  120.  
  121. exit(1);
  122.  
  123. }
  124.  
  125. gNumRecs=0;
  126.  
  127. int i;
  128.  
  129. for(i=0;i<cMaxRecs;i++){
  130.  
  131. char Tmp[cMaxChars];
  132.  
  133. fin >> Tmp;
  134.  
  135. if(fin.fail())break;
  136.  
  137. gPetRecs[i] = new PetRecord;
  138.  
  139. switch(Tmp[0]){
  140.  
  141. case'l': gPetRecs[i]->Status = Lost; break;
  142.  
  143. case'f': gPetRecs[i]->Status = Found;break;
  144.  
  145. }
  146.  
  147. fin >> Tmp;
  148.  
  149. switch(Tmp[0]){
  150.  
  151. case'c': gPetRecs[i]->Type = Cat; break;
  152.  
  153. case'd': gPetRecs[i]->Type = Dog; break;
  154.  
  155. }
  156.  
  157. fin >> Tmp;
  158.  
  159. switch(Tmp[0]){
  160.  
  161. case'm': gPetRecs[i]->Gender = Male; break;
  162.  
  163. case'f': gPetRecs[i]->Gender = Female; break;
  164.  
  165. default: gPetRecs[i]->Gender = Unknown; break;
  166.  
  167. }
  168.  
  169. fin.ignore(); //eat tailing '\n'
  170.  
  171. fin.getline(gPetRecs[i]->Breed,cMaxChars);
  172.  
  173. fin >> gPetRecs[i]->Age.Yrs>>gPetRecs[i]->Age.Mths;
  174.  
  175. fin.ignore(); //eat tailing '\n'
  176.  
  177. fin.getline(gPetRecs[i]->Colour,cMaxChars);
  178.  
  179. fin.getline(gPetRecs[i]->Location,cMaxChars);
  180.  
  181. fin.getline(gPetRecs[i]->PhoneNo,cMaxChars);
  182.  
  183. }
  184.  
  185. gNumRecs=i;
  186.  
  187. fin.close();
  188.  
  189. cout<< "\nThere are "<< gNumRecs <<" records in the Lost and Found Pet Database\n";
  190.  
  191. }
  192.  
  193.  
  194.  
  195. void DisplayRecs()
  196.  
  197. {// Displays records one at a time
  198.  
  199. for(int i=0;i<gNumRecs;i++){
  200.  
  201. DisplayRecord(i);
  202.  
  203. cout<<"Display next record (y/n)> ";
  204.  
  205. char Ans;
  206.  
  207. cin>>Ans;
  208.  
  209. cout<<endl;
  210.  
  211. if(Ans=='n') return;
  212.  
  213. }
  214.  
  215. }
  216.  
  217.  
  218.  
  219. void AddRecord()
  220.  
  221. {// Adds a new record to data file
  222.  
  223. int i = gNumRecs;
  224.  
  225. gPetRecs[i] = new PetRecord;
  226.  
  227. char temp;
  228.  
  229.  
  230.  
  231. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
  232.  
  233. cout << "+" << setfill(' ')<<setw(38)<< " Add Record to Database" << setfill(' ') <<setw(16) << "+" << endl;
  234.  
  235. cout << "+" << setfill(' ')<<setw(40)<<" (Enter your pets details)" << setfill(' ') <<setw(14) << "+" << endl;
  236.  
  237. cout << "+" << setfill(' ')<<setw(45)<<" (Enter ? or -1 if detail is unknown)" << setfill(' ') <<setw(9) << "+" << endl;
  238.  
  239. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
  240.  
  241. cout <<"Is the pet lost or found? (l/f) =>";
  242.  
  243. cin >> temp;
  244.  
  245. switch(temp)
  246.  
  247. {
  248.  
  249. case 'l': gPetRecs[i]->Status = Lost;break;
  250.  
  251. case 'f': gPetRecs[i]->Status = Found;break;
  252.  
  253. default: cout << " incorrect status";
  254.  
  255. }
  256.  
  257.  
  258.  
  259. cout <<"Is the pet a dog or cat? (d/c) =>";
  260.  
  261. cin >> temp;
  262.  
  263. switch(temp)
  264.  
  265. {
  266.  
  267. case'c': gPetRecs[i]->Type = Cat; break;
  268.  
  269. case'd': gPetRecs[i]->Type = Dog; break;
  270.  
  271. default: cout << " incorrect type";
  272.  
  273. }
  274.  
  275. cout <<"What is the gender of the pet (m/f) =>";
  276.  
  277. cin >> temp;
  278.  
  279. switch(temp)
  280.  
  281. {
  282.  
  283. case'm': gPetRecs[i]->Gender = Male; break;
  284.  
  285. case'f': gPetRecs[i]->Gender = Female; break;
  286.  
  287. default: gPetRecs[i]->Gender = Unknown; break;
  288.  
  289. }
  290.  
  291.  
  292.  
  293. cout <<"What breed is the pet? =>";
  294.  
  295. cin.ignore();
  296.  
  297. cin.getline(gPetRecs[i]->Breed,cMaxChars, '\n');
  298.  
  299. lowercase(gPetRecs[i]->Breed);
  300.  
  301. cout <<"What age is the pet? (yy mm ) =>";
  302.  
  303. cin >> gPetRecs[i]->Age.Yrs>>gPetRecs[i]->Age.Mths;
  304.  
  305. cout <<"What colour is the pet? =>";
  306.  
  307. cin.ignore();
  308.  
  309. cin.getline(gPetRecs[i]->Colour,cMaxChars, '\n');
  310.  
  311. lowercase(gPetRecs[i]->Colour);
  312.  
  313. cout <<"In what suburb was the pet lost? =>";
  314.  
  315. cin.getline(gPetRecs[i]->Location,cMaxChars, '\n');
  316.  
  317. lowercase(gPetRecs[i]->Location);
  318.  
  319. cout <<"What is your phone number? =>";
  320.  
  321. cin.getline(gPetRecs[i]->PhoneNo,cMaxChars, '\n');
  322.  
  323. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
  324.  
  325. cout <<"+" << setfill(' ')<<setw(48)<<"A new record has been added to the database"<< setfill(' ') <<setw(6) <<"+" <<endl;
  326.  
  327. cout <<"+" << setfill(' ')<<setw(10)<< gNumRecs <<" records are in the database"<< setfill(' ') << setw(16) <<"+"<<endl;
  328.  
  329. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
  330.  
  331.  
  332.  
  333. ofstream fout;
  334.  
  335. fout.open(cDataFileName, ios::app);
  336.  
  337. switch(gPetRecs[i]->Status)
  338.  
  339. {
  340.  
  341. case Lost: fout << "lost" << endl;break;
  342.  
  343. case Found: fout << "found" << endl;break;
  344.  
  345. }
  346.  
  347.  
  348.  
  349. switch(gPetRecs[i]->Type)
  350.  
  351. {
  352.  
  353. case Cat: fout << "Cat" << endl;break;
  354.  
  355. case Dog: fout << "Dog" << endl;break;
  356.  
  357. }
  358.  
  359.  
  360.  
  361. switch(gPetRecs[i]->Gender)
  362.  
  363. {
  364.  
  365. case Male: fout << "Male" << endl;break;
  366.  
  367. case Female: fout << "Female" << endl;break;
  368.  
  369. case Unknown: fout << "Unknown" << endl;break;
  370.  
  371. }
  372.  
  373.  
  374.  
  375. fout << gPetRecs[i]->Breed << endl;
  376.  
  377. fout << gPetRecs[i]->Age.Yrs << " " << gPetRecs[i]->Age.Mths << endl;
  378.  
  379. fout << gPetRecs[i]->Colour << endl;
  380.  
  381. fout << gPetRecs[i]->Location << endl;
  382.  
  383. fout << gPetRecs[i]->PhoneNo << endl;
  384.  
  385. }
  386.  
  387.  
  388.  
  389.  
  390.  
  391. void lowercase(char string[]) {
  392.  
  393. for(int i = 0; string[i]; i++)
  394.  
  395. string[i] = tolower(string[i]);
  396.  
  397. }
  398.  
  399.  
  400.  
  401. void SearchArray()
  402.  
  403. {
  404.  
  405.  
  406.  
  407. char temp;
  408.  
  409. PetRecord searchdetails;
  410.  
  411. bool search;
  412.  
  413. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
  414.  
  415. cout << "+" << setfill(' ')<<setw(35)<< " Search Database" << setfill(' ') <<setw(19) << "+" << endl;
  416.  
  417. cout << "+" << setfill(' ')<<setw(40)<<" (Enter your pets details)" << setfill(' ') <<setw(14) << "+" << endl;
  418.  
  419. cout << "+" << setfill(' ')<<setw(45)<<" (Enter ? or -1 if detail is unknown)" << setfill(' ') <<setw(9) << "+" << endl;
  420.  
  421. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
  422.  
  423.  
  424.  
  425. cout << "search lost or found pets(l/f)=>";
  426.  
  427. cin >> temp;
  428.  
  429. switch(temp)
  430.  
  431. {
  432.  
  433. case'l': searchdetails.Status = Lost; break;
  434.  
  435. case'f': searchdetails.Status = Found;break;
  436.  
  437. }
  438.  
  439.  
  440.  
  441. cout << " search for cat or dog (c/d)=>";
  442.  
  443. cin >> temp;
  444.  
  445. switch(temp)
  446.  
  447. {
  448.  
  449. case'c': searchdetails.Type = Cat; break;
  450.  
  451. case'd': searchdetails.Type = Dog; break;
  452.  
  453. }
  454.  
  455. cout << " search for a gender =>";
  456.  
  457. cin >> temp;
  458.  
  459. switch(temp)
  460.  
  461. {
  462.  
  463. case'm': searchdetails.Gender = Male; break;
  464.  
  465. case'f': searchdetails.Gender = Female; break;
  466.  
  467. default: searchdetails.Gender = Unknown; break;
  468.  
  469. }
  470.  
  471.  
  472.  
  473. cin.ignore();
  474.  
  475.  
  476.  
  477. cout <<"What breed to search for? =>";
  478.  
  479. cin.ignore();
  480.  
  481. cin.getline(searchdetails.Breed,cMaxChars,'\n');
  482.  
  483. lowercase(searchdetails.Breed);
  484.  
  485.  
  486.  
  487. cout <<"What age to search for? (yy mm ) =>";
  488.  
  489. cin >> searchdetails.Age.Yrs >> searchdetails.Age.Mths;
  490.  
  491. cin.ignore();
  492.  
  493.  
  494.  
  495. cout <<"What colour to search for? =>";
  496.  
  497. cin.getline(searchdetails.Colour,cMaxChars, '\n');
  498.  
  499. lowercase(searchdetails.Colour);
  500.  
  501.  
  502.  
  503. cout <<"In what suburb was the pet lost? =>";
  504.  
  505. cin.getline(searchdetails.Location,cMaxChars, '\n');
  506.  
  507. lowercase(searchdetails.Location);
  508.  
  509.  
  510.  
  511.  
  512.  
  513. for ( int i = 0; i < gNumRecs; i++)
  514.  
  515. {
  516.  
  517. search = true;
  518.  
  519. if ( searchdetails.Status!= gPetRecs[i]->Status) search =false;
  520.  
  521.  
  522.  
  523. if ( searchdetails.Type!= gPetRecs[i]->Type) search = false;
  524.  
  525.  
  526.  
  527. switch(searchdetails.Gender){
  528.  
  529. case Unknown: break;
  530.  
  531. default: if(searchdetails.Gender != gPetRecs[i]->Gender){
  532.  
  533. search = false;
  534.  
  535. }
  536.  
  537. }
  538.  
  539.  
  540.  
  541. switch(searchdetails.Breed[0]){
  542.  
  543. case '?': break;
  544.  
  545. default: if(strcmp(gPetRecs[i]->Breed, searchdetails.Breed) !=0){
  546.  
  547. search = false;
  548.  
  549. }
  550.  
  551. }
  552.  
  553.  
  554.  
  555. switch(searchdetails.Age.Yrs){
  556.  
  557. case -1: break;
  558.  
  559. default: if(gPetRecs[i]->Age.Yrs != searchdetails.Age.Yrs){
  560.  
  561. search = false;
  562.  
  563. }
  564.  
  565. }
  566.  
  567.  
  568.  
  569.  
  570.  
  571. switch(searchdetails.Age.Mths){
  572.  
  573. case -1: break;
  574.  
  575. default: if(gPetRecs[i]->Age.Mths != searchdetails.Age.Mths){
  576.  
  577. search = false;
  578.  
  579. }
  580.  
  581. }
  582.  
  583.  
  584.  
  585. switch(searchdetails.Colour[0]){
  586.  
  587. case '?': break;
  588.  
  589. default: if(strcmp(gPetRecs[i]->Colour, searchdetails.Colour) !=0){
  590.  
  591. search = false;
  592.  
  593. }
  594.  
  595. }
  596.  
  597.  
  598.  
  599. switch(searchdetails.Location[0]){
  600.  
  601. case '?': break;
  602.  
  603. default: if(strcmp(gPetRecs[i]->Location, searchdetails.Location) !=0){
  604.  
  605. search = false;
  606.  
  607. }
  608.  
  609. }
  610.  
  611.  
  612.  
  613. for ( int i =0; i < gNumRecs;i++)
  614.  
  615. if(search == false)
  616.  
  617. DeleteRecord(i);
  618.  
  619.  
  620.  
  621.  
  622.  
  623. }
  624.  
  625.  
  626.  
  627. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
  628.  
  629. cout <<"+" << setfill(' ')<<setw(42)<<"There are" << gNumRecs << "pets in the database"<< setfill(' ') <<setw(12)<<"+"<<endl;
  630.  
  631. cout <<"+" << setfill(' ')<<setw(42)<< "that match the search criteria"<< setfill(' ') << setw(12) <<"+"<<endl;
  632.  
  633. cout <<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641. }
  642.  
  643.  
  644.  
  645. void CleanUp()
  646.  
  647. {// Deletes all dynamic data in gPetRecs array
  648.  
  649.  
  650.  
  651. }
  652.  
  653.  
  654.  
  655. // ============= Private Functions Definitions =========================
  656.  
  657.  
  658.  
  659. void DeleteRecord(int RecordNum)
  660.  
  661. {
  662.  
  663. for( int i = RecordNum; i < gNumRecs; i++)
  664.  
  665. gPetRecs[i] = gPetRecs[i+1];
  666.  
  667.  
  668.  
  669. gNumRecs--;
  670.  
  671. }
  672.  
  673.  
  674.  
  675.  
  676.  
  677. void DisplayRecord(int i)
  678.  
  679. {// Displays record i on screen
  680.  
  681. cout<<"Status: ";
  682.  
  683. switch(gPetRecs[i]->Status){
  684.  
  685. case Lost: cout << "lost\n"; break;
  686.  
  687. case Found: cout << "found\n";break;
  688.  
  689. }
  690.  
  691. cout<<"Type : ";
  692.  
  693. switch(gPetRecs[i]->Type){
  694.  
  695. case Cat: cout << "cat\n";break;
  696.  
  697. case Dog: cout << "dog\n";break;
  698.  
  699. }
  700.  
  701. cout<<"Gender: ";
  702.  
  703. switch(gPetRecs[i]->Gender){
  704.  
  705. case Male: cout << "male\n"; break;
  706.  
  707. case Female: cout << "female\n";break;
  708.  
  709. default: cout << "unknown\n";break;
  710.  
  711. }
  712.  
  713. cout<<"Breed: "<<gPetRecs[i]->Breed<<endl;
  714.  
  715. cout<<"Age: "<<gPetRecs[i]->Age.Yrs<<" Yrs "<<gPetRecs[i]->Age.Mths<<" Mths\n";
  716.  
  717. cout<<"Colour: "<<gPetRecs[i]->Colour<<endl;
  718.  
  719. cout<<"Location: "<<gPetRecs[i]->Location<<endl;
  720.  
  721. cout<<"Phone No: "<<gPetRecs[i]->PhoneNo<<endl;
  722.  
  723. }
Add Comment
Please, Sign In to add comment