City to zip program help please

I need help i do not know how to enter the zip and have it display the city vice versa can someone please help me with this?

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
 #include <iostream>
#include <fstream>
#include <string>
using namespace std;

    int displayMenu ();
	void addRecords ();
	void zip ();
	void city ();

int main()
{
	int menu = 0;

	menu = displayMenu();

	while (menu != 4)
	{
		if(menu == 1)
			addRecords();
		else if (menu == 2)
			zip();
		else if (menu == 3)
			city();
		else
			cout << "Invalid menu choice..." << endl;

		menu = displayMenu();

	}


	system("pause");
	return 0;
}

int displayMenu()
{
	int choice = 0;

	cout <<"options"<<endl;
	cout <<"1 add city/zip"<<endl;
	cout <<"2 display zip"<<endl;
	cout <<"3 display city"<<endl;
	cout <<"4 exit program"<<endl;

	cout << "Enter menu option: ";
	cin >> choice;
	cin.ignore(1);
	return choice;
}

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

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

	if (outFile.is_open())
	{
		cout << "Enter zip code (4 to stop): ";
		getline(cin, zip);
		while (zip != "4")
		{ 
			cout << "Enter city: ";
			getline(cin, city);
			cin.ignore();
			outFile << zip << '#' << city << endl;
			cout << "Enter zip (4 to stop): ";
			getline(cin, zip);
		}

		outFile.close();
	} 
	else
		cout << "File could not be opened." << endl;
}

void city()
{
	string input = "";
	string zip = "";
	string city = "";
	bool found = false;

	ifstream inFile;
	inFile.open("zipcity.txt", ios::in);

	if (inFile.is_open())
	{
		cout << "Enter city: ";
		cin >> city;
		while (!inFile.eof() && !found) 
		{

				getline(inFile, input);

		} 

		inFile.close();

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

}

void zip()
{
	string input = "";
	string zip = "";
	string city = "";
	bool found = false;

	ifstream inFile;
	inFile.open("zipcity.txt", ios::in);

	if (inFile.is_open())
	{
		cout << "Enter zip code: ";
		cin >> zip;
		while (!inFile.eof() && !found) 
		{

				getline(inFile, input);

		} 

		inFile.close();

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

}
Last edited on
At lines 98 and 127 you have read a line of text from the data file into the string variable input. Now you just need to parse apart that lne of text.

A stringstream is the usual way to do this.
128
129
130
131
132
133
134
135
136
 
  stringstream ss(input);  // create a stream from the input line
  string f_zip;                   // zip code from file
  getline (ss, f_zip, '#');   // read the zip from the current line
  getline (ss, city);           // read the city from the file 
  if (f_zip == zip) 
  { found = true;
     cout << "The city for that zip is: " << city << endl;
  }

City to zip is essentially the same, except you're comparing the city name.

Please don't post the same question in multiple forums:
http://www.cplusplus.com/forum/general/128647/

Last edited on
Topic archived. No new replies allowed.