Thread: Problem with desimal number starting with 0

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    29

    Problem with desimal number starting with 0

    When I try to put in a zip code starting with 0 I get an error saying invalid octal number as in NJ 08088 or RI 02901. How do I tell it that it is a decimal number.
    Code:
    // states.h
    
    // header file for the states program
    
    
    // data structure declartion and initialization
    
    // initialize the state data in the array of the state structure.
    
    struct state_st{
     char *code;  //postal code for state name
     char *name; // state name
     char *capital;  // capital city
     unsigned int zip;  // zip code
     unsigned int population; // in thousands
     unsigned int area;  // square miles
    } States[] = {
     { "CO", "Colorado", "Denver", 80012, 2208, 104247 },
     { "MA", "Massachusetts", "Boston", 02108, 5689, 8257 },
     { "MD", "Maryland", "Annapolis", 21401, 3923, 10577 },
     { "RI", "Road Island", "Providence", 02901, 950, 1214 },
     { "NJ", "New Jersey", "Trenton", 08801, 8938, 8729 },
     {"PA", "Pennsylvania", "Harrisgurg", 17101, 1279, 44742 },
     { "TX", "Taxas", "Austin", 73301, 2696, 268820  },
     { "AK", "Alaska", "Juneau", 99801, 7367 , 663384 },
     { "CA", "California", "Sacramento", 94203, 3880, 163694 },
     { "AL", "Alabama", "Montgomery", 36101, 4849, 52419 },
     { "AZ", "Arizona", "Phoenix",85001, 6731, 113998 }
    };
    
    #define NSTATES sizeof States / sizeof (struct state_st)

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A number beginning with zero indicates octal.

    I would suggest you store zip codes as strings, since it's unlikely they'll be used as integers for any reason.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    29
    Thanks simple fix but it worked and I don't think I would have ever thought of it.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome. A lot of peoples first instinct when seeing information represented with numbers is to use an integer type. However, certain things (zip codes, phone numbers, etc) are better stored as strings, since values like those are not typically used and/or modified algebraically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with starting Problem 1
    By Mickey1990 in forum C Programming
    Replies: 8
    Last Post: 02-06-2014, 04:22 PM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. Starting with Dev C ; a problem
    By mabauti in forum C++ Programming
    Replies: 18
    Last Post: 12-05-2007, 11:16 AM
  4. Problem starting a thread from a system service
    By abachler in forum Windows Programming
    Replies: 2
    Last Post: 08-23-2007, 12:27 PM
  5. Replies: 27
    Last Post: 10-11-2006, 04:27 AM