[英]User Input Validation Using Character Array in C++
我正在創建一個 C++ 程序來使用 C++ 中的函數來驗證書籍 ID。 如果輸入有效,程序必須返回 1,如果輸入無效,程序必須返回 0。 輸入模式:“123-AB-12345” 這將被視為有效輸入。 有效輸入為: (a) 總字符數必須為 12 (b) 前三個字符必須為 1 到 9 之間的整數。 (c) 第 4 和第 7 個字符必須是漢字“-”。 (d) 最后 5 個字符必須是 1 到 9 的整數。
我嘗試了以下方法,但沒有得到想要的答案。 需要幫助lz
#include<iostream>
using namespace std;
bool isValidBookId(char bookId[13]);
int main()
{
char book[13];
cin.getline(book,12);
bool id = isValidBookId(book);
cout<<id;
}
bool isValidBookId(char bookId[13])
{
int i;
bool check1,check2,check3,check4,check5,check6;
check1=check2=check3=check4=check5=true;
if(bookId[12]=='\0'){
check1=true;
}
if(bookId[3]=='-')
{
check2=true;
}
if(bookId[6]=='-')
{
check3=true;
}
for(i=0; i<3;i++){
if(bookId[i]>=0 || bookId[i]<=9)
{
check4=true;
}
}
if(bookId[i]>= 'A' || bookId[i]<= 'Z')
{
check5=true;
}
for(i=7; i<12; i++)
{
if(bookId[i]>=0 || bookId[i]<=9)
{
check6=true;
}
}
if(check1==true && check2==true && check3==true && check4==true && check5==true && check6==true)
{
return true;
}
else
{
return false;
}
}
您的代碼中有一些錯誤。
首先,您將所有檢查初始化為true
,並且永遠不要將任何設置為false
,因此答案將始終為true
。 實際上,您希望將它們全部初始化為false
,並在滿足所有條件時更改為true
,或者假設為true
,並在false
條件時設置為false
。
其次,您對0-9
值的檢查不正確。 您不能將bookId[i]
與0
進行比較,而是要將其與字符'0'
進行比較。 另請注意,您的問題也說1-9
而不是0-9
第三,您對AZ
的檢查不正確(注意,此問題也適用於0-9
)。 你的代碼基本上是說bookId[i]
大於或等於 'A' 或小於或等於Z
,這總是true
。
我在下面寫了你的代碼:
bool isValidBookId( char bookId[13] ) {
if ( bookId[12] != '\0' )
return false;
if ( bookId[3] != '-' )
return false;
if ( bookId[6] != '-' )
return false;
for ( int i = 0; i < 3; i++ ) {
if ( bookId[i] < '1' || bookId[i] > '9' ) {
return false;
}
}
for ( int i = 4; i < 6; i++ ) {
if ( bookId[i] < 'A' || bookId[i] > 'Z' ) {
return false;
}
}
for ( int i = 7; i < 12; i++ ) {
if ( bookId[i] < '1' || bookId[i] > '9' ) {
return false;
}
}
return true;
}
此方法不需要任何布爾變量。 相反,我假設true
(最后一個return
語句)並嘗試證明false
。 一旦出現false
,您就可以返回而無需進行任何其他檢查。
由於給定的代碼是 C 風格的格式,我想在 C++ 中提出 2 個額外的解決方案。 我認為任務無論如何都要考慮模式以及如何檢測這些模式。
在我的第一個解決方案中,我只是添加了更多 C++ 元素。 第二種解決方案應該是正確的。
請參見:
#include <iostream>
#include <regex>
#include <string>
#include <cctype>
#include <vector>
bool isValidBookId1(const std::string& bookId) {
// Lambda to detect hyphen
auto ishyphen = [](int i){ return static_cast<int>(i == '-');};
// First check size of given string
bool result{(bookId.size() == 12)};
// Define the position of the types
std::vector<size_t> digitIndex{0,1,2,7,8,9,10,11};
std::vector<size_t> letterIndex{4,5};
std::vector<size_t> hyphenIndex{3,6};
// Check types
if (result) for (size_t index : digitIndex) result = result && std::isdigit(bookId[index]);
if (result) for (size_t index : letterIndex) result = result && std::isupper(bookId[index]);
if (result) for (size_t index : hyphenIndex) result = result && ishyphen(bookId[index]);
// Return resulting value
return result;
}
bool isValidBookId2(const std::string& bookId) {
// Define pattern as a regex
std::regex re{R"(\d{3}-[A-Z]{2}-\d{5})"};
// Check, if the book id matches the pattern
return std::regex_match(bookId, re);
}
int main()
{
// Get input from user
if (std::string line{}; std::getline(std::cin, line)) {
std::cout << "Check for valid Book input 1: " << isValidBookId1(line) << "\n";
std::cout << "Check for valid Book input 2: " << isValidBookId2(line) << "\n";
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.