Click here to Skip to main content
15,503,849 members
Articles / Desktop Programming / MFC
Posted 9 May 2005


13 bookmarked


Rate me:
Please Sign up or sign in to vote.
3.16/5 (6 votes)
9 May 20051 min read
An article on finding the distance between two Zip codes.

Sample Image


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.


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 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;
        return szTmp;
    // Save data, remove from left
    szTmp = szData.Left(pos);
    szData.Delete(0, pos + 2);
    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 == "*"))


  • 9 May, 2005 - Version 1.0.


  • CUnzip class - Florent Jugla
  • Distance function - ZipCodeWorld®


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralUsing my own data Pin
Alex Evans10-May-05 21:19
Alex Evans10-May-05 21:19 
GeneralRe: Using my own data Pin
Roger6511-May-05 3:41
Roger6511-May-05 3:41 
GeneralCould not open ZipCode zip file. Pin
WREY10-May-05 10:42
WREY10-May-05 10:42 
GeneralRe: Could not open ZipCode zip file. Pin
Roger6510-May-05 11:12
Roger6510-May-05 11:12 
GeneralVery Cool. Pin
WREY10-May-05 14:24
WREY10-May-05 14:24 
QuestionCanadian postal codes too? Pin
NGS 54967210-May-05 6:22
NGS 54967210-May-05 6:22 
AnswerRe: Canadian postal codes too? Pin
Roger6510-May-05 6:43
Roger6510-May-05 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.