void displayCity()
{//declare variables
string input = " ";
string city = " ";
string zip = " ";
char found = 'N';
//create file object and open the file
ifstream inFile;
inFile.open("Advanced26.txt");
//determine whether the file was opened
if (inFile.is_open())
{
//get the zip code from the user
cout<< "Enter the ZIP code";
getline(cin,input);
//read a record
getline(inFile,zip,'#');
getline(inFile,city);
}
while (!inFile.eof() && found == 'N')
{
//if the zip code matches, display the city name
if (zip == input)
{
cout<<endl<<"City name"<<city<<endl<<endl;
found = 'Y';
}
else
{
getline(inFile,zip,'#');
getline(inFile,city);
}//end if
}//end while
//close the file
inFile.close();
//display error message if ZIP code is not in the file
if (found == 'N')
cout<<endl<<"ZIP code"<<input<<"is not on file"<<endl;
}
#include<iostream>
#include<fstream>
#include<string>
usingnamespace std;
int displayMenu();
void addRecords();
void displayCity();
int main(){
//declare variables
int menuChoice = 0;
//display menu and get choice
menuChoice = displayMenu();
//all appropriate function or
//
while (menuChoice != 3)
{
if (menuChoice == 1)
addRecords();
elseif (menuChoice == 2)
displayCity();
else
cout<<"Invalid menu choice"<<endl<<endl;
menuChoice = displayMenu();
}
system("pause");
return 0;}
int displayMenu()
{
int choice = 0;
cout<<"Options"<<endl;
cout<<"1 Add City/ZIP Code"<<endl;
}
void addRecords()
{ //declare variables
string city = " ";
string zip = " ";
//create file object and open the file
ofstream outFile;
outFile.open("Advanced26.txt");
//determine whether the file was opened
if (outFile.is_open());
{ //get the zip code
cout<< "Enter the ZIP code (X to stop)";
getline(cin,zip);
while (zip != "X" && zip != "x")
{ //get city name
cout<< "Enter the city name";
getline(cin, city);
//write the records
outFile<<zip<<'#'<<city<<endl;
cout<< "Enter the ZIP code (X to stop):";
getline(cin,zip);
}//end while
//close the file
outFile.close();}}}
else
cout<<"File could not be opened"<<endl<<endl;
} //end if
}//end addRecords function
void displayCity()
{//declare variables
string input = " ";
string city = " ";
string zip = " ";
char found = 'N';
//create file object and open the file
ifstream inFile;
inFile.open("Advanced26.txt");
//determine whether the file was opened
if (inFile.is_open())
{
//get the zip code from the user
cout<< "Enter the ZIP code";
getline(cin,input);
//read a record
getline(inFile,zip,'#');
getline(inFile,city);
}
while (!inFile.eof() && found == 'N')
{
//if the zip code matches, display the city name
if (zip == input)
{
cout<<endl<<"City name"<<city<<endl<<endl;
found = 'Y';
}
else
{
getline(inFile,zip,'#');
getline(inFile,city);
}//end if
}//end while
//close the file
inFile.close();
//display error message if ZIP code is not in the file
if (found == 'N')
cout<<endl<<"ZIP code"<<input<<"is not on file"<<endl;
}