Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何限制用户输入任何4位组合,包括以0开头的组合,但不包括C中的所有其他字符?_C_Validation_Input_Integer_Scanf - Fatal编程技术网

如何限制用户输入任何4位组合,包括以0开头的组合,但不包括C中的所有其他字符?

如何限制用户输入任何4位组合,包括以0开头的组合,但不包括C中的所有其他字符?,c,validation,input,integer,scanf,C,Validation,Input,Integer,Scanf,我正在用C编写一个程序,我希望用户能够更改他们现有的PIN,允许PIN更改的唯一要求是,用户必须输入一个新PIN,该PIN必须是任何组合的4位数字,包括以0开头的数字(例如:0000,0297,0005,0050)PIN码不得包含任何字母字符 然后,他们必须重新输入新的PIN以进行确认 如果重新输入的pin与第一个新输入的pin匹配,则将为用户分配新pin if (temppin1 == temppin2) 我已将temp引脚初始化为int,用于上述比较参数 下面是我的代码片段

我正在用C编写一个程序,我希望用户能够更改他们现有的PIN,允许PIN更改的唯一要求是,用户必须输入一个新PIN,该PIN必须是任何组合的4位数字,包括以0开头的数字(例如:
0000
0297
0005
0050
)PIN码不得包含任何字母字符

然后,他们必须重新输入新的PIN以进行确认 如果重新输入的pin与第一个新输入的pin匹配,则将为用户分配新pin

if (temppin1 == temppin2)
我已将temp引脚初始化为
int
,用于上述比较参数

下面是我的代码片段

        case '2':
           //program asks user to enter their new PIN.
           printf("Enter your new PIN:\n");
           scanf("%04d", &temp_pin1);
           //program asks user to re-enter their new PIN.
           printf("Please re-enter to confirm your new PIN:\n");
           scanf("%04d", &temp_pin2);

           //if the re-entered pin matches the temp_pin1 then then the program will assign the new PIN to the users actual_pin.
           if (temp_pin1 == temp_pin2 && (isalpha(temp_pin1) == 0) && (temp_pin1 >= 1000 && temp_pin1 < 9999)) {
                printf("\n\n New PIN has been confirmed\n\n");
                actual_pin = temp_pin1;
           }
           //if the user input as letter, some other character or a number outside of the four digit including number starting with 0 range the program will give an appropriate error message.
           else if ((temp_pin1 != temp_pin2) && ((temp_pin1 > 1000) && (temp_pin1 < 9999))) {
                printf("error: your new PIN didn't match\n");
                printf("We couldn't confirm your new PIN\n\n");
           }
           //all possible 4 digit are between this range and if a number is entered outside this range the user will be given an appropriate error message.
           else if ((temp_pin1 < 1000) || (temp_pin1 > 9999)) {
                printf("error: Your new pin didn't meet our 4 digit PIN criteria\n\n");
           }
           break;
案例“2”:
//程序要求用户输入他们的新PIN。
printf(“输入新密码:\n”);
scanf(“%04d”、&temp_pin1);
//程序要求用户重新输入他们的新PIN。
printf(“请重新输入以确认您的新PIN:\n”);
scanf(“%04d”、&temp_pin2);
//如果重新输入的pin与temp_pin1匹配,则程序将把新pin分配给用户的实际pin。
如果(温度管脚1==温度管脚2&&(isalpha(温度管脚1)==0)&(温度管脚1>=1000&&温度管脚1<9999)){
printf(“\n\n新PIN已确认\n\n”);
实际引脚=温度引脚1;
}
//如果用户以字母、其他字符或四位数以外的数字输入,包括以0开头的数字,程序将给出相应的错误消息。
否则,如果((temp_pin1!=temp_pin2)&((temp_pin1>1000)&(temp_pin1<9999))){
printf(“错误:您的新PIN不匹配\n”);
printf(“我们无法确认您的新PIN\n\n”);
}
//所有可能的4位数字都在此范围内,如果输入的数字超出此范围,用户将收到相应的错误消息。
如果((温度pin1<1000)| |(温度pin1>9999)){
printf(“错误:您的新pin不符合我们的4位pin标准\n\n”);
}
打破
我计算出你可以使用
isalpha()==0
来确保用户输入不接受字母数字,我还计算出以(
1
2
3
4
5
6
7
8
9
)开头的所有可能的4位数字的范围。我需要弄清楚的最后一部分是如何让用户使用以0开头的4位数字(例如:
0000
0297
0005
0050
…),并将其与首字母
0
一起存储,我知道C将以
0
开头的输入整数作为空值,所以我还需要比较这两个变量,看看它们是否相同,可能需要使用不同的数据类型。。。但我不确定


非常感谢您提供任何帮助或深入了解我能做些什么来解决这个棘手的验证问题。

您的程序功能齐全;没有必要做任何改变。0008与8相同,因为它在内部存储为整数。在比较过程中不会有任何问题。如果需要,您可以在必要时将int更改为char[4](如果您想像字符串一样处理终止空字节,则可以将其更改为char[5])。使用密码读取pin

char temp_pin1[5];
scanf("%4s", temp_pin1);


然后检查字符是否为数字。

您的程序功能齐全;没有必要做任何改变。0008与8相同,因为它在内部存储为整数。在比较过程中不会有任何问题。如果需要,您可以在必要时将int更改为char[4](如果您想像字符串一样处理终止空字节,则可以将其更改为char[5])。使用密码读取pin

char temp_pin1[5];
scanf("%4s", temp_pin1);


然后检查字符是否为数字。

scanf
可以使用
%n
捕获处理的字符数。这将拒绝
12
005
12345
的输入<代码>%d只接受数字,因此将拒绝
abc
。不需要
isalpha

使用
getchar
清理输入。如果输入流中存在除换行符以外的任何字符。拒绝输入以防止输入
1234abc

fgets
也可用于读取行
strspn
将对连续匹配的字符进行计数。匹配的数字后面必须跟一个换行符,否则输入将被拒绝

通常不要将FGET和SCANF混合使用。这将同时使用这两个字符,并且只会起作用,因为在
scanf
之后,
getchar
会清除输入流中的挂起字符

#include <stdio.h>
#include <string.h>

int main ( void) {
    char line[100] = "";
    int temp_pin1 = 0;
    int temp_pin2 = 0;
    int result = 0;
    int start = 0;
    int end = 0;
    int index = 0;
    int clean = 0;

    do {
        if ( temp_pin1 != temp_pin2) {
            printf ( "PIN does not match. re-enter PIN\n");
        }
        do {
            if ( clean) {
                printf ( "try again\n");
            }
            end = 0;
            start = 0;
            printf("Enter your new PIN:\n");
            fflush ( stdout);
            if ( ( result = scanf ( " %n%d%n", &start, &temp_pin1, &end))) {
                if ( 4 != end - start) {
                    result = 0;
                }
            }
            while ( ( clean = getchar ( ))) {
                if ( '\n' == clean) {
                    break;
                }
                else {
                    result = 0;//found trailing characters
                }
                if ( EOF == clean) {
                    fprintf ( stderr, "End Of File\n");
                    return 1;
                }
            }
        } while ( 1 != result);

        clean = 0;
        do {
            if ( clean) {
                printf ( "try again\n");
            }
            index = 0;
            clean = 0;
            printf("Please re-enter to confirm your new PIN:\n");
            fflush ( stdout);
            if ( fgets ( line, sizeof line, stdin)) {
                index = strspn ( line, "0123456789");
                clean = 1;
            }
            else {
                fprintf ( stderr, "End Of File\n");
                return 1;
            }
        } while ( 4 != index || '\n' != line[index]);
        sscanf ( line, "%d", &temp_pin2);
    } while ( temp_pin1 != temp_pin2);

    printf ( "new PIN: %04d\n", temp_pin1);

    return 0;
}
#包括
#包括
内部主(空){
字符行[100]=“”;
int temp_pin1=0;
int temp_pin2=0;
int结果=0;
int start=0;
int end=0;
int指数=0;
int clean=0;
做{
如果(温度管脚1!=温度管脚2){
printf(“PIN不匹配。请重新输入PIN\n”);
}
做{
如果(清洁){
printf(“重试\n”);
}
结束=0;
开始=0;
printf(“输入新密码:\n”);
fflush(stdout);
if((结果=scanf(“%n%d%n”,&start,&temp_pin1,&end))){
如果(4!=结束-开始){
结果=0;
}
}
而((clean=getchar()){
如果('\n'==干净){
打破
}
否则{
结果=0;//找到尾随字符
}
如果(EOF==清洁){
fprintf(stderr,“文件结束\n”);
返回1;
}
}
}当(
        case '2':
           //program asks user to enter their new PIN.
           printf("Enter your new PIN:\n");
           if (scanf("%d", &temp_pin1) != 1 || temp_pin1 < 0 || temp_pin1 > 9999) {
               printf("invalid PIN: must be 4 digits\n");
               break;
           }
           //program asks user to re-enter their new PIN.
           printf("Please re-enter to confirm your new PIN:\n");
           if (scanf("%d", &temp_pin2) != 1 || temp_pin1 != temp_pin2) {
               printf("error: your new PIN didn't match\n");
               printf("We couldn't confirm your new PIN\n\n");
               break;
           }
           printf("\n\n New PIN has been confirmed\n\n");
           actual_pin = temp_pin1;
           break;