0

I am creating a C++ program to validate book name using a function in c++. The function must return 1 if the input is valid and 0 if the input is invalid. Book name can only contain upper case, lower case characters, color(:), comma(,) and space (there should be no consecutive spaces, commas, and colons). And the maximum characters in a character array is 60.

I tried the following way but I am not getting the desired answer.

const int MAX_BOOK_NAME = 60;

bool isValidBookName(char bookName[MAX_BOOK_NAME])
{
     int length = strlen(bookName);
     if (length > 59)
     {
          return false;
     }

     for (int i = 0; i < 59; i++)
     {
          if (bookName[i] < 'A' || bookName[i] > 'Z' || bookName[i] < 'a' || bookName[i] > 'z' || bookName[i] != '  ' || bookName[i] != ':' || bookName[i] != ',')
          {
               return false;
          }
     }

     return true;
}

int main()
{
     char arr[60];
     cout << "Please Enter Your Book Id : ";
     cin.getline(arr, 60);
     cout << "Your Entered Name is " << isValidBookName(arr) << endl;
}
11
  • If length > 59 is true, then you're already in trouble as that would mean the buffer is overflowed and you already have undefined behavior because of that. Commented Jan 13, 2020 at 14:24
  • 2
    I also suggest you take a look as the std::isalpha function, as your way to check for letters is encoding-specific and C++ doesn't assume any specific encoding. Commented Jan 13, 2020 at 14:25
  • And the loop for (int i = 0; i < 59; i++) disregard the string null terminator, and could go out into uninitialized areas of the array which lead to undefined behavior. Commented Jan 13, 2020 at 14:26
  • 1
    Is there a reason you use C strings in C++? Commented Jan 13, 2020 at 14:27
  • 1
    "I tried the following way but I am not getting the desired answer. " Please provide expected input and expected output Commented Jan 13, 2020 at 14:28

2 Answers 2

2
bookName[i] < 'A' || bookName[i] > 'Z' || bookName[i] < 'a' || bookName[i] > 'z'

These checks match every character because for example (assuming ASCII or compatible encoding) all capital letters match the condition bookName[i] < 'a' and all smaller case letters match bookName[i] > 'Z'. Since these checks are used to match an invalid character, the check is wrong.

std::isalpha can simplify your program greatly, as pointed out in comments.

Sign up to request clarification or add additional context in comments.

Comments

1

The logic of your character check is flawed.

The requirement in your question is that each character must be a letter, or a comma, or a colon, or ... etc. The reverse of that is not "not a letter or not a comma or not a colon or ... etc." The reverse is "not a letter and not a comma and not a colon and ... etc."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.