zip to city

Would this program be good and complete?

#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
if you want to reviewed i'd post it in code tags if i was you.
How do i do that?
i think there is an error with the system
Can anyone tell me how to enter the zip and it display the city its not working for me im a beginner programmer and im confused.
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;

} 
alright now can someone help me
Can anyone tell me how to enter the zip and it display the city
Have you looked at the data file?

The code that writes the file is:
 
outFile << zip << '#' << city << endl;


But the code that reads it, doesn't seems to decode that line. I don't see anything that splits that line into a zip/city pair.
can you teach me how to do that kbw its confusing to me
We're not mind-readers. Explain as clearly and precisely as you can what problem you have. What behaviour are you seeing, and how does that differ from the behaviour you think you should be seeing?

Just as a general bit of advice: your code would be much clearer and easier to understand - for you, and for us - if you used a sensible indentation style. That will help you see at a glance what the flow of control and logic is through the code, and will also help you see scoping issues more easily.
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;

}
That is much better, I can read your code now. So what is the question ?

If it compiles, runs, gives you the output that it should, doesn't crash when you put in crap data, then it's good and complete, if not then it's not.

So I would remove the system("pause"); line 33
I would remove cin.ignore(); line 69

When I run your program, at the menu, if I type A instead of 1-4, the program doesn't handle that very well. So I would say you need to keep working on it.
Topic archived. No new replies allowed.