C 此程序不支持';似乎没有更新值并验证新值

C 此程序不支持';似乎没有更新值并验证新值,c,C,这是我为pin验证编写的代码,但我无法让系统验证新pin。在该程序中,输入来自用户,并使用预定义的pin进行验证。如果用户未能验证该代码,则会显示一个新代码作为新pin #include<stdio.h> #include<stdlib.h> #include<strings.h> #include<windows.h> int pin(); int main() { int pin(); { int pin

这是我为pin验证编写的代码,但我无法让系统验证新pin。在该程序中,输入来自用户,并使用预定义的pin进行验证。如果用户未能验证该代码,则会显示一个新代码作为新pin

#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<windows.h>

int pin();  

int main()
{
    int pin();
    {
        int pin,new_pin,old_pin;
        {
            old_pin=1111;
            printf("Enter your pin: ");
            scanf("%d", &pin);

            if(pin==old_pin)
            {
                printf("PIN succefully verified \n");
            }
            else if(pin!=old_pin)
            {
                printf("PIN couldn't be verified. \n");

                printf("Press 1 to generate a new pin, 2 to re-enter or 0 to exit\n");
                scanf("%d", &new_pin);
                if (new_pin==1)
                {
                    printf("Your new pin is: \n");
                    //generating random 4numbered pin

                    int i,random;
                    for(i=0; i<3; i++)
                        random= (rand() );
                    printf("%d\n", random);

                    main();
                    random=new_pin;
                    new_pin=old_pin;
                }
                else if (new_pin==2) {
                    main();
                }
                else{
                    printf("Exiting the program....\n");
                    exit;
                }
            }
        }
    }
    return(0);
}
#包括
#包括
#包括
#包括
int pin();
int main()
{
int pin();
{
int引脚、新引脚、旧引脚;
{
旧针=1111;
printf(“输入您的pin:”);
scanf(“%d”和pin);
如果(引脚==旧引脚)
{
printf(“PIN成功验证\n”);
}
否则如果(pin!=旧pin)
{
printf(“无法验证PIN。\n”);
printf(“按1生成新pin,按2重新输入或按0退出\n”);
scanf(“%d”和新的_-pin);
如果(新引脚==1)
{
printf(“您的新pin是:\n”);
//生成随机4号管脚
int i,随机;
对于(i=0;我这里怎么了?
首先,需要重新查看代码。您引用的函数名为
pin()
,这是我们不知道的源代码。接下来,我们遇到了名称的微小冲突(
int-pin;
int-pin();
)。代码块太多。变量的命名很差(
new\u pin
不是新pin,
old\u pin
不是旧pin)。生成pin的循环是无用的,因为您重写变量而不存储旧的随机数。禁止使用
printf
中没有
\
的多行字符串(以及其他字符串)(我在原始代码附近发现了它们).
exit
缺少语法,这是关于语法、代码优化和命名质量的

代码递归地调用main,这肯定不是一个好主意(除非您知道自己在做什么!)

如何修复代码。 我已经格式化了它并删除了
int pin();
声明,因为它们是冗余的。代码块的复杂性降低了,不必要的库导入也被剥离了。for循环简化了(因为它是冗余的)。代码现在看起来像这样:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int pin, new_pin, old_pin = 1111;
    printf("Enter your pin: ");
    scanf("%d", &pin);
    if (pin == old_pin)
        printf("PIN succefully verified.\n");
    else if (pin != old_pin) {
        printf("PIN couldn't be verified.\n");
        printf("Press 1 to generate a new pin, 2 to re-enter or 0 to exit\n");
        new_pin = getchar();
        if (new_pin == '1') {
            int i, random;
            printf("Your new pin is: %4d\n" rand() % 9999);
            main();
        } else if (new_pin=='2')
            main();

        else {
            printf("Exiting the program....\n");
            return 0;
        }
    }
}
#include <stdio.h>
#include <stdlib.h>

int proc() {
    int pin;
    static int validPin = 1111;

    printf("Enter your pin: ");
    scanf("%d", &pin);
    if (pin == validPin)
        printf("PIN succefully verified.\n");
    else if (pin != validPin) {
        int command;
        printf("PIN couldn't be verified.\n");
        printf("Press 1 to generate a new pin, 2 to fall back or any other key to exit.\n");
        command = getchar();
        if (command == '1') {
            validPin = rand() % 9999;
            printf("Your new pin is: %4d.\n", validPin);
            return 0;
        } else if (command == '2')
            return 0;
        else 
            return 1;
    }

    return 1;
}

int main(void) {
    while(!proc());
    printf("Exiting the program.\n");
}
附录 通过将god procedure
proc()
分解为pin验证器和命令处理程序,我的最终代码本可以得到改进,但应用程序非常简单,可以保持简洁。在您的学习过程中,这个应用程序示例可能是训练重构技能的一段不错的代码


希望我的评论帮助你。< /p>对< C++ >主代码的递归调用得到一组新的局部变量。此外,技术上不允许调用<代码>主< /C> >。这两种语言都不禁止递归main。@KrzysztofSzewczyk:C不禁止递归
main();C++。我格式化了代码。它有无用的块。谢谢!我是新的编程人员,我们最近有一个项目。今天学到了很多东西。谢谢伙伴!@ ASTIK,如果你检查绿色标记来批准我的答案,那就太好了。
#include <stdio.h>
#include <stdlib.h>

int proc() {
    /* ... the code in partially unmodified form goes here. */
}

int main(void) {
    while(!proc());
}
#include <stdio.h>
#include <stdlib.h>

int proc() {
    int pin;
    static int validPin = 1111;

    printf("Enter your pin: ");
    scanf("%d", &pin);
    if (pin == validPin)
        printf("PIN succefully verified.\n");
    else if (pin != validPin) {
        int command;
        printf("PIN couldn't be verified.\n");
        printf("Press 1 to generate a new pin, 2 to fall back or any other key to exit.\n");
        command = getchar();
        if (command == '1') {
            validPin = rand() % 9999;
            printf("Your new pin is: %4d.\n", validPin);
            return 0;
        } else if (command == '2')
            return 0;
        else 
            return 1;
    }

    return 1;
}

int main(void) {
    while(!proc());
    printf("Exiting the program.\n");
}
        int command;
        printf("PIN couldn't be verified.\n");
        printf("Press 1 to generate a new pin, 2 to fall back or 0 to exit:\n");
        scanf("%d", &command);
        if (command == 1) {
            validPin = rand() % 9999;
            printf("Your new pin is: %4d.\n", validPin);
            return 0;
        } else if (command == 0)
            return 1;
          else
            return 0;