所以我有一个问题。
我需要一个字符串(我不能将它作为一个 int,它必须定义为一个字符串)并且在 set 方法中,我必须验证该数字是 9 位还是 13 位长并且只包含数字。
我可以使用 str.length() 验证长度,但无法验证输入是否仅包含数字。
这是我当前的代码:
void Book::setISBN(string ISBN)
{
//validate the length here ALSO NEED TO VALIDATE ITS A NUMBER
if (ISBN.length() >= 9 && ISBN.length() <= 13) {
this->ISBN = ISBN;
}
else {
this->ISBN = '0';
}
}
这是它将提取信息的代码:
Book b = Book();
b.setISBN("23");
b.setAuthor("R.L. Stine");
b.setTitle("Goosebumps");
b.setPrice(25.00);
Book b2 = Book("thisisshith", "Stephen King", "IT", 20.32);
第一部分,b 工作,因为 setISBN("23") 将返回零,但 b2 "thisisshith" 就这样返回。我也需要它返回为 0。如果它是一组长度在 9-13 之间的数字,那么它将正确返回。
任何帮助,将不胜感激。
我试过 isdigit() 但说它不能在字符串和 int 之间转换。