Writing Zip codes and cities to sequential access file

I was just wondering what I was doing wrong in my program, which is supposed to add records from the user of zip codes and corresponding cities, and then either display the zip code or city based on input of the user. It compiles properly, but the program itself seems to have logic errors.

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//ZIPCITY.cpp – saves cities and ZIP codes to a sequential access file
//and also displays the city corresponding to a ZIP code

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::ios;

//function prototypes
int displayMenu();
void addRecords();
void displayCity();
void displayZip();
void displayFile();

int main()
{	
	//declare variable
	int menuChoice = 0;

	//display menu and get choice
	menuChoice = displayMenu();

	//call appropriate function
	//or display error message
	while (menuChoice != 5)
	{
		if (menuChoice == 1)
			addRecords();
		else if (menuChoice == 2)
			displayCity();
		else if (menuChoice == 3)
			displayZip();
		else if (menuChoice == 4)
			displayFile();
		else
			cout << "Invalid menu choice" << endl;
		//end ifs

		//display menu and get choice
		menuChoice = displayMenu();
	}	//end while
	return 0;
}   //end of main function

//*****function definitions*****
int displayMenu()
{
	//declare variable
	int choice = 0;

	//display menu
	cout << "Options" << endl;
	cout << "1 Add Zip/City" << endl;
	cout << "2 Display City" << endl;
	cout << "3 Display Zip Code" << endl;
	cout << "4 Display File" << endl;
	cout << "5 Exit Program" << endl;

	//get user's choice, then return the choice
	cout << "Enter menu option: ";
	cin >> choice;
	cin.ignore(1);
	return choice;
}	//end of displayMenu function

void addRecords()
{
	string zip = "";
	string city = "";

	ofstream outFile;
	outFile.open("zipcity.txt", ios::app);

	if (outFile.is_open())
	{	//get the name
		cout << "Enter zip code (X to stop): ";
		getline(cin, zip);
		while (zip != "X" && zip != "x")
		{
			//get the city 
			cout << "Enter city: ";
			getline(cin, city);
			transform(city.begin(), city.end(), city.begin(), toupper);
			//write the record
			outFile << zip << '#' << city << endl;
			//get another name
			cout << "Enter zip (X to stop): ";
			getline(cin, zip);
		} //end while

		//close the file
		outFile.close();
	} //end if
	else
		cout << "File could not be opened." << endl;
	//end if
} //end of addRecords function

void displayCity()
{
//declare variables
	string input = "";
	string zip = "";
	string city = "";
	bool found = false;

	//create file object and open the file
	ifstream inFile;
	inFile.open("zipcity.txt", ios::in);

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//read a record
	    cout << "Enter zip code (X to stop): ";
		getline(cin, input);
		getline(inFile, zip, '#');
		getline(inFile, city);
	while (input != "X" && input != "x")
	{
		if (input != "X" && input != "x")
		{
		while (!inFile.eof() && !found) 
		{

			if (zip == input) 
			{
				cout << "City: " << city << endl;
				found = true;
			}
			else 
			{
				//read another line from the file
				getline(inFile, zip, '#');
				getline(inFile, city);
			}

		} //end while

		} //end if 
		
		else 
		{
			cout << "INVALID ZIP" << endl;
		}
		
		cout << "Enter zip code: ";
		getline(cin, input);
		
	} //end while
		//close the file
		inFile.close();

	}// end if
	else 
		cout << "File could not be opened." << endl;
	//end if 
}

void displayZip()
{
	//declare variables
	string input = "";
	string zip = "";
	string city = "";
	bool found = false;

	//create file object and open the file
	ifstream inFile;
	inFile.open("zipcity.txt", ios::in);

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//read a record
		cout << "Enter city (X to stop): ";
		getline(cin, input);
		transform(city.begin(), city.end(), city.begin(), toupper);
		getline(inFile, zip, '#');
		getline(inFile, city);
	while (input != "X" && input != "x") 
	{
		if (input != "X" && input != "x")
		{
		while (!inFile.eof() && !found) 
		{

			if (zip == input) 
			{
				cout << "Zip Code: " << zip << endl;
				found = true;
			}
			else 
			{
				//read another line from the file
				getline(inFile, zip, '#');
				getline(inFile, city);
			}
		
		} //end while
		} //end if
		else 
		{
			cout << "INVALID ZIP" << endl;
		}
		cout << "Enter city (X to stop): ";
		getline(cin, input); 
	} //end while

		//close the file
		inFile.close();

	}//end if
	else 
		cout << "File could not be opened." << endl;
	//end if 
}

void displayFile()
{
	//declare variables
	string line = "";

	//create file object and open the file
	ifstream inFile;
	inFile.open("zipcity.txt", ios::in);

	//determine whether the file was opened
	if (inFile.is_open())
	{
		//read a line from the file
		getline (inFile, line);
		while (!inFile.eof())
		{
			//display the line
			cout << line << endl;
			//read another line from the file
			getline(inFile, line);
		} //end while

		//close the file
		inFile.close();
	}
	else 
		cout << "File could not be opened." << endl;
	//end if

} //end of displayFile function 


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
void displayCity()
{
    while (true)
    {
        //declare variables
        string input = "";
        string zip = "";
        string city = "";
        bool found = false;

        //create file object and open the file
        ifstream inFile;
        inFile.open("zipcity.txt", ios::in);

        //determine whether the file was opened
        if (inFile.is_open())
        {
            //read a record
            cout << "Enter zip code (X to stop): ";
            getline(cin, input);

            if (input == "X" || input == "x") return;

            while (!inFile.eof() && !found)
            {
                getline(inFile, zip, '#');
                getline(inFile, city);

                if (zip == input)
                {
                    cout << "City: " << city << endl;
                    found = true;
                }
            } //end while

            if (!found)
            {
                cout << "City not found.\n";
            }

            inFile.close();
        }// end if
        else
        {
            cout << "File could not be opened." << endl;
            return;
        }
        //end if
    }
}

void displayZip()
{
    while (true)
    {
        //declare variables
        string input = "";
        string zip = "";
        string city = "";
        bool found = false;

        //create file object and open the file
        ifstream inFile;
        inFile.open("zipcity.txt", ios::in);

        //determine whether the file was opened
        if (inFile.is_open())
        {
            //read a record
            cout << "Enter city (X to stop): ";
            getline(cin, input);
            transform(input.begin(), input.end(), input.begin(), toupper);

            if (input == "X" || input == "x") return;

            while (!inFile.eof() && !found)
            {
                getline(inFile, zip, '#');
                getline(inFile, city);

                if (city == input)
                {
                    cout << "Zip Code: " << zip << endl;
                    found = true;
                }
            } //end while

            if (!found)
            {
                cout << "Zip not found.\n";
            }

            //close the file
            inFile.close();

        }//end if
        else
            cout << "File could not be opened." << endl;
        //end if
    }
}


Compare it with your code and study it for your own benefit.. Good luck!!
Topic archived. No new replies allowed.