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
// definitions of functiions
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 != "0")
{
//get the city name
cout << "City name: ";
getline (cin, cityName);
//write the record
outFile << zipCode << '#'
<< cityName << endl;
//get another ZIP code
cout << "Zip code (0 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 a text 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
//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