Click here to Skip to main content
13,451,369 members (51,908 online)
Rate this:
Please Sign up or sign in to vote.
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.


Venu :)
Posted 13-Aug-12 1:36am
Updated 13-Aug-12 4:18am
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?
Rate this: bad
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! ;)

Updates due to your questions:

In order to check than a string is different than something:[^] or[^]
StarRocksAgain 14-Aug-12 3:03am

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

Joan Murt 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 15-Aug-12 8:55am
Nice answer, +5 :)
Joan Murt 15-Aug-12 9:00am
Thank you CIDev! ;)
Rate this: bad
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.....


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180318.3 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2018
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