//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();
elseif (menuChoice == 2)
displayCity();
elseif (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()
{
}
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.
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.
//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();
elseif (menuChoice == 2)
displayCity();
elseif (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()
{
}