Distance






3.16/5 (6 votes)
May 10, 2005
1 min read

37514

733
An article on finding the distance between two Zip codes.
Introduction
This article allows the user to use Zip code look up from entered city, state information. Then the program will display the distance between the two zip codes.
Background
You will notice the demo project is large, 1.28 MB. A compressed copy of the database is included and is unzipped by the program when started. The file zipcode.zip is a private format known only to the included zipdll.dll. When uncompressed, the zipcode.csv file is 8.42 MB which is deleted when the program is closed. The source is a fully functional version if you already have a copy of ZIPCODEWORLD-US-PREMIUM.CSV. The program and database must be in the same directory.
Using the code
To use the program, enter both Zip codes and press the Calculate button. Or, press the Zip Code button and enter city, state. Or, you can enter a * for the state and all states will be listed that match the city. City and state are case insensitive and I used CString.Find()
so you only have to enter the first few letters of a city. Caution: if you wanted "New York" and you only entered "New" you will get a long list. Click the desired item in the list box and follow the instructions.
The real worker is the Parse()
function:
CString CDistanceDlg::Parse(CString &szData) { CString szTmp = ""; int pos = szData.Find("\",", 0); // last string, no more seps if (pos == -1) { szTmp = szData; szTmp.Remove('\"'); return szTmp; } // Save data, remove from left szTmp = szData.Left(pos); szData.Delete(0, pos + 2); szTmp.Remove('\"'); return szTmp; }
Points of Interest
When writing a compound if()
statement:
if((szCity.Find(City, 0) >= 0) && ((szState.Compare(State) == 0) || (State == "*")))
watch the parentheses. The above works, the code below fills the list box with all 79,614 items.
if((szCity.Find(City, 0) >= 0) && (szState.Compare(State) == 0) || (State == "*"))
History
- 9 May, 2005 - Version 1.0.
Credits
CUnzip
class - Florent Jugla- Distance function -
ZipCodeWorld
®