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

Your function displayZip contains this line:
cout << zipCode << " " << cityName << endl;
so it displays the zipCode and the cityName. If you don't want it to display the cityName, remove that from the code.

Ok next problem for the searchZip when I enter 60561 I get Zip code nit found. What am I dooing wrong?

I found several things wrong in your SearchZip routine. It's easier to just to show you the corrected code:

void searchZip()
{
    string zipCode = "";
    string cityName = "";
    string searchFor = "";
    //I set this to false. This way it only needs to change if a zip is found.
    bool found = false;
//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())
    {
        //You had this inside the while loop.  It would keep asking for another zip code  
        //but there was only one record to compare it to
        cout << "Enter Zip Code: " ;
        getline(cin,searchFor);

        while (!inFile.eof())
        {
            //You had this outside the while loop.  Therefore it only loaded the first record.
            getline(inFile, zipCode, '#');
            getline(inFile, cityName);
            if (zipCode == searchFor)
            {
                //Once it's found no need to search any further.
                found = true;
                break;
            }
        }
        //I moved the display outside the while loop, to display the final results 
        //once the while loop is finished.
        if(found)
            cout << cityName << endl;
        else
            cout << "Zip code not found." << endl;
        inFile.close();
    }
    else
        cout << "File could not be opened." << endl;
}

The only other change I had to make was to add int menuOption; at the beginning of main.

If this has answered your questions please remember to mark this solved. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.