Problem with a program using a sequential access file

This is a class assignment that I'm having a little trouble with. This is my first time using sequential access files and I really could use a little help. What I don't have figured out yet is in my searchZip function. I have 5 zip codes in the file with their corresponding city name.

60561 Darien
60544 Hinsdale
60137 Glen Ellyn
60135 Downers Grove
60136 Burr Ridge

When the user types in a ZIP code in the searchZip function, it is supposed to return the corresponding city name if it is found, otherwise it is supposed to return "Zip code not found". It will find the first ZIP code in the file 60561, but it will not find any of the others. Any suggestions would be greatly appreciated. Here is my code so far.

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 

In your searchZip() function, you are only reading the first record, which is out side of your loop. That is why that value is the only one found. Have the user enter the zip code before you read the file. Then loop and read each line and match on the zip code. If you match, display the cityName and break the loop. If not, read another record. If the user enters a zip code that doesn't exist in your file, inFile will receive EOF and you will display "zip code not found."
Thanks, that's much better!
You're most welcome -- please mark this as completed.
Topic archived. No new replies allowed.