I have to make a project for school. The program is supposed to let me write zip codes and cities to a file, display the contents of the file, search for a city via zip code, and search for a zip code via city. All of them work, except for search for a zip code via city. If I put in data that's in the file, it works. But if I put in data that's not in the file, like Cihcago rather than Chicago, the program breaks. Here's my code:
Why doesn't this work? Thanks for the help!
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
int Menu(int choice);
void addRecords();
void dispRecords();
void dispCity();
void dispZIP();
int main()
{
int choice = 0;
cout << "Welcome to the program!\n";
while (choice != 5)
{
cout << "1.)Add Records\n2.)Display Records\n3.)Display City\n4.)Display ZIP\n5.)Exit\n";
cin >> choice;
cin.ignore();
Menu(choice);
}
}
int Menu(int choice)
{
system("CLS");
switch (choice)
{
case 1: addRecords();
break;
case 2: dispRecords();
break;
case 3: dispCity();
break;
case 4: dispZIP();
break;
case 5:
{
return 0;
}
}
}
void addRecords()
{
ofstream outFile;
outFile.open("Chapter14Lab26.txt");
if (outFile.is_open())
{
string city = " ";
string zip = " ";
while (zip != "0")
{
cout << "Enter a ZIP code. 0 to end.\n";
getline(cin, zip);
if (zip != "0")
{
outFile << zip << "#";
cout << "Enter a city\n";
getline(cin, city);
transform(city.begin(), city.end(), city.begin(), toupper);
outFile << city << endl;
}
}
outFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened. . .\n";
}
void dispRecords()
{
string line = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, line);
cout << line << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}
void dispCity()
{
bool found = false;
string line = " ";
string city = " ";
string zip = " ";
string compare = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
cout << "Enter a ZIP code: ";
getline(cin, zip);
while (!inFile.eof() && found == false)
{
getline(inFile, line);
compare = line.substr(0, 5);
if (compare == zip)
{
cout << "ZIP: " << zip << " City: " << line.substr(6) << endl;
found = true;
}
}
if (found == false)
{
cout << "No city found for ZIP " << zip << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}
void dispZIP()
{
bool found = false;
string line = " ";
string city = " ";
string zip = " ";
string compare = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
cout << "Enter a city: ";
getline(cin, city);
transform(city.begin(), city.end(), city.begin(), toupper);
while (!inFile.eof() && found == false)
{
getline(inFile, line);
compare = line.substr(6);
if (compare == city)
{
cout << "ZIP: " << line.substr(0,5) << " City: " << city << endl;
found = true;
}
}
if (found == false)
{
cout << "No ZIP found for city " << city << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
int Menu(int choice);
void addRecords();
void dispRecords();
void dispCity();
void dispZIP();
int main()
{
int choice = 0;
cout << "Welcome to the program!\n";
while (choice != 5)
{
cout << "1.)Add Records\n2.)Display Records\n3.)Display City\n4.)Display ZIP\n5.)Exit\n";
cin >> choice;
cin.ignore();
Menu(choice);
}
}
int Menu(int choice)
{
system("CLS");
switch (choice)
{
case 1: addRecords();
break;
case 2: dispRecords();
break;
case 3: dispCity();
break;
case 4: dispZIP();
break;
case 5:
{
return 0;
}
}
}
void addRecords()
{
ofstream outFile;
outFile.open("Chapter14Lab26.txt");
if (outFile.is_open())
{
string city = " ";
string zip = " ";
while (zip != "0")
{
cout << "Enter a ZIP code. 0 to end.\n";
getline(cin, zip);
if (zip != "0")
{
outFile << zip << "#";
cout << "Enter a city\n";
getline(cin, city);
transform(city.begin(), city.end(), city.begin(), toupper);
outFile << city << endl;
}
}
outFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened. . .\n";
}
void dispRecords()
{
string line = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, line);
cout << line << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}
void dispCity()
{
bool found = false;
string line = " ";
string city = " ";
string zip = " ";
string compare = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
cout << "Enter a ZIP code: ";
getline(cin, zip);
while (!inFile.eof() && found == false)
{
getline(inFile, line);
compare = line.substr(0, 5);
if (compare == zip)
{
cout << "ZIP: " << zip << " City: " << line.substr(6) << endl;
found = true;
}
}
if (found == false)
{
cout << "No city found for ZIP " << zip << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}
void dispZIP()
{
bool found = false;
string line = " ";
string city = " ";
string zip = " ";
string compare = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
cout << "Enter a city: ";
getline(cin, city);
transform(city.begin(), city.end(), city.begin(), toupper);
while (!inFile.eof() && found == false)
{
getline(inFile, line);
compare = line.substr(6);
if (compare == city)
{
cout << "ZIP: " << line.substr(0,5) << " City: " << city << endl;
found = true;
}
}
if (found == false)
{
cout << "No ZIP found for city " << city << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}