-
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
-
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!
-
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.
-
<cctype> contains functions to check if a character is a number or a string, that's a start...
http://cppreference.com/stdstring/index.html
-
How come I've never heard of postcode? :D
EDIT: Ooops, nevermind. I get it now. I was thinking of something totally different.
-
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
-
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).
-
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?