问题
I am creating a c++ program to validate book ID using function in c++. The program must return 1 if the input is valid and 0 if the input is invalid. INPUT Pattern: "123-AB-12345" This will be considered as a valid input. The valid input is: (a) Total characters must be 12 (b) First three characters must be integers from 1 to 9 each. (c) 4th and 7th characters must be hiphen "-". (d) Last 5 characters must be integers from 1 to 9 each.
I tried the following way but I am not getting the desired answer. Need help plz
#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;
}
}
回答1:
There's a few errors that you have in your code.
First, you initialize all your checks to true
, and never set anything to false
, so the answer will always be true
. Realistically, you want to initialize them all to false
, and change to true
when all the conditions are met, or assume true
, and set to false
when the condition is not met.
Second, your check for the values 0-9
is incorrect. You cannot compare bookId[i]
to 0
, you want to compare it to the character '0'
. Also note that the question you have also says 1-9
not 0-9
Third, your check for A-Z
is incorrect (note, this issue also applies to 0-9
). You're code basically says is bookId[i]
greater than or equal to 'A' OR less than or equal to Z
, which is always going to be true
.
I've written your code below:
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;
}
This method doesn't require any Boolean variables. Instead, I assume true
(the last return
statement) and instead try to prove false
. As soon as something is false
, you can return without doing any other checks.
回答2:
Since the given code is in C-Style format, I would like to present 2 additional solutions in C++. I think the task is anyway to think about patterns and how these could be detected.
In my first solutions I just added more C++ elements. The 2nd solution should be the correct on.
Please see:
#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;
}
来源:https://stackoverflow.com/questions/59568183/user-input-validation-using-character-array-in-c