Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Linux
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 Smile | :)
Posted 13-Aug-12 2:36am
Edited 13-Aug-12 5:18am
Joan Murt82.8K
v2
Comments
nv3 at 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 at 13-Aug-12 10:21am
   
How about String manipulation, RegEx or something simular? Were are you stuck anyway, methods, creation subs functions or?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
// 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! Wink | ;)
 
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/[^]
  Permalink  
v3
Comments
StarRocksAgain at 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 Murt at 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.
CIDev at 15-Aug-12 8:55am
   
Nice answer, +5 :)
Joan Murt at 15-Aug-12 9:00am
   
Thank you CIDev! ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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,
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100