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
|
/**
•Write a program that will read in an array of records of people's names and addresses from the keyboard and then and writes that information to an output file.
•Each record will consist of the following fields: first name 15 characters, last name 15 characters, street address 30 characters, city 20 characters, state 10 characters, zip long integer. Declare a structure called AddressBook to hold a record.
•Read an array of records from the keyboard by prompting for data till the user enters a sentinel to quit. The program should keep track of the number of records read.
•Store this information to a binary file.
•Read from the binary file back to another array of records, and store this information to a text file. Left justify the information for each field.
•Assume a maximum of 20 records.
Output Run
First Name: Jackie
Last Name: Doe
Address: 555 Golf Club Road
City: Pleasant Hill
State: CA
Zip: 94523
First Name: John
Last Name: Anon
Address: 10007 Side Street
City: San Ramon
State: CA
Zip: 94582
First Name: Sandy
Last Name: Beach
Address: 2345 Ocean Avenue
City: Santa Cruz
State: CA
Zip: 95060
First Name: June
Last Name: Empire
Address: 245 33rd Street
City: New York
State: NY
Zip: 10017
*/
#include<iostream>
#include<fstream>
using namespace std;
//------------------------------------------------------------------------------------------
const int SIZE_OF_FIRST_NAME = 15, SIZE_OF_LAST_NAME = 15, STREET_ADDRESS = 30, CITY = 20, STATE = 10, ZIPCODE = 5;
//------------------------------------------------------------------------------------------
struct AddressBook
{
char FirstName[SIZE_OF_FIRST_NAME],
LastName[SIZE_OF_LAST_NAME],
StreetAddress[STREET_ADDRESS],
City[CITY],
State[STATE];
int Zip;
};
//------------------------------------------------------------------------------------------
int main ()
{
int iteration = 0,
option;
AddressBook Address[20],
Address2[20];
string content,
name,
temp,
file_name = "AddressBook.doc";
char insert;
fstream file;
do
{
cout << "Select: ";
cin >> option;
cin.ignore();
switch(option)
{
case 1:
cout << "\n--------- Address Book: ---------\n";
do
{
cout << "First Name: ";
cin.getline(Address[iteration].FirstName, SIZE_OF_FIRST_NAME);
cout << "\nLast Name: ";
cin.getline(Address[iteration].LastName, SIZE_OF_LAST_NAME);
cout << "\nStreet Address: ";
cin.getline(Address[iteration].StreetAddress, STREET_ADDRESS);
cout << "\nCity: ";
cin.getline(Address[iteration].City, CITY);
cout << "\nState: ";
cin.getline(Address[iteration].State, STATE);
cout << "\nZip Code: ";
cin >> Address[iteration].Zip;
iteration++;
//This Section will prompt ask the user wants to re-input the data.
cout << "\nPlease indicate if you would like\nto write a new record (Y/N) ";
cin >> insert; insert = toupper(insert);
cin.ignore();
if(insert == 'Y'){cout << "\n---------------------------------\n\n";}
}
while (insert == 'Y');
file.open("AddressBook.doc", ios::out | ios::binary);
file.write(reinterpret_cast<char *>(&Address), sizeof(Address));
//This section will close the file:
file.close();
//This section will open the file for the binary input:
file.open("AddressBook.doc", ios::in | ios::binary);
//This section will read the date that was previously inputted into the program:
file.read(reinterpret_cast<char *>(&Address2), sizeof(Address2));
file.close();
file.open("AddressBook.doc", ios::out);
//This section displays the contact stored inside the binary file:
/***/
for(int i = 0; i < iteration; i++)
{
file << "--------- " << Address2[i].FirstName << ' ' << Address2[i].LastName << " ---------";
file << "\nFirst Name: " << Address2[i].FirstName;
file << "\nLast Name: " << Address2[i].LastName;
file << "\nAddress: " << Address2[i].StreetAddress;
file << "\nCity: " << Address2[i].City;
file << "\nState: " << Address2[i].State;
file << "\nZip: " << Address2[i].Zip;
file << endl;
}
file << "--------------------------------------\n";
//Re-close file:
file.close();
cout << endl;
break;
case 2:
file.open("/Users/kyledrewes/Library/Developer/Xcode/DerivedData/Address_Book-apkdrrzyaohdlieockksffoggadr/Build/Products/Debug/AdressBook.doc", ios::in);
if(file.is_open())
{
cout << "\nWhich name would you\nlike to delete ? ";
getline(cin,name);
while(!file.eof())
{
getline(file,content);
if(content.find(name) == string::npos){temp += content;}
content.clear();
}
file.close();
file.open("/Users/kyledrewes/Library/Developer/Xcode/DerivedData/Address_Book-apkdrrzyaohdlieockksffoggadr/Build/Products/Debug/AdressBook.doc",ios::out|ios::trunc);
if(file.is_open()){file << temp; file.close();}
else{cout << "\nFile is not open\n\n";}
}
else{cout << "\nFile is not open\n\n";}
break;
case 3:
//Display Content
for (int i = 0; i < iteration; i++)
{
cout << "-------------- " << Address2[i].FirstName << ' ' << Address2[i].LastName << " --------------";
cout << "\nFirst Name: " << Address2[i].FirstName;
cout << "\nLast Name: " << Address2[i].LastName;
cout << "\nAddress: " << Address2[i].StreetAddress;
cout << "\nCity: " << Address2[i].City;
cout << "\nState: " << Address2[i].State;
cout << "\nZip: " << Address2[i].Zip;
cout << endl << endl;
}
break;
default:
cout << "\nInvalid entry, please re-enter.";
break;
}
}while(option!=4);
return 0;
}
//------------------------------------------------------------------------------------------
|