验证最后只有数字 [英] Validation for Having only Numbers at Last

查看:81
本文介绍了验证最后只有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的CPians,





这是Venu,我对C ++有点新鲜,我有一个疑问:



我有一个名为VehicleNumber的字段,应该采用MPQW1029之类的东西。



最后一个四个字符应该是所有数字而不是全部为零。



我需要在按下按钮后检查该格式,如果没有遵循约束,则会出现错误消息应该出现。



我已经在JS中完成了它的工作但我不知道如何用C ++编写它。



任何解决方案都会很明显。



先谢谢你。



问候,



Venu:)

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 :)

推荐答案

你必须将问题分成几部分,如果你'已经在javascript中创建它,将它转换为C ++将很容易。



1.使用scanf左右获取文本。

2.修剪它以获得最后四个字符。

3.使用char数组的索引来检查char by char(我认为你需要的函数是IsDigit())。

4.如果IsDigit是正确的然后你可以继续...

5.在结尾或开头(可能会更好)你可以检查价值是否等于或不同于0000。



我会使用while循环,如:



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...
}





你刚刚看过伪代码...只是为了给你一个如何做的粗略想法。

这是一个很好的程序。



享受它。



分而治之! ;)



PS:

由于您的问题更新:



为了检查字符串不同于某些字符串: http://www.cplusplus.com/reference/string / string / compare / [ ^ ]或 http://www.cplusplus.com/reference/clibrary/cstring/strcmp/ [ ^ ]

亲爱的琼,





其实谢谢你的回复......请找到我得到的错误我跑的时候你说的是



--->''checkstringDifferentThan''未在此范围内宣布



---->''Isdigit''未在t中声明他的范围



任何解决方案都会很明显.......提前致谢.....



问候,
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,


这篇关于验证最后只有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆