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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
#include "AddressBook.h"
#include <iostream>
#include <string>
#include <istream>
#include <fstream>
#include <ostream>
using namespace std;
const int ADDR_BOOK_SZ = 1000;
AddressBook addrBook[ADDR_BOOK_SZ];
int numEntries = 0;
bool fileOpen = false;
void openFile(ifstream& infile, string filename);
int getSize(ifstream& infile, string filename);
void makeArray(ifstream& infile);
void setArray(ifstream& infile, string& filename);
void printArray();
void addEntry();
void writeToFile();
char printMenu();
void printErrorMessage();
int main()
{
ifstream infile;
string filename;
char choice;
cout << endl;
do
{
choice = printMenu();
switch (choice)
{
case 'a': setArray(infile, filename);
break;
case 'b': addEntry();
break;
case 'c': printArray();
break;
case 'd': break;
default:
printErrorMessage();
}
} while (choice != 'd');
writeToFile();
return 0;
}
void openFile(ifstream& infile, string filename)
{
infile.open(filename.c_str());
if (infile.fail())
{
cout << "Error in opening the input file." << endl;
cout << "Ending program now." << endl;
exit(1);
}
}
int getSize(ifstream& infile, string filename)
{
string oneItem;
int count = 0;
infile >> oneItem;
while (!infile.eof())
{
getline (infile, oneItem);
count++;
}
infile.close();
infile.open(filename.c_str());
return (count / 7);
}
void makeArray(ifstream& infile)
{
string oneItem;
for(int j = 0; j < numEntries; j++)
{
getline (infile, oneItem);
addrBook[j].SetFirstName(oneItem);
getline (infile, oneItem);
addrBook[j].SetLastName(oneItem);
getline (infile, oneItem);
addrBook[j].SetStreetNum(oneItem);
getline (infile, oneItem);
addrBook[j].SetStreetName(oneItem);
getline (infile, oneItem);
addrBook[j].SetCity(oneItem);
getline (infile, oneItem);
addrBook[j].SetState(oneItem);
getline (infile, oneItem);
addrBook[j].SetZipCode(oneItem);
}
infile.close();
fileOpen = true;
}
void setArray(ifstream& infile, string& filename)
{
//cout << "Enter a filename to open: ";
//cin >> filename;
filename = "sample.txt";
openFile(infile, filename);
numEntries = getSize(infile, filename);
makeArray(infile);
cout << "Loaded file and read " << numEntries << " records." << endl << endl;
}
void printArray()
{
for(int i = 0; i < numEntries; i++)
{
addrBook[i].PrintToConsole();
cout << endl;
}
cout << endl;
}
void addEntry()
{
bool duplicate = false;
int i = 0;
addrBook[numEntries].AddEntryFromConsole();
while(duplicate == false && i < numEntries)
{
duplicate = (addrBook[i] == addrBook[numEntries]);
i++;
}
if (duplicate)
{
cout << "This is a duplicate entry and will not be added to the address book." << endl << endl;
}
else if (!duplicate)
{
numEntries++;
}
}
void writeToFile()
{
for(int i = 0; i < numEntries; i++)
{
addrBook[i].AppendToFile();
cout << endl;
}
cout << endl;
}
char printMenu()
{
char choice;
cout << "Please make a selection:" << endl;
cout << "a) Open an address book file" << endl;
cout << "b) Add a new address book entry" << endl;
cout << "c) Print the contents of current address book" << endl;
cout << "d) Quit" << endl;
cout << "-> ";
cin >> choice;
choice = tolower(choice);
cout << endl;
cin.ignore(); //to discard the endl for other cin functions
return choice;
}
void printErrorMessage()
{
cout << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
cout << "!!!! Input only listed menu options !!!" << endl;
cout << "!!!! Please try again. !!!" << endl;
cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
cout << endl;
}
|