Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear CPians,


This is Venu, I am a bit new to C++, I have a doubt:

I have a field by name "VehicleNumber" and that should take something like "MPQW1029".

The last four characters should be all digits and not all of them zeroes.

I need to check that format after pressing a button and if the constraints are not followed then an error message should appear.

I have done the in JS and it worked but im not knowing how to code it in C++.

Any Solution would be appreciable.

Thank you in Advance.

Regards,

Venu :)
Posted
Updated 13-Aug-12 4:18am
v2
Comments
nv3 13-Aug-12 7:57am    
So where is the problem? When entering the license plate data, parse the string with sscanf or any other parser/tokenizer, look at the four digits and decide whether it's a legal string; otherwise produce a message box with an appropriate error message.
Kenneth Haugland 13-Aug-12 10:21am    
How about String manipulation, RegEx or something simular? Were are you stuck anyway, methods, creation subs functions or?

You have to divide the problem in parts, and, if you've already made it in javascript, translating it to C++ will be easy.

1. Get the text, using scanf or so.
2. Trim it to get the last four chars.
3. use an index of the char array to check char by char (I think the function you need is IsDigit()).
4. if IsDigit is TRUE then you can continue...
5. at the end or at the beginning (probably it would be better) you can check if the value is equal or different than 0000.

I would use a while loop like:

C++
// here you are happy...
bool bAllOk = checkstringDifferentThan('0000');
int iCurrentNumber = 1;
while bAllOk && iCurrentNumber < 5
{
  bAllOk = IsDigit(plateNumbersArray[iCurrentNumber]);
  iCurrentNumber ++;
}

if (!bAllOk)
{
  // here you are not happy...
}


You've just seen pseudocode... only to give you a rough idea on how to do it.
It is a nice program to do.

Enjoy it.

Divide and conquer! ;)

PS:
Updates due to your questions:

In order to check than a string is different than something: http://www.cplusplus.com/reference/string/string/compare/[^] or http://www.cplusplus.com/reference/clibrary/cstring/strcmp/[^]
 
Share this answer
 
v3
Comments
StarRocksAgain 14-Aug-12 3:03am    
Hi,

What exactly checkstringDifferentThan and plateNumbersArray is im not understanding that........Can you just explain me that it would be greatful if you do so...

Regards.
Joan M 14-Aug-12 6:40am    
plateNumberArray is the name of the string (group of chars) where you've stored the data.
checkstringDifferentThan... look at the answer again I've updated it with two links.
Keep in mind that this is pseudocode.
BillW33 15-Aug-12 8:55am    
Nice answer, +5 :)
Joan M 15-Aug-12 9:00am    
Thank you CIDev! ;)
Dear Joan,


Actually thanks for the reply......Please find the errors that i am getting when i run as u said

--->'checkstringDifferentThan' was not declared in this scope

---->'Isdigit' was not declared in this scope

Any solution would be appreciable.......Thanks in Advance.....

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900