Postcode Validation

This is a discussion on Postcode Validation within the C++ Programming forums, part of the General Programming Boards category; hi everyone im new to this programming, just wondered, i have this code and does anyone know how i can ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Postcode Validation

    hi everyone im new to this programming,

    just wondered, i have this code and does anyone know how i can modify it so it will validate a postcode.

    Code:
    #include <iostream>
    // need cmath if using ceil() library function
    #include <cmath>
    using namespace std;
    
    const char POUND = 156; // value to output a pound sign, e.g.
    //	cout << POUND << "9" << endl;
    // would output: 9.
    // An alternative to this is to use the backslash sequence \234, e.g.
    //	cout << "\2347" << endl;
    // would output: 7
    
    // function prototypes as given in the assignment script
    bool isValidPostcode(char postcode[]);
    float calculateCost( int numberOfBottles);
    float calculateVAT( float cost);
    float calculateDeliveryCharge(int numberOfBottles);
    void displayOrder(char initials[], char surname[ ], char address[],
    				  char postcode[], int numberOfBottles, float cost, 
                      float costPlusVAT, float deliveryCharge,
    			      float totalCost );
    bool enterAnotherOrder();
    
    
    void main()
    {
    
    	// main program loop
    	do
    	{	
    		// validation loop for post code
    		do
    		{
    	
    		} while(!isValidPostcode(postcode))
    		
    		calculateCost(numberOfBottles);
    
    		calculateVAT(cost);
    		
    		calculateDeliveryCharge(numberOfBottles);
    		
    		displayOrder(initials, surname, address, postcode, numberOfBottles, cost,
    			costPlusVAT, deliveryCharge, totalCost);
    	} while(enterAnotherOrder());	
    }
    
    system("pause");
    }
    Thanks for your help

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I worked for Royal Mail for a short time, and I can tell you that validating post codes is NOT as simple as it may seem (Partially for historial reasons, Royal Mail did not think out their post code system very well)

    Anyway, here are the "rules" of the post code system, they are not foolproof, but here's the closest you can get to validation.

    1) The 2nd part of a post code cannot contain the letters C, I, K, M, O, V
    2) The 1st part of a post code must begin with a letter.
    3) The 2nd part of a post code must begin with a number.
    4) The 2nd part of a post code must end with 2 letters.
    5) A postcode must be between 5 and 7 significant characters (plus a space)
    6) The first letter of the postcode cannot be Q, V or X.

    Not that there are 6 different formats that you can test for + examples of valid postcodes (A=Alpha N=Numeric) :

    Code:
    AN NAA 	        M1 1AA
    ANN NAA 	M60 1NW
    AAN NAA 	CR2 6XH
    AANN NAA 	DN55 1PT
    ANA NAA 	W1A 1HQ
    AANA NAA 	EC1A 1BB
    Special Case: Giro bank use a postcode which breaks the rules: GIR 0AA .


    To summarise, there's no quick and simple way to implement all of this. First of all, I reccomend you split the postcode into 2 seperate strings - the Outcode (the first half) and the Incode (the 2nd half)
    eg, "SW1A" and "0AA" - Which, is the postcode for Westminster Parliament, by the way.

    then write a bunch of seperate functions which validate against some or all of those rules on each incode/outcode respectively.

    The Outcode is always much harder to validate, because the rules are far less well defined. the Incode is more trivial.

    Good Luck!
    Last edited by Bench82; 03-15-2006 at 07:13 PM.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    Thanks,

    Do you or anyone know how i can start it off by checking to see if a user has inputted a letter or a number because i am really stuck?

    Thanks for your help.

  4. #4
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    <cctype> contains functions to check if a character is a number or a string, that's a start...

    http://cppreference.com/stdstring/index.html
    There is a difference between tedious and difficult.

  5. #5
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    How come I've never heard of postcode?

    EDIT: Ooops, nevermind. I get it now. I was thinking of something totally different.

  6. #6
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    grrrr, this postcode thing is doing ma head in, lol, i bet it is all basic stuff for you lot but im really finding it hard as you all can prob tell, lol

    anyone else seen ma code n knw what i can do?

    Thanks

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Like I said before, divide the postcode up into Outcode and Incode before you do anything else.
    you will have to do alot of string manipulation - read up on the C++ <string> library, it will make your life much easier (do not use char* for this unless you want a really hard time).

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    cool, think i might be gettin somewhere on it, just another question do how do u check to see if it is a letter going in or a number?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ validation
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 02:53 PM
  2. Please Help!!! Validation loop for postcode!!
    By LouiseJon in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2006, 01:17 PM
  3. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  4. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 02:10 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM

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