Problem 1: When I need to display my zipcode, I get zipcode and city. Problem 2 : When I enter zipcode I get zipcode not found.
This my zipcode and city
60561 Darien
60544 Hinsdale
60137 Glen Ellyn
60135 Downers Grove
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//function prototypes
void saveZip();
void displayZip();
void searchZip();
int main()
{
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 != "-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 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
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