Thursday, January 26, 2012

I'd like to write a programm in C++ language that matches a given ZIP Code to a City Name. I haven't a clue :(?

A text file with over 200 ZIP Codes has been provided and my Program must first read this file and from this information, the Program should be able to tell to which area a ZIP Code inputted by the user belongs.

Thanks a lot :))I'd like to write a programm in C++ language that matches a given ZIP Code to a City Name. I haven't a clue :(?
Well, here's a class definition (header file) that you can use. You'll have to create the actual code to do it (.cpp file).



Here's and example of operator overloading for the redirection operators: http://www.codersource.net/c/c-miscellan鈥?/a>



Here's the getline() for stream: http://www.cplusplus.com/reference/strin鈥?/a>



And here's how you can use "push_back()" for the 'zipCodeDB' variable: http://www.cplusplus.com/reference/stl/v鈥?/a>

Obviously you'll notice that a vector is just a container that doesn't care what it holds.



#include %26lt;iostream%26gt;

#include %26lt;vector%26gt;

using namespace std;



class ZIP_Code

{

聽聽聽public:

聽聽聽聽聽聽ZIP_Code();

聽聽聽聽聽聽~ZIP_Code();



聽聽聽聽聽聽string city;

聽聽聽聽聽聽string add_on;

聽聽聽聽聽聽string ZIP;

聽聽聽聽聽聽string dailing_code;聽聽聽// Is this supposed to be "dialing" code?

聽聽聽聽聽聽string state;

};



class ZIPCodes

{

聽聽聽public:

聽聽聽聽聽聽ZIPCodes();

聽聽聽聽聽聽~ZIPCodes();



聽聽聽聽聽聽// Stream read/write operators

聽聽聽聽聽聽friend istream%26amp; operator %26gt;%26gt;(istream %26amp;is, ZIPCodes %26amp;obj);

聽聽聽聽聽聽friend ostream%26amp; operator %26lt;%26lt;(ostream %26amp;os, const ZIPCodes %26amp;obj);



聽聽聽聽聽聽// Function to locate a ZIP code

聽聽聽聽聽聽ZIP_Code %26amp;findZIPforCity(string cityToFind)

聽聽聽聽聽聽{

聽聽聽聽聽聽聽聽聽vector%26lt;ZIP_Code%26gt;::iterator it;

聽聽聽聽聽聽聽聽聽static ZIP_Code rtrn_val;



聽聽聽聽聽聽聽聽聽this-%26gt;cityToFind = cityToFind;

聽聽聽聽聽聽聽聽聽it = find_if(zipCodeDB.begin(), zipCodeDB.end(), IsCityMatch);

聽聽聽聽聽聽聽聽聽rtrn_val = *it;



聽聽聽聽聽聽聽聽聽return (%26amp;rtrn_val);

聽聽聽聽聽聽};



聽聽聽private:

聽聽聽聽聽聽vector%26lt;ZIP_Code%26gt; zipCodeDB;

聽聽聽聽聽聽string cityToFind;

聽聽聽聽聽聽const string DELIM = ";";



聽聽聽聽聽聽// Function to compare for city match

聽聽聽聽聽聽bool IsCityMatch(ZIP_Code compareZIP)

聽聽聽聽聽聽{

聽聽聽聽聽聽聽聽聽if ( this-%26gt;cityToFind.compare( compareZIP.city ) )

聽聽聽聽聽聽聽聽聽聽聽聽return true;

聽聽聽聽聽聽聽聽聽else

聽聽聽聽聽聽聽聽聽聽聽聽return false;

聽聽聽聽聽聽};

};
This is a simple comma delimited text format. You need to read the data into an array of structs in which the data will reside during the runtime of the program. You can, in C++ design a class for this, and in this class you may create a function that will accept a stream as an argument and then read a line from the file and parse it into the struct, returning a zero if the eof has been reached. Another function may be created in the class which accepts a code as an argument and then iterates through the list of structs to find the proper corresponding zip code. When it does so, it can return the entire struct, which will have the city, zip, ph.no, and state included.I'd like to write a programm in C++ language that matches a given ZIP Code to a City Name. I haven't a clue :(?
Does your zip file contains the city names or only zip codes? If the user inputs the zip code then the file must contain the city names.

Anyway you need to open the file in text mode and read the contents into an array and match the zipcode



A piece of code:

ifstream myfile ("example.txt");

while ( myfile.good() )

{

getline (myfile,line);I'd like to write a programm in C++ language that matches a given ZIP Code to a City Name. I haven't a clue :(?
You're talking about U.S. Zip codes, I suppose, which all have five digits. So you can declare an array of std::string with 100000 elements. You can initialize it with empty strings. Then go through the file and assign the appropriate array elements upon parsing each line. Then all relevant data will be in your array.



Edit: Regarding additional details: Oh okay. I'm wondering if you are allowed to modify your input file. Because if you use whitespace (for example, tab characters) instead of semicolons to separate data, then you can use fgets() along with sscanf(line, "%s %s %d", %26amp;city, %26amp;dummy, %26amp;zip); where line is the variable that holds the result of fgets().

No comments:

Post a Comment