Code Problem

I have a problem with the searchzip function

This is the zip code and city

60561 Darien
60544 Hinsdale
60137 Glen Ellyn
60135 Downers Grove


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
#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

// 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 << " " << 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
And what's the problem?
When I enter the zip codes you see at the top e.g. 60561 , I get the right answer, I just want to know how to end the searchZip like by a number.
You want to end searchZip once you get the max amount of numbers?
No say like I just got tired of looking for cities and I just want to end it, no max number needed.
Look intp the SDL library. Just poll for events and see if esc is pressed and end the program.
1
2
3
4
5
6
7
8
9
10
while (!inFile.eof())
{
	cout << "Enter Zip Code: " ;
	getline(cin,searchFor);

	if (zipCode == searchFor)
		cout << cityName << endl;
	else
		cout << "Zip code not found." << endl;
}
┬┐what part of the loop may change the value of the condition?
You are not affecting `inFile' in any way.

That snip will compare whatever the user enters against the first line (and only the first line) of the file.
Thanks
Topic archived. No new replies allowed.