我今天刚刚开始C++。我正在做一些简单的注册程序。我想验证输入。验证全名和birth_date时,我卡住了。这里是我的要求:在C++中验证字符串和日期输入?
- 全名:我只是想检查其空的,没有标点符号
- date_birth:我知道这是升技棘手。但是如果我可以验证输入是否有效,如:月(1-12),日期(1-30)和年份(不超过当年)应该足够。
任何快速的方法来做到这一点?
编辑:
我试着用Google搜索字符串的验证,我仍然获得了大量的错误。这里是我当前的代码:
string fullname;
do{
cout << endl << "Please enter your fullname";
cin >> fullname;
} while(!ispunct(fullname));
我的错误信息是:
XXXX: no matching function for call to `ispunct(std::string&)'
我已经包括图书馆,这是一个正确的方法来检查字符串输入。你通常如何进行验证?
编辑2:
bool valid;
string fullname;
do{
valid = true;
cout << endl << "Please enter your fullname";
cin >> fullname;
string::iterator it;
for (it=fullname.begin() ; it < fullname.end(); it++)
if(ispunct(*it)){
valid = false;
}
} while(!ispunct(fullname));
它的怪异,我进入: “!!!”,但它仍然被通过。我的代码中有什么错误?
哦,谢谢。所以我存储在字符数组而不是字符串数据类型? – 2010-11-07 18:13:26
@cpp_is_cool,不,你一定要使用'std :: string'。但'std :: string'基本上是内部字符数组。例如,如果你有'std :: string s',并且你说'[3]',它会返回字符串中的第四个'char'。同样,当你迭代字符串时,每次你取消引用迭代器,你都会得到一个单独的字符。 – 2010-11-07 18:15:38
哦..我不知道。 1个快速问题,如果我的字符串是“abcdefg”,有没有一种方法来确定大小?如果不是,我该如何声明for循环。 for(int i = 0; i ??; i ++) – 2010-11-07 18:17:55