locked
What is the best way to do this? RRS feed

  • Question

  • I'm creating a program that is a chapter locator for an organization. It requests the user's zip code and then tells them the location of the nearest chapter. I have not gotten much code written. So far all I have is a simple output and input statement that requests the zip code from the user. When I got to thinking about how the program would use the zip codes to determine which chapter would be the closest I had figured on writing If/Else If statements. However, upon further research and thinking I began to realize that if I were to use If/Else If statements then I would have to write 42,741 statements, because that is how many zip codes there are in the United States.

    Is there a better way to do this?
    Monday, March 1, 2010 10:34 PM

Answers

  • The best thing to use is a map. I would advise std::map. You could populate the map from a file.

    But how are you going to characterize the stores? And how are you going to figure out the nearest one for each zip code.
    David Wilkinson | Visual C++ MVP
    • Marked as answer by Wesley Yao Monday, March 8, 2010 3:07 AM
    Monday, March 1, 2010 11:05 PM
  • Yes, you can not find something which is not a finish solution. You better try to read that symbolic. May i have shorten the way to a target for you too much. So here is a better explanation.

    1. create a class for your expected data. for e.g.

    #include <string>  // For wstring
    class ZipTable
    {
    public:
      ZipTable() {};
      ~ZipTable() {};
     
      // This holds zip code
      unsigned int id;

      // This is for city
      std::wstring city;

      // This is for state
      std::wstring state;

      // This is for country
      std::wstring country;
    };

    2. Now you have to fill up the data in your class array

    ZipTable[][4] simpleZipTable =
      // zip      city                      state                     country
      { 12345, L"ExampleCity 1", L"ExampleState 1", L"ExampleCountry 1" },
      { 12346, L"ExampleCity 2", L"ExampleState 2", L"ExampleCountry 2" },
      ...
    • Marked as answer by Wesley Yao Monday, March 8, 2010 3:06 AM
    Tuesday, March 2, 2010 2:36 AM

All replies

  • 1. Try it with a database, this is used in general to handle data like this. If its only the zips (you always should keep in mind that you have to update them unregular) than you also can use an array (index is the zip code) and couple there your data

    2. You should know each location or region of each zip so that you can calculate the distance between two locations (zips), than you only have to search for a zip and get all regions near by given distance.

    or if you have for e.g. 5 fixed locations you only group all zips are near enough to them. if the given zip part of one or more group you have your result.

    Monday, March 1, 2010 10:54 PM
  • So, is it possible so that when the user enter's the zip code into the input then the program searches through the database for that zip code and then pulls the information from it?
    Monday, March 1, 2010 11:02 PM
  • The best thing to use is a map. I would advise std::map. You could populate the map from a file.

    But how are you going to characterize the stores? And how are you going to figure out the nearest one for each zip code.
    David Wilkinson | Visual C++ MVP
    • Marked as answer by Wesley Yao Monday, March 8, 2010 3:07 AM
    Monday, March 1, 2010 11:05 PM
  • I'm not familiar with std::map
    Monday, March 1, 2010 11:12 PM
  • Well not that easy, you should know how a database is used for in general. than you will learn that you can ask a database with an specific language for e.g SQL with a statement for e.g. SELECT zipId, region, whatever FROM table1 WHERE region is < givenRegion + 5 AND WHERE region is > givenRegion - 5; this might not be a correct solution but should lead you the way
    Monday, March 1, 2010 11:13 PM
  • If you want to use distance and regions you better try to have an array with regions coordinates like xStart, yStart, xEnd and yEnd in correlation to each zip. Then you are able to compare each region if it is in range of your given region (for e.g. a big square around your companies coordinates).

    or http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29 for a map desciption
    Monday, March 1, 2010 11:26 PM
  • I found an Excel database online that has all of the US zip codes listed. The A column contains the zip codes beginning with A3 going all the way to A42743. The city is listed in D3 to D42743. The state is listed in E3 to E42743 and the county is listed in F3 to F42743.

    The tab at the bottom of the database says zip_codes. I'm assuming that would be the same as the table1 you had talked about earlier. I'm not sure about the givenRegion.

    Sorry, if this seems a little too easy to you or to others. My college C++ class doesn't get into databases. So I'm pretty much having to figure it out myself.

    Tuesday, March 2, 2010 12:17 AM
  • This may sound like a dump question, but when you are writing an If statement and you are wanting to say that If number is equal to 1, 25, or 30 then this happens can you write that all in one If statement rather than using the If/Else If statement? If you can, then would it look something like this?

    If ( number = 1, 25, 30)
     answer = True

    Tuesday, March 2, 2010 12:59 AM
  • Ah well, ok, so you need first the basics.

    1. Your Excel list is a very simple form of an database. And to use it you can realize this in different ways. One is to put all in a real database and then connect the data with your application. This would be a solution i prefer. An other solution, which you should try first is to put all this data in a simple array of a class. For. e.g. ClassZipTable[][4] simpleZipTable= {1.zip, 1.city, 1.state, 1.country}, {2.zip, 2.city, 2.state, 2.country}, .... You have to do this by your own. There are several other possible solutions like useing a web service which do this all for you or whatever.

    2. A colon of if statements should be better done like this:

    switch(number)
    {
      case 1:
      case 25:
      case 30:
      {
        //TODO: Something meaningful here
        break;
      }
      case 18:
      case 22:
      {
        //TODO: Something meaningful here
        break;
      }
      default:
      {
        //TODO: This is similar to the else part
        break;
      }
    }
    Tuesday, March 2, 2010 1:23 AM
  • Correct me if I'm wrong, but I'm assuming that since you said that I need to put it all in a real database that I need to copy and paste each column into my own database such as an Access database?

    Tuesday, March 2, 2010 1:51 AM
  • Well, an access database has some strange limitations, but yes, it could be used for an easy start. Copy and paste *should* be compatible with your data coming from an excel list. Alternatively you can try to export the data in excel into an .csv (comma separated) file and than import the data in access a bit more accurate. In any case you have to check all results very painstakingly.

    You should also keep in mind that you have to deliver this all together when you want to use your software outside your development environment. That means that your customer must have the same or newer version of access installed.
    Tuesday, March 2, 2010 2:05 AM
  • Ok, I now have those 4 columns in an Access Database.

    I tried googling ClassZipTable[][4] simpleZipTable so that I could understand what you was saying but the search didn't find anything.
    Tuesday, March 2, 2010 2:19 AM
  • Yes, you can not find something which is not a finish solution. You better try to read that symbolic. May i have shorten the way to a target for you too much. So here is a better explanation.

    1. create a class for your expected data. for e.g.

    #include <string>  // For wstring
    class ZipTable
    {
    public:
      ZipTable() {};
      ~ZipTable() {};
     
      // This holds zip code
      unsigned int id;

      // This is for city
      std::wstring city;

      // This is for state
      std::wstring state;

      // This is for country
      std::wstring country;
    };

    2. Now you have to fill up the data in your class array

    ZipTable[][4] simpleZipTable =
      // zip      city                      state                     country
      { 12345, L"ExampleCity 1", L"ExampleState 1", L"ExampleCountry 1" },
      { 12346, L"ExampleCity 2", L"ExampleState 2", L"ExampleCountry 2" },
      ...
    • Marked as answer by Wesley Yao Monday, March 8, 2010 3:06 AM
    Tuesday, March 2, 2010 2:36 AM