9 Replies - 961 Views - Last Post: 29 August 2009 - 12:06 AM Rate Topic: -----

#1 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Writing to and reading from a file

Post icon  Posted 28 August 2009 - 10:16 PM

This program is suppose to allow the user to add cities with coresponding zip codes. It should save all entered cities and zips so later a user can come back and enter a zip and display the proper city for it. If no matching city is found for the entered zip code it should display an error to the user for an invalid zip. I know I am not doing the display portion correctly since I am not getting the desired result of a city name when entering the zip code. I am not exactly sure what the correct way to display what I want is. Can someone please point back on the correct track please.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Writing to and reading from a file

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 10:53 PM

	if (code = zip)


This part should be
	if (code == zip)


Was This Post Helpful? 1
  • +
  • -

#3 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 11:05 PM

Ok this is what your code should look like
//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
		while(zip!=code){//going throw all lines of records
		getline(inFile, city, '#');
		inFile >> zip;
		}
	}
	if (code == zip)//= is to assign a value == is to compare
	{
		inFile.ignore('#',zip);//to ignore everything but the city
		inFile >> city;
		

		inFile.close();

		//display the City
		cout << endl << city << endl;
	}
	else
		cout << "File could not be opened" << endl;
	//end if
}



Hope it helps xD

This post has been edited by poncho4all: 28 August 2009 - 11:05 PM

Was This Post Helpful? 1
  • +
  • -

#4 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 11:21 PM

Its actually still not displaying the city when I enter the zip codes. its displaying a number.

Also how would I go about making it display multiple cities? would i set up the loop how I did to enter the data?

Thanks

Attached File(s)


This post has been edited by vwyodapink: 28 August 2009 - 11:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 11:23 PM

Glad i helped xD

And is no problem to take time to help you if i can xD
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 11:33 PM

Btw the data i tried the program with was
Guatemala 12
Salvador 11
Guam 111

The text file looks like this

Guatemala#12
Salvador#11
Guam#111

And it works

View Postvwyodapink, on 28 Aug, 2009 - 10:21 PM, said:

Its actually still not displaying the city when I enter the zip codes. its displaying a number.

Also how would I go about making it display multiple cities? would i set up the loop how I did to enter the data?

Thanks


Do you mean something like multiple cities with the same zipcode?
Was This Post Helpful? 0
  • +
  • -

#7 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 11:40 PM

no like Darien 60561, Hinsdale 60544, Glen Ellyn 60137, Downers Grove 60135, Burr Ridge 60136..

I attached the whole file with the text file filled how the book wants it for this file. incase that would help better. I hate when I get most of a concept but not one major part :P
Was This Post Helpful? 0
  • +
  • -

#8 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Writing to and reading from a file

Posted 28 August 2009 - 11:56 PM

Well if you take of the
inFile >> city
it should work you see this line is messing up the reading of the city element just comment it out and try it as for the exact why it does this im not really sure but maybe some else will read this and help someone with better concept handling than me xD

Sry if im confusing you more, im really bad at explaining
Was This Post Helpful? 1
  • +
  • -

#9 vwyodapink  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 141
  • Joined: 12-May 09

Re: Writing to and reading from a file

Posted 29 August 2009 - 12:04 AM

No I think I get what your saying. I have the same problem as you I most of the time do not make things easier to understand :P I will go try it out and see what happens. Thanks again !!

You were right that did it. I dont get why that made it work either. I would think you would have to something putting it into the city variable to make it display but I guess it just goes there automatically. Thanks again now I am going to make it so it will ask for multiple zips. Should be some what easy.

Appriciate your in put
Was This Post Helpful? 0
  • +
  • -

#10 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Writing to and reading from a file

Posted 29 August 2009 - 12:06 AM

I hope it works!

If not let me know what the errors were xD
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1