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

How do I limit a user to enter any 4 digit combination including combinations beginning with 0 and excluding all other characters in C?

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

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

if (temppin1 == temppin2)

我已将临时引脚初始化为 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;

我想你可以使用 isalpha() == 0 来确保用户输入不接受字母数字,我还想出了以 (1,2,3,4,5,6,7,8,9) .我只需要弄清楚的最后一部分是如何让用户以 0 开头的 4 位数字(例如:0000029700050050.. .) 并将其与初始 0 一起存储,我知道 C 会将以 0 开头的输入 int 数字作为空值,但我还需要比较这两个变量并查看它们是否相同,也许需要使用不同的数据类型...但我不确定。

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

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

读取引脚
char temp_pin1[5];
scanf("%4s", temp_pin1);

char temp_pin1[4];
scanf("%c%c%c%c", &temp_pin1[0], &temp_pin1[1], &temp_pin1[2], &temp_pin1[3]);

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

scanf 可以使用 %n 来捕获处理的字符数。这将拒绝 1200512345 的输入。 %d 将只接受数字,因此 abc 将被拒绝。不需要 isalpha.
使用 getchar 清理输入。如果输入流中有除换行符以外的任何字符。输入被拒绝以防止输入 1234abc.

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

通常不要混用 FGETS 和 SCANF。这两者都使用并且仅会起作用,因为在 scanfgetchar 清除待处理字符的输入流之后。

#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;
}

测试 isalpha(temp_pin1) 没有意义:您正在测试 PIN 码是否是字母的 ASCII 码,这是无关紧要的:0065 是一个有效的 PIN,但也恰好是'A'.

的 ASCII 码

您应该将 PIN 读作 int 并验证它是否在 09999 范围内(含):

        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;