Thanks i advance!
//Ch13AppE01.cpp
//This program allows the user to enter a zip code and city and have it stored on a file. It will also display a city with
//an entered zip code.
#include <iostream>
#include <string>
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::ios;
//function prototypes
int displayMenu ();
void addInfo();
void displayCity();
int main()
{
int menuChoice = 0;
//display menu and get choice
menuChoice = displayMenu();
//call appropriate function or display error
while (menuChoice !=3)
{
if(menuChoice == 1)
addInfo();
else if(menuChoice == 2)
displayCity();
else
cout << "Invalid choice" << endl;
//end ifs
//display menu and get choice
menuChoice = displayMenu();
}//end while
return 0;
}
// Function definitions
int displayMenu()
{
//display a menu and then gets and returns users choice
int choice = 0;
cout << "1 Add Information" << endl;
cout << "2 Display City" << endl;
cout << "3 Exit Program" << endl;
cout << "Enter your choice: " << endl;
cin >> choice;
cin.ignore(100, '\n');
return choice;
}//end of displayMenu function
void addInfo()
{
//writes records to a sequential access file
string city = "";
int zip = 0;
//create file object and open the file
ofstream outFile;
outFile.open("city.txt", ios::app);
//determine whether the file was opened
if (outFile.is_open())
{
//get the name of the city
cout << "Enter the name of the City (X to stop): ";
getline(cin, city);
while (city != "X" && city != "x")
{
//get the zip code
cout << "Enter the Zip Code for the city entered: ";
cin >> zip;
cin.ignore(100, '\n');
//write the record
outFile << city << '#' << zip << endl;
//get another city
cout << "Enter the name of the City (X to stop): ";
getline(cin, city);
}//end while
//close the file
outFile.close();
}
else
cout << "File could not be opened" << endl;
//end if
}//end of addInfo function
void displayCity()
{
//reads records from a file and displays the City to the corresponding zip code entered by the user
string city = "";
int zip = 0;
int code = 0;
//create file object and open the file
ifstream inFile;
inFile.open("city.txt", ios::in);
//determine whether the file was opened
if (inFile.is_open())
{
//Get zip code from user
cout << "Enter the Zip Code to display the corresponding City: " << endl;
cin >> code;
//read a record
getline(inFile, city, '#');
inFile >> zip;
}
if (code = zip)
{
getline(inFile, city, '#');
inFile >> city;
inFile.ignore();
inFile.close();
//display the City
cout << endl << city << endl;
}
else
cout << "File could not be opened" << endl;
//end if
}
This post has been edited by vwyodapink: 28 August 2009 - 10:18 PM

New Topic/Question
Reply



MultiQuote






|