在 C++ 中使用字符数组验证用户输入 [英] 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 的整数.
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;
}
}
推荐答案
您的代码中存在一些错误.
There's a few errors that you have in your code.
首先,您将所有检查初始化为 true
,并且永远不要将任何内容设置为 false
,因此答案将始终为 true
.实际上,您希望将它们全部初始化为 false
,并在满足所有条件时更改为 true
,或者假设 true
,并设置为false
不满足条件时.
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.
其次,您对 0-9
值的检查不正确.您不能将 bookId[i]
与 0
进行比较,而是要将其与字符 '0'
进行比较.另请注意,您的问题还说 1-9
而不是 0-9
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
第三,您对 A-Z
的检查不正确(注意,此问题也适用于 0-9
).你的代码基本上是 bookId[i]
大于或等于 'A' 或小于或等于 Z
,这是 总是 将是 true
.
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;
}
此方法不需要任何布尔变量.相反,我假设 true
(最后一个 return
语句)并尝试证明 false
.一旦出现false
,您无需进行任何其他检查即可返回.
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.
这篇关于在 C++ 中使用字符数组验证用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!