Hello
I just wanted a help on how do I do this.
Okay! what I want to do is that a person should enter an alphanumeric code(without any space) containing 2 alphabets and 3 numbers. The program should accept only tht anything less or more should display an err. msg.
The program is as follows
char ticketno[5];
cout<<"enter the ticket number";
cin>>ticketno;
the rest of the modification i don't know how to do.
Please help, this is my earnest plea.
Thx in advance
I would advise you to read on how to use regex to compare your user input against a pattern that you want to accept and then reject anything that doesn't meet the regex pattern.
you can use arrays or strings and can split your code in following parts
as you have told containing 2 alphabets and 3 numbers
i.e. max 5 no's you can firstly check this:
if(length_of_ticketno is equal to 5)
{
//do some thing like to display msgs.
// you can use here checks that will check that first two elements must
// be alphabets and rest 3 must be digits.
}
else
{
//display that you have entered wrong input. Try again
}
A c-style string is a sequence of characters terminated by a null character (which is logically not part of the sequence).
The size of the array ticketno should be one greater than the maximum number of characters that we want to read.
1 2
enum { NALPHA = 2, NDIGITS = 3, NCHARS = NALPHA + NDIGITS, SZ = NCHARS+1 } ;
char ticketno[SZ] ; // array has one extra char for the terminating null character
The user may try to enter more than NCHARS (five) characters; we need to limit the input to a maximum of NCHARS characters.
std::cin >> std::setw(SZ) >> ticketno ; // limit input to a maximum of NCHARS characters
Now count the number of alpha and numeric characters.
1 2 3 4 5 6 7 8 9
// count number of alphas and digits
int alphas = 0 ;
int digits = 0 ;
for( int i = 0 ; ticketno[i] != 0 ; ++i ) // for every char upto the terminating null character
{
constchar c = ticketno[i] ;
if( std::isalpha(c) ) ++alphas ;
elseif( std::isdigit(c) ) ++digits ;
}
And validate that there are NALPHA (two) alphabetic characters and NDIGITS (three) digits.
1 2
if( alphas==NALPHA && digits==NDIGITS ) std::cout << "ok\n" ;
else std::cerr << "'" << ticketno << "' is not a valid ticket number\n" ;
> Which alphabets do you want to use? Greek and Hebrew?