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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//function prototypes
void saveZip();
void displayZip();
void searchZip();
int main()
{
int menuOption = 0;
do //begin loop
{
//display menu and get option
cout << endl;
cout << "1 Enter ZIP code information" << endl;
cout << "2 Display ZIP Codes" << endl;
cout << "3 Search for city name by ZIP code" << endl;
cout << "4 End the program" << endl;
cout << "Enter menu option: ";
cin >> menuOption;
cin.ignore(100, '\n');
cout << endl;
//call appropriate function
//or display error message
if (menuOption ==1)
saveZip();
else if (menuOption ==2)
displayZip();
else if (menuOption ==3)
searchZip();
//end if
} while (menuOption != 4);
return 0;
} //end of main
//*****function definitions*****
void saveZip()
{
//writes records to a sequential access file
string zipCode = "";
string cityName = "";
//create file object and open the file
ofstream outFile;
outFile.open("Advanced26.txt", ios::app);
//determine whether the file is opened
if (outFile.is_open())
{
//get the ZIP code
cout << "Zip code (-1 to stop): ";
getline(cin, zipCode);
while (zipCode != "-1")
{
//get the city name
cout << "City name: ";
getline (cin, cityName);
//write the record
outFile << zipCode << '#'
<< cityName << endl;
//get another ZIP code
cout << "Zip code (-1 to stop): ";
getline(cin, zipCode);
} //end while
//close the file
outFile.close();
}
else
cout << "File could not be opened." << endl;
//end if
} //end of saveZip function
void displayZip()
{
//displays the records stored in the zip.txt file
string zipCode = "";
string cityName = "";
//create file object and open the file
ifstream inFile;
inFile.open("Advanced26.txt", ios::in);
//determine whether the file was opened
if (inFile.is_open())
{
//read a record
getline(inFile, zipCode, '#');
getline(inFile, cityName);
while (!inFile.eof())
{
//display the record
cout << zipCode << " " <<
cityName << endl;
//read another record
getline(inFile, zipCode, '#');
getline(inFile, cityName);
} //end while
//close the file
inFile.close();
}
else
cout << "File could not be opened." << endl;
//end if
} //end of displayZip function
void searchZip()
{
string zipCode = "";
string cityName = "";
string searchFor = "";
//create file object and open the file
ifstream inFile;
inFile.open("Advanced26.txt", ios::in);
//determine whether the file was opened
if (inFile.is_open())
{
getline(inFile, zipCode, '#');
getline(inFile, cityName);
while (!inFile.eof())
{
cout << "Enter Zip Code: " ;
getline(cin,searchFor);
if (zipCode == searchFor)
cout << cityName << endl;
else
cout << "Zip code not found." << endl;
}
inFile.close();
}
else
cout << "File could not be opened." << endl;
} //end of searchZip function
|