Zip Code and City program that writes to text file

I am confused on how to have a user enter a zip code, and then have to proper city to display, based on records previously added by the user.



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

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)
			addRecords();
		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 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: ";
		cin >> zip;
		while (!inFile.eof() && !found) 
		{
			{
			if (search >= 0)
			{
			cout << "City: " << city << 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 
}

void displayZip()
{

}
zipcity.txt has a bunch of lines in it:

number#name
number#name
...

So at line 121, you want to start parsing the data. Extract an int (the zipcode), char (the junk '#', which is useful to have btw), and then getline() your city. Then compare the extracted int with the variable "zip". If they are the same, close the file, break the loop, and print the variables to the screen. If you get to the end of the file with no matches, then have an error report.

My teacher taught us not to combine cin/cout with logic functions. For example, Display city should take a zip as an argument, and then return a city name. A non-existent zip code could just return "not found".

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


Why????


A simple "using namespace std;" will remove the need for all of that BS. I still don't understand why all these beginners are so opposed to using the std namespace.

Sorry if you didn't know why.


I copied you code and line 124 doesn't compile because the variable "search" is not declared.
Last edited on
namespace std; is not a great idea because it restricts the program. they way he did it is perfect if he does not want to do std::cout or std::cin and so on every times he wants to use one of those.
Im sure I want to use boolean values, but Im not sure what to do with them. I also don't know what I should use to check for the entered ZIP in each line until the zip is found and for the user to be able to stop searching zips whenever wanted.

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

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)
			addRecords();
		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 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: ";
		cin >> zip;
		while (!inFile.eof() && !found) 
		{

			//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()
{

}

Last edited on
bool values are good to check if something occurs or exists.
Topic archived. No new replies allowed.