Postal Code Validation

This is a discussion on Postal Code Validation within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am getting some problems with validating a postal code in C++. It will catch some of the ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    73

    Postal Code Validation

    Hi everyone,

    I am getting some problems with validating a postal code in C++. It will catch some of the bad data entered if it's not in 'L#L#L#L#' format but not all of it and that's where I'm stuck. I need to check the length of the postal code to ensure that it is just 6 characters and then I need to make sure the format of the 6 characters is 'letter-number-letter-number-letter-number'.

    Here is the code segment for the validation that I have at this point:

    Code:
    string pcode;
    int i;
    int pcodelen;
    int pcode_flag = 0;
    
    while (pcode_flag == 0) {
          cout << "\nEnter your postal code: ";    cin >> pcode;
          pcodelen = pcode.length();
          if (pcodelen != 6) {
             pcode_flag = 0;
             break;
          }
          else {
             for (i = 0; i < 6; i++) {
                if (i % 2 == 0) {
                   if (pcode[i] < 65 || pcode[i] > 90) {
                      pcode_flag = 0;
                      break;
                   }
                   else {
                      pcode_flag = 1;
                   }
                }
                else {
                   if (pcode[i] < 48 || pcode[i] > 57) {
                      pcode_flag = 0;
                      break;
                   }
                   else {
                      pcode_flag = 1;
                   }
                }
             }
          }
    }
    As I said above, it is catching some of the errors in the inputted data but it's not foolproof yet and is still allowing invalid postal codes to be accepted.

    If anyone can tell me how this needs to be revised so that it works fully and correctly, it would be greatly appreciated.

    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    How about
    Code:
    valid = pcode.length() == 6 &&
            isalpha(pcode[0]) && isalpha(pcode[2]) && isalpha(pcode[4]) &&
            isdigit(pcode[1]) && isdigit(pcode[3]) && isdigit(pcode[5]);
    The 'is' functions are in
    #include <cctype>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21