Zip Code/City Sequential Access File Program Help

Im writing a program that stores zip codes with corresponding cities in a sequential access file. This I have no problem with. The program also has to display the city corresponding with the zip code entered by the user, and vice versa. I am confused on what to do for this part of the program.

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

//Ch13Ex1.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>

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();

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

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

	//call appropriate function
	//or display error message
	while (menuChoice != 4)
	{
		if (menuChoice == 1)
			addZipcity();
		else if (menuChoice == 2)
			displayCity();
		else if (menuChoice == 3)
			displayZip();
		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 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);
			cin.ignore();
			//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 addZipcity 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: ";
		cin >> zip;
		while (!inFile.eof() && !found) 
		{
			
			cout << "City: " << city << endl;
			
			

			//read another line from the file
				getline(inFile, input);

		} //end while

		//close the file
		inFile.close();

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

void displayZip()
{

}


for void displayZip() I know I'd essentially have to reverse whatever I did in void displayCity().

I am extremely confused.
Last edited on
Topic archived. No new replies allowed.