1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <iostream>
#include "ass1.h"
using namespace std;
bool loadData(ifstream& fileLoad, struct animal records[totalRecords], int& recordCount)
{
cout << "in loadData" << endl;
int i = 0;
fileLoad.open("pets2.dat");
if(!fileLoad.good())
{
cerr << "file cant be opened" << endl;
return false;
}
else
{
while(fileLoad.getline(records[i].status, maxStringLength, '\n'))
{
recordCount++;
fileLoad >> records[i].type >> records[i].gender;
fileLoad.ignore();
fileLoad.getline(records[i].breed, maxStringLength, '\n');
fileLoad >> records[i].ageYears >> records[i].ageMonths >> records[i].colour >> records[i].chipNum;
fileLoad.ignore();
fileLoad.getline(records[i].location, maxStringLength, '\n');
fileLoad >> records[i].phoneNum;
fileLoad.ignore();
//cout << records[i].status << " " << records[i].type << records[i].gender << records[i].breed << records[i].ageYears << records[i].ageMonths << records[i].colour << records[i].chipNum << records[i].location << records[i].phoneNum << endl;
i++;
}
cout << "There are " << recordCount << " records in the directory" << endl;
return true;
}
return true;
}
void displayRecords(struct animal records[totalRecords])
{
//display first record
//offer option to display next or quit
//if quit close
//if display next show next
//continue until no is chosen
int i = 0;
char cmd;
while(cmd != 'n')
{
switch(recordDisplay(records, i, cmd))
{
case 'y':
i++;
recordDisplay(records, i, cmd);
break;
case 'n':
cout << "Returning to Main Menu" << endl;
break;
}
}
}
char recordDisplay(struct animal records[totalRecords], int i, char& cmd)
{
cout << "in displayRecords" << endl;
cout << "Status: " << records[i].status << endl;
cout << "Type: " << records[i].type << endl;
cout << "Gender: " << records[i].gender << endl;
cout << "Breed: " << records[i].breed << endl;
cout << "Age: " << records[i].ageYears << " years " << records[i].ageMonths << " months" << endl;
cout << "Colour: " << records[i].colour << endl;
cout << "Microchip: " << records[i].chipNum << endl;
cout << "Location: " << records[i].location << endl;
cout << "Telephone: " << records[i].phoneNum << endl;
cout << "Would you like to display another record? (y/n): ";
cin >> cmd;
cin.ignore();
return cmd;
}
bool addRecord(struct animal records[totalRecords], int& recordCount, int totalRecords)
{
cout << "in addRecord" << endl;
recordCount++;
//check to see if index > totalrecords
//if yes return false
//if no input data
//update file
if(recordCount > totalRecords)
{
cerr << "Sorry the directory is full" << endl;
recordCount--;
return false;
}
else
{
cout << "Status: ";
cin >> records[recordCount].status;
cout << "Type: ";
cin >> records[recordCount].type;
cout << "Gender: ";
cin >> records[recordCount].gender;
cout << "Breed: ";
cin >> records[recordCount].breed;
cout << "Age (YY MM): ";
cin >> records[recordCount].ageYears >> records[recordCount].ageMonths;
cout << "Colour: ";
cin >> records[recordCount].colour;
cout << "Microchip: ";
cin >> records[recordCount].chipNum;
cout << "Location: ";
cin >> records[recordCount].location;
cout << "Telephone: ";
cin >> records[recordCount].phoneNum;
}
cout << records[recordCount].status << " " << records[recordCount].type << records[recordCount].gender << records[recordCount].breed << records[recordCount].ageYears << records[recordCount].ageMonths << records[recordCount].colour << records[recordCount].chipNum << records[recordCount].location << records[recordCount].phoneNum << endl;
return true;
}
bool searchRecords()
{
cout << "in searchRecords" << endl;
return true;
}
bool eraseRecord()
{
cout << "in removeRecord" << endl;
return true;
}
bool undoErase()
{
cout << "in undoRemove" << endl;
return true;
}
bool updateFile(ofstream& fileOut, struct animal records[totalRecords], int recordCount)
{
//open file
//check to see if file open is good
//if yes
//loop through struct array outputting data
//return true
//if no return false
fileOut.open("pets2.dat", ios::out | ios::trunc);
if(!fileOut.good())
{
cerr << "Cannot Open File" << endl;
return false;
}
else
{
for(int i = 0; i <= recordCount; i++)
{
fileOut << records[i].status << '\n' << records[i].type << '\n' << records[i].gender << '\n' << records[i].breed << '\n' << records[i].ageYears << " " << records[i].ageMonths << '\n' << records[i].colour << '\n' << records[i].chipNum << '\n' << records[i].location << '\n' << records[i].phoneNum << endl;
cout << records[i].status << " " << records[i].type << records[i].gender << records[i].breed << records[i].ageYears << records[i].ageMonths << records[i].colour << records[i].chipNum << records[i].location << records[i].phoneNum << endl;
fileOut.clear();
}
}
return true;
}
| |